Spring整合mybatis

Spring整合mybatis

整合过程

1.1 导包

(1)Spring包

(2)mybatis包

(3)导入整合包

Spring整合mybatis

1.2搭建环境

Spring整合mybatis

创建applicationContext.xml,sqlMapConfig.xml,db.properties,log4j.properties等文件

1.3开始整合mybatis

不管哪种整合都依赖于sessionFactory对象,sessionFactory需要依赖于读取mybatis核心配置文件中的信息。

分为三层:

一、别名的设置

二、数据库的链接信息

三、读取映射文件

其中的第二层可以使用连接池来取代,实际上sqlSession需要依赖两个一个是DataSource还有就是核心配置文件

(一) 原始整合


public class User {
    private Integer id;
    private String name;
    private String sex;
    private String address;
    private Integer balance;

    public User() {
    }

    public User(Integer id, String name, String sex, String address, Integer balance) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.address = address;
        this.balance = balance;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", balance=" + balance +
                '}';
    }
}

创建dao层接口,并实现类


public interface UserDao {
    User findUserById(Integer id);
}

 


public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Override
    public User findUserById(Integer id) {
        User user = getSqlSession().selectOne("test.findUserById", 1);
        return user;
    }
}

配置applicationContext.xml文件和数据库连接文件db.properties以及userMapper.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
>


    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="password" value="${jdbc.password}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
    </bean>

    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:resources/mybatis/sqlMapConfig.xml"></property>
    </bean>


    <bean name="userDao" class="cd.hd.dao.impl.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    <bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        <property name="mapperInterface" value="cd.hd.mapper.UserMapper"></property>
    </bean>

 

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url
=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.username
=root
jdbc.password
=1234

 

<?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="cd.hd.mapper.UserMapper">-->
    
<mapper namespace="test">

    <select id="findUserById" parameterType="int" resultType="user">
        SELECT * FROM mbs WHERE id=#{id}
    </select>
</mapper>

最后书写测试代码


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:resources/spring/applicationContext.xml")
public class Demo {

    @Resource(name = "userDao")
    private UserDao userDao;

    @Test
    public void fun(){
        User userById = userDao.findUserById(1);
        System.out.println(userById);
    }

测试结果

Spring整合mybatis

(二)mapper整合

需要一个bean类--》mapperFactoryBean类(需要取名字)

该类依赖于两个(sqlSessionFactory和接口的位置)

直接进行测试

这种方式需要写很多的mapper,mybatis提供了升级版

配置一个扫描类,自动扫描某个位置下的mapper,并起名为对应的小写

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cd.hd.mapper"></property>
</bean>

userMapper.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="cd.hd.mapper.UserMapper">
    <!--<mapper namespace="test">-->

    
<select id="findUserById" parameterType="int" resultType="user">
        SELECT * FROM mbs WHERE id=#{id}
    </select>
</mapper>

Namespace的属性值设为userMapper接口的相对路径

测试代码直接使用注解


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:resources/spring/applicationContext.xml")
public class Demo {

   @Resource(name = "userMapper")
    private UserMapper userMapper;
    @Test
    public void fun1(){
        User userById = userMapper.findUserById(1);
        System.out.println(userById);
    }
}

测试结果

Spring整合mybatis