Spring整合Mybatis

导包

		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

整合方式一(需要注入SqlSessionTemplate)

在这里插入图片描述

添加spring-dao.xml配置文件(名字随意取)

<?xml version="1.0" encoding="UTF8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--DataSource:使用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="tl"/>
    </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/tl/dao/*.xml"/>
    </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <!--加入UserMapper接口的实现类-->
    <bean id="userMapperImpl" class="com.tl.dao.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
    <!--这里不需要加实体类,因为在UserMapper.xml映射器中Mybatis已经创建了该实体类对象-->
</beans>


Mybatis配置文件

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--写一些settings-->
</configuration>

映射器

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.UserMapper">
    <select id="getUserList" resultType="com.tl.pojo.User">
    select id,name,pwd
    from user

  </select>
</mapper>

添加一个接口实现类

package com.tl.dao;

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

import java.util.List;

/**
 * @author tl
 * Mybatis不能完全整合到Spring中,需要加一个接口的实现类,以便Spring代理
 */
public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSessionTemplate;

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

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

测试

package com.tl.test;

import com.tl.dao.UserMapper;
import com.tl.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author com.com.tl
 */
public class UserMapperTest {
    @Test
    public void Test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper bean = context.getBean(UserMapper.class);
        List<User> userList = bean.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
    }
}

在这里插入图片描述

整合方式二(无需注入SqlSessionTemplate)

接口实现类继承SqlSessionDaoSupport

package com.tl.dao;

import com.tl.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

/**
 * @author tl
 * Mybatis不能完全整合到Spring中,需要加一个接口的实现类,以便Spring代理
 */
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{


    public List<User> getUserList() {

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

配置(无需注入SqlSessionTemplate)

<?xml version="1.0" encoding="UTF8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--DataSource:使用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="tl"/>
    </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/tl/dao/*.xml"/>
    </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
<!--    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
<!--        &lt;!&ndash;只能使用构造器注入sqlSessionFactory,因为它没有set方法&ndash;&gt;-->
<!--        <constructor-arg index="0" ref="sqlSessionFactory" />-->
<!--    </bean>-->
    <!--加入UserMapper接口的实现类-->
<!--    <bean id="userMapperImpl" class="com.tl.dao.UserMapperImpl">-->
<!--        <property name="sqlSessionTemplate" ref="sqlSession"/>-->
<!--    </bean>-->
    <bean id="userMapperImpl" class="com.tl.dao.UserMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!--这里不需要加实体类,因为在UserMapper.xml映射器中Mybatis已经创建了该实体类对象-->
</beans>

其他都和方式一一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值