Spring(八):spring整合mybatis

15 篇文章 0 订阅
11 篇文章 0 订阅

什么是MyBatis-Spring

MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。
使用MyBatis-Spring需要导入jia包

MyBatis-Spring使用文档:http://mybatis.org/spring/zh/index.html

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

要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类。

SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession

整合mybatis之前保证mybatis可以正常跑代码
1.导包
2.创建mybatis-config.xml文件
3.创建实体类(User)
4.编写mapper接口(UserMapper)
5.编写mapper.xml文件(UserMapper.xml)
6.测试(Test)

整合实现一

1、引入Spring配置文件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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">


</beans>

2、配置数据源(dataSource)

<!--DateSource:使用Spring的数据源代替Mybatis的配置  c3p0  dbcp  druid
    我们这里使用spring提供的JDBC:
    -->
    <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=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="140720"/>
    </bean>

3、配置SqlSessionFactory,关联MyBatis

<!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--关联Mybatis-->
        <!--configuration:Mybatis配置文件的地址-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--mapperLocations:注册映射器-->
        <property name="mapperLocations" value="classpath:com/Devin/mapper/*.xml"/>
    </bean>

4、注册sqlSessionTemplate,关联sqlSessionFactory

<!--SqlSessionTemplate:即使我们使用的sqlsession-->
    <!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 只能使用构造器注入:sqlsessionFactory,因为他没有set方法 -->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

5、创建一个applicationContext.xml,导入spring-dao.xml,配置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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <import resource="spring-dao.xml"/>

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

</beans>

6、增加Dao接口的实现类(UserMapperImpl);私有化sqlSessionTemplate

package com.Devin.mapper;

import com.Devin.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper {
/*
在原来所有操作都用sqlsession来执行,现在用sqlsessionTemplate来执行
 */
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public List<User> selectUser() {
        /*
        Mybatis测试类里的获取类文件
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
         */
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

7、注册bean实现

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

8、测试

import com.Devin.mapper.UserMapper;
import com.Devin.pojo.User;

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

public class MyTest {
    @Test
    public  void test() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }
}

整合实现二

mybatis-spring1.2.3版以上的才有这个 .

官方文档截图 :

dao继承Support类 , 直接利用 getSqlSession() 获得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不需要管理SqlSessionTemplate
在这里插入图片描述
1、再创建一个UserMapperImpl02,继承一个SqlSessionDaoSupport,实现UserMapper接口

package com.Devin.mapper;

import com.Devin.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl02 extends SqlSessionDaoSupport implements UserMapper {
    
    @Override
    public List<User> selectUser() {
        SqlSession sqlSession = getSqlSession();
        return sqlSession.getMapper(UserMapper.class).selectUser();
    }
}

2、编写bean的配置

<bean id="userMapper2" class="com.Devin.mapper.UserMapperImpl02">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

3、测试

 @Test
    public  void test2() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }

本文章来源于B站(狂神说) B站地址:https://space.bilibili.com/95256449

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值