1. 程式人生 > >spring入門(六) spring mvc+mybatis

spring入門(六) spring mvc+mybatis

odi 創建 mode utf from 針對 namespace tid oca

1.引入依賴

 1 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
 2 <dependency>
 3     <groupId>org.mybatis</groupId>
 4     <artifactId>mybatis</artifactId>
 5     <version>3.4.5</version>
 6 </dependency>
 7 
 8 <!--spring的版本是5.0.9,mybatis-spring的版本要是1.3+ 
--> 9 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 10 <dependency> 11 <groupId>org.mybatis</groupId> 12 <artifactId>mybatis-spring</artifactId> 13 <version>1.3.2</version> 14 </dependency>

2.在springmvc-config.xml增加mybatis配置

 1 <!-- 針對myBatis的配置項 -->
 2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 3     <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 -->
 4     <property name="dataSource" ref="dataSource" />
 5     <!-- 自動掃描classpath:mapper/*.xml         
--> 6 <property name="mapperLocations" value="classpath:mapper/*.xml" /> 7 </bean> 8 <!-- 配置掃描器 --> 9 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 10 <!-- 掃描com.ice.dao這個包以及它的子包下的所有映射接口類,省去了在每個mapper接口上註解 --> 11 <property name="basePackage" value="com.ice.dao" /> 12 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 13 </bean>

3.在classpath:mapper/ 創建CustomerMapper.xml

註意創建的位置,是在上面設置的 mapperLocations.

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <!--  namespace要跟後面該xml對應的mapper類的全名一致 -->
 4 <mapper namespace="com.ice.dao.CustomerMapper">
 5     <!--  id設置為mapper類對應的方法名, parameterType跟方法的參數類型一致,resultType跟方法的返回類型一致.
 6     如果返回單個對象,mybatis會自動調用selectOne方法;如果返回的是List,mybatis會自動調用selectList方法.
 7     -->    
 8     <select id="getUser" parameterType="java.lang.String"
 9             resultType="com.ice.model.Customer">
10         select id,name from customer where name=#{name}
11     </select>
12 </mapper>

4.創建CustomerMapper.xml對應的CustomerMapper.java

註意創建的位置,是在上面設置的 basePackage.

1 package com.ice.dao;
2 
3 import com.ice.model.Customer;
4 //不用在xml裏配置該bean,也不用註解.已經配置了MapperScanner 
5 //<property name="basePackage" value="com.ice.dao" />
6 public interface CustomerMapper {
7 
8     Customer getUser(String name);
9 }

5.簡單測試(在controller裏測試,也可考慮springTest)

正式項目裏,要有service層.

 1 package com.ice.controller;
 2 
 3 import com.ice.dao.CustomerMapper;
 4 import com.ice.model.Customer;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 @Controller
11 @RequestMapping("/mybatis")
12 public class MybatisController {
13 
14     @Autowired
15     private CustomerMapper customerMapper;
16 
17     @RequestMapping("/query")
18     @ResponseBody
19     public String index() {
20         Customer user = customerMapper.getUser("hello");
21         return "顧客id" + user.getId();
22     }
23 }

spring入門(六) spring mvc+mybatis