Spring整合Mybatis

1、需要的jar包

mybatis包:mybatis-3.2.7

spring包:spring-4.1.3

spring整合mybatis包:mybatis-spring-1.2.2

数据库连接包:mysql-connector-java-5.1.7

 

2、Spring中ApplicationContext.xml的配置

1)数据库连接交给Spring进行配置,在ApplicationContext.xml中配置如下:

    <!-- 加载配置文件 -->
    <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>

2)SqlSessionFactory交给Spring进行管理

    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
    </bean>

 

3、Mybatis中SqlMapConfig.xml的配置

    <mappers>
        <mapper resource="User.xml"/>
        <!-- 
        使用class属性引入接口的全路径名称:
        使用规则:
            1、接口的名称和映射文件名称要完全一致
            2、接口和映射文件要放在同一个目录下
         -->
        <mapper class="cn.itheima.mapper.UserMapper"/>
    </mappers>

 

4、在ApplicationContext.xml中配置实体类bean

这里有两种实现方式,一种是原生DAO,另一种是Mapper接口代理。

1)原生DAO方式实现

    <!-- 配置原始DAO实现 -->
    <bean id="userDAO" class="cn.itheima.dao.UserDAOImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

DAO实现类中还需要继承SqlSessionDaoSupport类

package cn.itheima.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.itheima.pojo.User;

public class UserDAOImpl extends SqlSessionDaoSupport implements UserDAO {

    @Override
    public User findUserById(Integer id) {
        // SqlSession是线程不安全的,所以最佳使用范围是方法里
        SqlSession session = this.getSqlSession();
        User user = session.selectOne("test.findUserById", id);
        return user;
    }
    @Override
    public List<User> findUserByUserName(String username) {
        SqlSession session = this.getSqlSession();
        List<User> userList = session.selectList("test.findUserByUserName", username);
        return userList;
    }
}

2)Mapper接口代理

    <!-- Mapper接口代理实现 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 配置Mapper接口的全路径名称 -->
        <property name="mapperInterface" value="cn.itheima.mapper.UserMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

 

5、测试

原生DAO实现方式测试

package cn.itheima.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itheima.dao.UserDAO;
import cn.itheima.pojo.User;


public class UserDAOTest {

    private ApplicationContext applicationContext;
    
    @Before
    public void setUp() throws Exception {
        String configLocation = "classpath:ApplicationContext.xml";
        applicationContext = new ClassPathXmlApplicationContext(configLocation);
    }
    
    @Test
    public void testFindUserById() throws Exception {
        UserDAO userDAO = (UserDAO) applicationContext.getBean("userDAO");
        User user = userDAO.findUserById(1);
        System.out.println(user);
    }
}

Mapper接口代理实现测试

package cn.itheima.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itheima.mapper.UserMapper;
import cn.itheima.pojo.User;

public class UserMapperTest {

private ApplicationContext applicationContext;
    
    @Before
    public void setUp() throws Exception {
        String configLocation = "classpath:ApplicationContext.xml";
        applicationContext = new ClassPathXmlApplicationContext(configLocation);
    }
    
    @Test
    public void testFindUserById() throws Exception {
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        User user = userMapper.findUserById(1);
        System.out.println(user);
    }
}

 

原文地址:https://www.cnblogs.com/binaway/p/9185304.html