1、采用MapperScannerConfigurer,它將會查找類路徑下的映射器并自動將它們創建成MapperFactoryBean。
spring-mybatis.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:mvc= "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.1.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.1.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動掃描 --> <context:component-scan base- package = "com.hua.saf" /> <!-- 引入配置文件 --> <bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "location" value= "classpath:jdbc.properties" /> </bean> <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" > <property name= "driverClassName" value= "${driver}" /> <property name= "url" value= "${url}" /> <property name= "username" value= "${username}" /> <property name= "password" value= "${password}" /> <!-- 初始化連接大小 --> <property name= "initialSize" value= "${initialSize}" /> <!-- 連接池最大數量 --> <property name= "maxActive" value= "${maxActive}" /> <!-- 連接池最大空閑 --> <property name= "maxIdle" value= "${maxIdle}" /> <!-- 連接池最小空閑 --> <property name= "minIdle" value= "${minIdle}" /> <!-- 獲取連接最大等待時間 --> <property name= "maxWait" value= "${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "dataSource" /> <!-- 自動掃描mapping.xml文件,**表示迭代查找 --> <property name= "mapperLocations" value= "classpath:com/hua/saf/**/*.xml" /> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 ,包下的類需要使用 @MapperScan 注解,否則容器注入會失敗 --> <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basePackage" value= "com.hua.saf.*" /> <property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" /> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" /> </bean> </beans> |
UserMapper.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!-- 為這個mapper指定一個唯一的namespace,namespace的值習慣上設置成包名+sql映射文件名,這樣就能夠保證namespace的值是唯一的 例如namespace= "me.gacl.mapping.userMapper" 就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后綴) --> <mapper namespace= "com.hua.saf.dao.UserDao" > <!-- 在select標簽中編寫查詢的SQL語句, 設置select標簽的id屬性為getUser,id屬性值必須是唯一的,不能夠重復, 使用parameterType屬性指明查詢時使用的參數類型,resultType屬性指明查詢返回的結果集類型 resultType= "com.hua.saf.User" 就表示將查詢結果封裝成一個User類的對象返回,User類就是t_user表所對應的實體類 --> <!-- 根據id查詢得到一個user對象--> <select id= "getUser" parameterType= "int" resultType= "com.hua.saf.pojo.User" > select * from t_user where id=#{id} </select> </mapper> |
dao類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 這里的@MapperScan就是上面所講的Mapper掃描器中所需要的配置,會自動生成代理對象。 * 注意,接口中的方法名稱要和對應的MyBatis映射文件中的語句的id值一樣,因為生成的 * 動態代理,會根據這個匹配相應的Sql語句執行。另外就是方法的參數和返回值也需要注 * 意。接口中的方法如何定義,對應的MyBatis映射文件就應該進行相應的定義。 * 最后,標注中的userDao是用來作為Spring的Bean的id(或name)進行使用的,方便我 * 們在Service層進行注入使用。 */ @MapperScan public interface UserDao { //此處的方法名必須和mapper中的映射文件中的id同名 //回去映射文件中通過com.hua.saf.dao.UserDao.getUser,即this.getClass().getName()+".getUser" public User getUser( int id); } |
service類
1
2
3
4
5
6
7
8
|
@Service ( "userService" ) public class UserServiceImpl implements IUserService { @Resource private UserDao userDao; public User getUser( int id) { return userDao.getUser(id); } } |
2、采用接口org.apache.ibatis.session.SqlSession的實現類org.mybatis.spring.SqlSessionTemplate。
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.來創建。MyBatis-Spring 中,使用了SqlSessionFactoryBean來替代。SqlSessionFactoryBean有一個必須屬性dataSource,另外其還有一個通用屬性configLocation(用來指定mybatis的xml配置文件路徑)。
spring-mybatis.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:mvc= "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.1.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.1.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動掃描 --> <context:component-scan base- package = "com.hua.saf" /> <!-- 引入配置文件 --> <bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "location" value= "classpath:jdbc.properties" /> </bean> <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" > <property name= "driverClassName" value= "${driver}" /> <property name= "url" value= "${url}" /> <property name= "username" value= "${username}" /> <property name= "password" value= "${password}" /> <!-- 初始化連接大小 --> <property name= "initialSize" value= "${initialSize}" /> <!-- 連接池最大數量 --> <property name= "maxActive" value= "${maxActive}" /> <!-- 連接池最大空閑 --> <property name= "maxIdle" value= "${maxIdle}" /> <!-- 連接池最小空閑 --> <property name= "minIdle" value= "${minIdle}" /> <!-- 獲取連接最大等待時間 --> <property name= "maxWait" value= "${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "dataSource" /> <property name= "configLocation" value= "classpath:sqlMapConfig.xml" /> <!-- 自動掃描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中單獨指定xml文件--> <property name= "mapperLocations" value= "classpath:com/hua/saf/**/*.xml" /> </bean> <!-- mybatis spring sqlSessionTemplate,使用時直接讓spring注入即可 --> <bean id= "sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate" > <constructor-arg index= "0" ref= "sqlSessionFactory" ></constructor-arg> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" /> </bean> </beans> |
sqlMapConfig.xml
1
2
3
4
5
6
7
8
9
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <typeAliases> <typeAlias type= "com.hua.saf.pojo.User" alias= "User" /> </typeAliases> </configuration> |
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class User { private int id; private String username; private String password; private int age; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
UserDao.java
1
2
3
4
5
6
7
8
|
@Repository public class UserDao{ @Resource private SqlSessionTemplate sqlSessionTemplate; public User getUser( int id) { return sqlSessionTemplate.selectOne( this .getClass().getName() + ".getUser" , 1 ); } } |
UserService.java
1
2
3
4
5
6
7
8
|
@Service public class UserService{ @Resource private UserDao userDao; public User getUser( int id) { return userDao.getUser(id); } } |
3、采用抽象類org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
spring-mybatis.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:mvc= "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.1.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.1.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動掃描 --> <context:component-scan base- package = "com.hua.saf" /> <!-- 引入配置文件 --> <bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "location" value= "classpath:jdbc.properties" /> </bean> <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" > <property name= "driverClassName" value= "${driver}" /> <property name= "url" value= "${url}" /> <property name= "username" value= "${username}" /> <property name= "password" value= "${password}" /> <!-- 初始化連接大小 --> <property name= "initialSize" value= "${initialSize}" /> <!-- 連接池最大數量 --> <property name= "maxActive" value= "${maxActive}" /> <!-- 連接池最大空閑 --> <property name= "maxIdle" value= "${maxIdle}" /> <!-- 連接池最小空閑 --> <property name= "minIdle" value= "${minIdle}" /> <!-- 獲取連接最大等待時間 --> <property name= "maxWait" value= "${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "dataSource" /> <property name= "configLocation" value= "classpath:sqlMapConfig.xml" /> <!-- 自動掃描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中單獨指定xml文件--> <property name= "mapperLocations" value= "classpath:com/hua/saf/**/*.xml" /> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" /> </bean> </beans> |
sqlMapConfig.xml
1
2
3
4
5
6
7
8
9
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <typeAliases> <typeAlias type= "com.hua.saf.pojo.User" alias= "User" /> </typeAliases> </configuration> |
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class User { private int id; private String username; private String password; private int age; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
UserDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Repository public class UserDao extends SqlSessionDaoSupport{ public User getUser( int id) { return this .getSqlSession().selectOne( this .getClass().getName() + ".getUser" , 1 ); } //使用SqlSessionDaoSupport必須注意,此處源碼1.1.1中有自動注入,1.2中取消了自動注入,需要手工注入,侵入性強 //也可在spring-mybatis.xml中如下配置,但是這種有多少個dao就要配置到少個,多個dao就很麻煩。 //<bean id="userDao" class="com.hua.saf.dao.UserDao"> // <property name="sqlSessionFactory" ref="sqlSessionFactory"/> //</bean> @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // TODO Auto-generated method stub super .setSqlSessionFactory(sqlSessionFactory); } } |
UserService.java
1
2
3
4
5
6
7
8
|
@Service public class UserService{ @Resource private UserDao userDao; public User getUserss( int id) { return userDao.getUser( 1 ); } } |
以上所述是小編給大家介紹的基于spring與mybatis三種整合方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/wangmingshun/p/5674633.html