Spring整合Mybatis

Spring整合Mybatis就是将原本Mybatis中的Mapper.xml文件在Spring容器中注册为对象。

导入mybatis-spring依赖包

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.2</version>
</dependency>

创建实体类User,创建UserMapper接口和UserMapper.xml实现的配置文件。

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}
public interface UserMapper {
    List<User> getUser();
}
<?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.along.mapper.UserMapper">
    <select id="getUser" resultType="user">
        select * from user;
    </select>
</mapper>

创建一个spring-dao.xml文件,将原本Mybatis核心配置文件连接数据库的工作代替,创建一个数据源的bean。原本使用Mybatis需要将核心配置文件转化为流,再用SqlSessionFactoryBuilder的build方法创建一个SqlSessionFactory对象,再用SqlSessionFactory的openSession方法创建一个SqlSession对象,再用SqlSession的getMapper方法读取接口的字节码创建一个接口的实例对象,这样我们就可以使用这个实例对象的各种方法了。但在spring容器中,这些步骤都可以省略了,我们先生成一个SqlSessionFactory的bean,里面配置数据源,绑定核心配置文件和Mapper.xml文件等属性。然后生成一个SqlSessionTemplate的bean,这个就相当于Mybatis中的SqlSession,功能是一致的。这样我们如果需要使用Mapper接口中的方法就不需要再写Mybatis中这些繁琐的步骤了,使用组合的方法就可以直接使用,使用组合的时候在类里面加上set方法,这个类也需要注册到spring容器中去,使用set注入。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--DateSource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"/>
        <property name="username" value="数据库账号"/>
        <property name="password" value="数据库密码"/>
    </bean>
    <!--SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定Mybatis的核心配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/along/mapper/UserMapper.xml"/>
    </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

写一个Mapper接口的实现类,就是用组合的方法,从spring容器中取出sqlSession。

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> getUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUser();
    }
}

将接口实现类注册到spring容器中去,以后使用dao层的方法就不用使用Mybatis中那些繁琐的步骤了,Mybatis已经被整合到spring中了,直接创建spring容器从里面拿就可以了。

<bean id="userMapperImpl" class="com.along.mapper.UserMapperImpl">
  <property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>

总结整合步骤

1.编写数据源配置
2. sqlSessionFactory
3. sqlSessionTemplate
4.需要给接口加实现类
5.将自己写的实现类,注入到Spring中
6.测试使用即可!

posted @ 2023-09-08 09:40  数星观月  阅读(18)  评论(0)    收藏  举报