Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合

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提供。

Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合_第1张图片Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合_第2张图片

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"/>

    
    <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>
    
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <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">

<mapper namespace="test">
    
    
    
    <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>
      
      <typeAliases>
          <package name="joanna.yan.po"/>
      typeAliases>

    <mappers>
        <mapper resource="sqlmap/User.xml"/>
        
        <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。

 
    <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">

<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。

 
    <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批量扫描。

 
    <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的配置就可以省略掉了。

Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合_第3张图片

测试程序:

@Test
    public void findUsetByIdTest() throws Exception{
        UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
        User user=userMapper.findUserById(1);
        System.out.println(user);
    }

 

你可能感兴趣的:(Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合)