mybatis整合spring

youcongtech發表於2019-01-19

1、整合思路

(1)資料來源資訊交給spring管理

(2)SqlSessionFactory交給spring進行單例管理

(3)由spring來管理原始dao的實現類或者mapper代理的代理類

2、案例需求

(1)使用原始dao方式和mapper代理方式實現以下功能:
根據使用者ID查詢商品資訊

3、工程建立

(1)新增jar:ysql的驅動包;Mybatis的核心包和依賴包;Mybatis和spring的整合包;Spring的包;dbcp資料庫連線池包
(2)圖解:
這裡寫圖片描述

4、整合配置檔案

(1)mybatis配置檔案:
在config下,建立mybatis目錄,然後建立SqlMapConfig.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>

    <!-- 載入對映檔案 -->
    <mappers>
        <mapper resource="mybatis/sqlmap/User.xml"/>
    </mappers>
</configuration>

(2)spring配置檔案:
在config下,建立spring目錄,然後建立applicationContext.xml

<?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: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 ">
    <!-- 載入Java配置檔案 -->
    <context:property-placeholder location="db.properties"/>

    <!-- 建立資料來源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>

    <!-- SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis的全域性配置路徑 -->
        <property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
        <!-- 資料來源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- userDao -->
    <bean id="userDao" class="com.san.dao.UserDaoImpl">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

    <!-- 配置UserMapper代理類 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 設定代理類的介面 -->
        <property name="mapperInterface" value="com.san.mapper.UserMapper"/>
        <!-- 注入SqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

5、整合程式碼—原始dao開發

(1)對映檔案:
在config/mybatis下建立sqlmap,然後建立User.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">
<mapper namespace="test">
    <!-- 根據使用者ID查詢使用者資訊 -->
    <select id="findUserById" resultType="com.san.po.User" parameterType="int">
        select * from user where id=#{id}
    </select>
</mapper>

(2)dao程式碼:

public interface UserDao {
    /**
     * 根據使用者ID查詢使用者資訊
     * @param id
     * @return
     */
    public User findUserById(int id);
}
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    @Override
    public User findUserById(int id) {
        return this.getSqlSession().selectOne("test.findUserById",id);
    }

}

(3)配置UserDao實現類:
在applicationContext.xml中配置UserDao實現類

<!-- userDao -->
<bean id="userDao" class="com.san.dao.UserDaoImpl">
    <!-- 注入sqlSessionFactory -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

(4)測試程式碼:

private ApplicationContext cxt;
@Before
public void setUp() throws Exception {
    cxt = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}

@Test
public void testFindUserById() {
    UserDao userDao = (UserDao) cxt.getBean("userDao");
    User user= userDao.findUserById(1);
    System.out.println(user);
}

6、整合程式碼—mapper代理

(1)對映檔案:
將對映檔案放到UserMapper介面的同包下

<?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.san.mapper.UserMapper">

    <!-- 根據使用者ID查詢使用者資訊 -->
    <select id="findUserById" parameterType="int" resultType="com.san.po.User">
        select * from user where id=#{id}
    </select>
</mapper>

(2)mapper介面:

public interface UserMapper {
    /**
     * 根據使用者資訊查詢使用者資訊
     * @param id
     * @return
     */
    public User findUserById(int id);
}

(3)配置mapper代理類:
單個mapper代理類配置:

<!-- 配置UserMapper代理類 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <!-- 設定代理類的介面 -->
    <property name="mapperInterface" value="com.san.mapper.UserMapper"/>
    <!-- 注入SqlSessionFactory -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

批量設定mapper代理類:

<!-- 批量配置mapper代理類,預設bean的id為類名首字母小寫 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 配置掃描的包 -->
    <property name="basePackage" value="com.san.mapper"></property>

    <!-- 預設不需要配置SqlSessionFactory(只有一個SqlSessionFactory時),單獨配置也可以 -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

(4)測試程式碼:

private ApplicationContext cxt;
@Before
public void setUp() throws Exception {
    cxt = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}

@Test
public void testFindUserById() {
    UserMapper mapper = (UserMapper) cxt.getBean("userMapper");
    User user = mapper.findUserById(1);
    System.out.println(user);
}

相關文章