Spring整合mybatis

本文介绍了两种方法将Spring与Mybatis进行整合。方法一包括:使用Spring管理DataSource,配置SqlSessionFactoryBean,创建SqlSessionTemplate,注册DAO和Bean,并进行测试。方法二则利用SqlSessionDaoSupport简化配置,实现UserMapperImpl2。内容涵盖了Spring配置、数据源、SqlSessionFactory、SqlSessionTemplate以及Mapper的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring整合mybatis步骤

方法一

1.替换mybatis的DataSource,注册到Spring,由Spring管理

spring管理jdbc

   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.10</version>
        </dependency>

2.替换SqlSessionFactory

用org.mybatis.spring.SqlSessionFactoryBean

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
 **在SplSessionFactory中绑定mybatis配置文件**

3.创建sqlSessionTemplate

4.在spring中注册dao(spring-dao.xml)

5.注册bean

测试

spring-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--1.DataSource:使用Spring的数据源替换Mybatis的配置-->

    <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;userUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
     <!--2.导入SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="DataSource"/>
        <!--绑定配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/my/mapper/*.xml"/>
    </bean>
    <!--3.sqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
       <!--4.注册dao-->
    <import resource="spring-dao.xml"/>
    <!--5.注册bean-->
     <bean id="userMapperImpl" class="com.my.mapper.UserMapperImpl">
           <property name="sqlSession" ref="sqlSessionTemplate"/>
       </bean>
</beans>

pojo

@Data
public class User {
    private int id;
    private  String name;
    private String pwd;
}

1.userMapper

public interface UserMapper {
    public List<User> queryAll();
}

2.userMapper.xml

<mapper namespace="com.my.mapper.UserMapper">
    <select id="queryAll" resultType="user">
    select * from mybatis.user
    </select>
</mapper>

3.userMapperImpl.xml

public class UserMapperImpl implements UserMapper{
    //原先,所有的操作,都是用SqlSession来执行,现在有用SqlSessionTemplate
    private SqlSessionTemplate sqlSession;
    //由spring配置文件赋值
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

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

4.mybatis-config.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="com.my.pojo"/>
    </typeAliases>
</configuration>

Test

  @Test
    public void my1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapperImpl userMapperImpl = context.getBean("userMapperImpl", UserMapperImpl.class);//spring中id名
        List<User> list = userMapperImpl.queryAll();
        for (User user : list) {
            System.out.println(user);
        }
    }

方法二

UserMapperImpl2 .java

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    @Override
    public List<User> queryAll() {

        return  getSqlSession().getMapper(UserMapper.class).queryAll();
    }
}

注意:SqlSessionDaoSupport 能获取getSqlSession。所以代替spring配置文件中的sqlSessionTemplate
spring-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
            <!--1.DataSource:使用Spring的数据源替换Mybatis的配置-->
        <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;userUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
           <!--2.sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="DataSource"/>
            	<!--绑定配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:com/my/mapper/*.xml"/>
        </bean>
</beans>

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!--3.注册dao-->
    <import resource="spring-dao.xml"/>
    <!--4.注册bean-->
    <bean id="userMapperImpl2" class="com.my.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值