十一、mybatis和spring整合
想要整合mybatis和spring,那麽我們首先要知道這兩個框架是幹嘛的,對於mybatis我們前面幾篇博客已經有了很詳細的介紹,我們通過加載mybatis-configuration.xml 文件來產生SqlSessionFactory,然後通過SqlSessionFactory去產生sqlSession,最後用 sqlSession對數據庫表所映射的實體類進行增刪改查操作。而spring是幹嘛的呢,簡單來說,通過spring的DI和IOC,能幫助我們產生對象並管理對象的聲明周期,而sprig的AOP也能幫助我們管理對象的事務。那麽整合思路就很清晰了。
1、需要spring通過單例的方式管理 SqlSessionFactory,並用 SqlSessionFactory 去創建 sqlSession
2、持久層的 mapper 需要spring 管理
本篇所有源碼鏈接:https://pan.baidu.com/s/1dG00Ksp 密碼:d1ev
回到頂部1、創建mybatis-spring 工程,並導入相應的 jar 包(詳情見上面源碼)
回到頂部
2、在 spring 全局配置文件中 applicationContext.xml 中配置 SqlSessionFactory,以及數據源
①、我們將數據庫配置信息寫入classpath 目錄的 db.properties 文件中
1 2 3 4 5 6 |
#db.properties
dataSource=org.apache.commons.dbcp.BasicDataSource jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql: //localhost:3306/mybatisrelation
jdbc.username=root
jdbc.password=root
|
②、在mybatis全局配置文件mybatis-configuration.xml 開啟二級緩存,以及別名定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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>
<!--開啟二級緩存 -->
<settings>
<setting name= "cacheEnabled" value= "true" />
</settings>
<!-- 包的別名定義 -->
<typeAliases>
< package name= "com.ys.po" />
</typeAliases>
<!-- 註意:下面的以前有mybatis全局配置文件管理mapper,現在轉移到spring容器管理 -->
<!-- <mappers>
<mapper class = "com.ys.po.UserMapper" />
</mappers> -->
</configuration>
|
③、在 spring 全局配置文件中 applicationContext.xml 中配置 SqlSessionFactory,以及數據源
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 |
<?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:context= "http://www.springframework.org/schema/context"
xmlns:aop= "http://www.springframework.org/schema/aop"
xmlns:tx= "http://www.springframework.org/schema/tx"
xsi:schemaLocation="http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-4.2.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context.xsd
http: //www.springframework.org/schema/aop
http: //www.springframework.org/schema/aop/spring-aop-4.2.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加載classpath下的db.properties文件,裏面配了數據庫連接的一些信息 -->
<context:property-placeholder location= "classpath:db.properties" />
<!-- 配置數據源 -->
<bean id= "dataSource" class = "${dataSource}" destroy-method= "close" >
<property name= "driverClassName" value= "${jdbc.driver}" />
<property name= "url" value= "${jdbc.url}" />
<property name= "username" value= "${jdbc.username}" />
<property name= "password" value= "${jdbc.password}" />
<property name= "maxActive" value= "10" />
<property name= "maxIdle" value= "5" />
</bean>
<!-- 配置sqlSessionFactory,SqlSessionFactoryBean是用來產生sqlSessionFactory的 -->
<bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
<!-- 加載mybatis的全局配置文件,放在classpath下的mybatis文件夾中 -->
<property name= "configLocation" value= "mybatis/mybatis-configuration.xml" />
<!-- 加載數據源,使用上面配置好的數據源 -->
<property name= "dataSource" ref= "dataSource" />
</bean>
</beans>
|
回到頂部
3、mapper 接口開發配置
①、創建 po 類
在com.ys.po包下創建 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 |
package com.ys.po;
public class User {
private int id;
private String username;
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this .sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ "]" ;
}
}
|
②、創建接口 UserMapper.java,以及UserMapper.xml 文件
必須滿足以下四點:
1 2 3 4 |
1 、UserMapper 接口必須要和 UserMapper.xml 文件同名且在同一個包下,也就是說 UserMapper.xml 文件中的namespace是UserMapper接口的全類名
2 、UserMapper接口中的方法名和 UserMapper.xml 文件中定義的 id 一致
3 、UserMapper接口輸入參數類型要和 UserMapper.xml 中定義的 parameterType 一致
4 、UserMapper接口返回數據類型要和 UserMapper.xml 中定義的 resultType 一致
|
UserMapper.java
1 2 3 4 5 6 7 8 |
package com.ys.mapper;
import com.ys.po.User;
public interface UserMapper {
//根據 id 查詢 user 表數據
public User selectUserById( int id) throws Exception;
}
|
UserMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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= "com.ys.mapper.UserMapper" >
<!-- 根據 id 查詢 user 表中的數據
id:唯一標識符,此文件中的id值不能重復
resultType:返回值類型,一條數據庫記錄也就對應實體類的一個對象
parameterType:參數類型,也就是查詢條件的類型
-->
<select id= "selectUserById"
resultType= "com.ys.po.User" parameterType= "int" >
select * from user where id = #{id}
</select>
</mapper>
|
回到頂部
4、在 spring全局配置文件applicationContext.xml 中配置 mapper
1 2 3 4 5 |
<!-- MapperFactoryBean:根據mapper接口生成的代理對象 -->
<bean id= "userMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" >
<property name= "mapperInterface" value= "com.ys.mapper.UserMapper" />
<property name= "sqlSessionFactory" ref= "sqlSessionFactory" />
</bean>
|
測試:
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 |
package com.ys.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ys.mapper.UserMapper;
import com.ys.po.User;
public class UserMapperTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext.xml" ); //得到spring容器
}
@Test
public void testSelectUserById() throws Exception {
UserMapper userMapper = (UserMapper) applicationContext.getBean( "userMapper" );
User user = userMapper.selectUserById( 1 );
System.out.println(user);
}
}
|
從配置中可以看出,使用MapperFactoryBean來產生mapper的代理對象,首先要配置一個mapperInterface,即你要spring產生哪個mapper接口對應的代理對象,將mapper接口的全類名給傳進去,spring就知道要創建對應的代理對象了,然後配置sqlSessionFactory,用來產生sqlSession。
但是我們發現,id="userMapper" 寫死了,如果我們有很多 mapper 接口,那麽我們每一個都需要配置嗎?答案是不用的,我們可以不用配置 id 屬性,然後加上包掃描配置,如下:
1 2 3 4 5 6 7 8 9 10 |
<!--第二種方式:mapper批量掃描
規範:mapper.java和mapper.xml必須在同一個包下,並沒名稱一致
bean的id為mapper類名(首字母小寫)
-->
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
<!-- 指定掃描的包名
如果掃描多個包,那麽每個包中間使用半角逗號分隔 -->
<property name= "basePackage" value= "com.ys.mapper" ></property>
<property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" />
</bean>
|
測試程序還是和上面一樣,那麽spring和mybatis整合就完美了。
十一、mybatis和spring整合