Spring-Mybatis结合

88 阅读2分钟

入门

  • 安装和配置 MyBatis-Spring

  • 安装 导入依赖

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.4</version>
</dependency>

配置pom.xml,以便读取资源文件

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

使用方式一

步骤:

  1. 编写数据源配置【写好几乎不用更改】
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 需要给接口加实现类【比MyBatis多这一步】
  5. 将自己写的实现类,注入到Spring中 样例
    步骤1、2、3、5用spring.xml配置完成
<?xml version="1.0" encoding="GBK"?>
<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">

    <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?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true&amp;serverTimezone=Asia/Shanghai"/>
           <property name="username" value="root"/>
           <property name="password" value="123456"/>
    </bean>

    <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/mybatis/DAO/*.xml"/>
    </bean>
           <!--引用要要用ref-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

    <bean id="peopleMapper" class="com.mybatis.DAO.PeopleMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

</beans>

image.png 遇到的问题

image.png

image.png 解决方法
(1)因为注释有中文,将编码改成GBK
(2)参数是类不能用value,将其改为ref

步骤4

接口

public interface PeopleMapper {
    List<People> getPeopleList();
}

Mapper.xml

<mapper namespace="com.mybatis.DAO.PeopleMapper">
    <select id="getPeopleList" resultType="com.mybatis.pojo.People">
        select * from mybatis.people;
    </select>
</mapper>

实现接口

public class PeopleMapperImpl implements PeopleMapper{
    private SqlSessionTemplate sqlSession;
    //我们的所有操作,都使用sqLSession来执行,在原来,现在都使川sqLSessionTemplate;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
}
   @Override
    public List<People> getPeopleList() {
    PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class);
    return mapper.getPeopleList();
    }
}

属性SqlSessionTemplate不可少,bean的注入还要有setter方法 test

public class MyTest {
    @Test
    public void print() {
        ApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");
       PeopleMapperImpl peopleMapper=context.getBean("peopleMapper", PeopleMapperImpl.class);
        List<People> peopleList = peopleMapper.getPeopleList();
        for (People p: peopleList) {
            System.out.println(p);
        }
    }
}

image.png 小结(单独使用MyBatis和使用MyBatis-Spring的区别)

  • 在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession。
  • 在基础的 MyBatis 用法中,是通过 SqlSessionFactoryBuilder 来创建 SqlSessionFactory 的。而在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来创建。
  • SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession。 SqlSessionTemplate 是线程安全的,可以被多个 DAO 或映射器所共享使用。
  • 使用 SqlSessionFactory 作为构造方法的参数来创建 SqlSessionTemplate 对象

方式二(SqlSessionDaoSupport)

  • SqlSessionDaoSupport是一个抽象的支持类,用来为你提供 SqlSession。调用 getSqlSession() 方法你会得到一个 SqlSessionTemplate,之后可以用于执行 SQL 方法
  • SqlSessionDaoSupport 需要通过属性设置一个 sqlSessionFactorySqlSessionTemplate。如果两个属性都被设置了,那么 SqlSessionFactory 将被忽略

样例
在方式一的基础上,更改实现类

public class PeopleMapperImpl2 extends SqlSessionDaoSupport implements PeopleMapper {
    @Override
    public List<People> getPeopleList() {
        return getSqlSession().getMapper(PeopleMapper.class).getPeopleList();
    }
}

spring注入

<bean id="peopleMapper2" class="com.mybatis.DAO.PeopleMapperImpl2">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>

image.png