1.整合思路
需要Spring通過單例方式管理SqlSessionFactory。
Spring和MyBatis整合生成代理對象,使用SqlSessionFactory創建SqlSession。(Spring和MyBatis整合自動完成)
持久層的mapper都需要由Spring進行管理。
2.整合環境
創建一個java工程(接近實際開發的工程結構)
jar包:
mybatis3.2.7的jar包
spring3.2.0的jar包
dbcp連接池
數據庫驅動
mybatis和spring的整合包:早期ibatis和spring整合是由spring官方提供,mybatis和spring整合由mybatis提供。
3.spring配置文件
在applicationContext.xml配置sqlSessionFactory和數據源。SqlSessionFactory在mybatis和spring的整合包下。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 數據庫連接池,使用dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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配置 --> <!-- 讓Spring管理SqlSessionFactory使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據庫連接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加載mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean> </beans>
4.Mapper編寫的三種方法
4.1原始dao開發(和spring整合后)
4.1.1User.xml
<?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"> <!--namespace命名空間,作用就是對sql進行分類化的管理,理解為sql隔離 注意:使用mapper代理開發時,namespace有特殊作用 --> <mapper namespace="test"> <!--在映射文件中配置很多sql語句 --> <!--需求:通過id查詢用戶表的記錄 --> <!--id:標識映射文件中的sql,稱為statement的id。將sql語句封裝在mapperStatement的對象中,所有id稱為Statement的id; parameterType:指定輸入參數的類型,這里指定int型; #{}:表示一個占位符; #{id}:其中id表示接收輸入的參數,參數名稱就是id,如果輸入參數是簡單類型,#{}中的參數名可以任意,可以是value或其它名稱; resultType:指定輸出結果所映射的Java對象類型,select指定resultType表示將單條記錄映射成Java對象。 --> <select id="findUserById" parameterType="int" resultType="joanna.yan.po.User"> select * from user where id=#{value} </select> </mapper>
在SqlMapConfig.xml中加載User.xml
<?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> <!-- 批量別名的定義: package:指定包名,mybatis會自動掃描包中的pojo類,自定義別名,別名就是類名(首字母大寫或小寫都可以) --> <typeAliases> <package name="joanna.yan.po"/> </typeAliases> <mappers> <mapper resource="sqlmap/User.xml"/> <!-- 批量加載映射配置文件,mybatis自動掃描包下的mapper接口進行加載; 遵循一定的規范:需要將mapper接口類名和mapper.xml映射文件名稱保持一致,且在一個目錄中; 以上規范的前提是:使用的是mapper代理方法; --> <package name="joanna.yan.mapper"/> </mappers> </configuration>
4.1.2 dao(實現類繼承SqlSessionDaoSupport)
public interface UserDao { //根據id查詢用戶信息 public User findUserById(int id) throws Exception; }
dao接口實現類需要注入SqlSessionFactory,通過spring進行注入。
這里用spring聲明配置方式,配置dao的bean:讓UserDaoImpl實現類繼承SqlSessionDaoSupport。
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Override public User findUserById(int id) throws Exception { //繼承SqlSessionDaoSupport,通過this.getSqlSession()得到sqlSession SqlSession sqlSession=this.getSqlSession(); User user=sqlSession.selectOne("test.findUserById", id); return user; } }
4.1.3配置dao
在applicationContext.xml中配置dao。
<!-- 方法一:原始dao接口 --> <bean id="userDao" class="joanna.yan.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
4.1.4測試程序
public class UserDaoImplTest { private ApplicationContext applicationContext; @Before public void setUp(){ applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void findUsetByIdTest() throws Exception{ UserDao userDao=(UserDao) applicationContext.getBean("userDao"); User user=userDao.findUserById(1); System.out.println(user); } }
4.2mapper代理開發
4.2.1mapper.xml和mapper.java
<?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"> <!--namespace命名空間,作用就是對sql進行分類化的管理,理解為sql隔離 注意:使用mapper代理開發時,namespace有特殊作用,namespace等于mapper接口地址 --> <mapper namespace="joanna.yan.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id=#{value} </select> </mapper>
public interface UserMapper { public User findUserById(int id) throws Exception; }
4.2.2通過MapperFactoryBean創建代理對象
在applicationContext.xml中配置mapper。
<!-- 方法二:mapper配置 MapperFactoryBean:根據mapper接口生成代理對象 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="joanna.yan.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
4.2.3測試程序
public class UserMapperTest { private ApplicationContext applicationContext; @Before public void setUp(){ applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void findUsetByIdTest() throws Exception{ UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper"); User user=userMapper.findUserById(1); System.out.println(user); } }
此方法的問題:需要針對每個mapper進行配置,麻煩。
4.3通過MapperScannerConfigurer進行mapper掃描(建議使用)
在applicationContext.xml中配置mapper批量掃描。
<!-- 方法三:mapper批量掃描 從mapper包中掃描出mapper接口,自動創建代理對象并且在spring容器中注冊 遵循規范:將mapper.java和mapper.xml映射文件名稱保持一致,且在一個目錄中。 自動掃描出來的mapper的bean的id為mapper類名(首字母小寫) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定掃描的包名 如果掃描多個包,每個包中間使用半角逗號分隔 --> <property name="basePackage" value="joanna.yan.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
此時,SqlMapConfig.xml中批量加載joanna.yan.mapper下mapper的配置就可以省略掉了。
測試程序:
@Test public void findUsetByIdTest() throws Exception{ UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper"); User user=userMapper.findUserById(1); System.out.println(user); }
文章列表