6 Spring和Mybaits的整合

6 Spring和Mybaits的整合

一,整合概述

将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring容器来管理,所以,该整合,只需要将SqlSessionFactory的对象生成器SqlSessionFactoryBean注册在Spring容器中,再将其注入给Dao的实现类即可完成整合。、

Mybatis开发过程出现的问题:

  • 配置实体别名时候繁琐
  • 注册Mybatis主配置文件繁琐
  • MybaitsAPI的调用繁琐,即使封装了一次也存在大量代码冗余

二,整合开发步骤

1.开发回顾

Mybatis 常规开发:

  1. 建立表
  2. 建立实体
  3. 创建DAO
  4. 实现对应DAOMapper文件
  5. 配置主配置文件

2.Mybaits-Spring整合开发步骤

  1. 建立表
  2. 建立实体
  3. 创建DAO
  4. 实现对应DAOMapper文件
  5. 配置Spring配置文件

3.环境配置

需要的额外的Jar包有(只是整合)

  • druid
  • spring-tx
  • mybatis-spring
  • spring-jdbc
  • mysql-connector-java
  • mybatis

4.整合编码

Mybatis-Spring.xml

连接池配置

指定阿里巴巴的Druid连接池

数据库验证信息字段是固定的

<bean id="dataSourse" class="com.alibaba.druid.pool.DruidDataSource" >
    <property name="username" value="root"/>
    <property name="url" value="jdbc:mysql://localhost:3306/jdbc_test?useSSL=false&amp;allowPublicKeyRetrieval=true&amp;serverTimezone=UTC"/>
    <property name="password" value="123456"/>
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
</bean>

sqlseesion工厂创建

类型别名使用typeAliases指定,日后使用实体直接使用类名

mapperLocations使用List集合,指定目录,会自动寻找,此处是src/Mapper目录下的的符合*Mapper.xml命名规范的所有文件

注:

​ org.Mybatis.StudentEntity是实体类

<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--指定数据源-->
    <property name="dataSource" ref="dataSourse"/>
    <!--类型别名,日后直接用类名充当-->
    <property name="typeAliases" value="org.Mybatis.StudentEntity"/>
    <!--指定Mapper位置-->
    <property name="mapperLocations" >
        <list>
            <value>
                <!--通配写法,按照这个命名规范书写-->
                classpath:Mapper/*Mapper.xml
            </value>
        </list>
    </property>
</bean>

创建DAO 对象

basePackage指定的是DAO类所在的包

在通过Spring工厂获取对象时使用的时接口名首字母小写,我们在接口命名时就需要约定接口名首字母大写

注:

​ org.Mybatis是DAO所在的包

<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--对应上面的SqlSessionFactoryBean的名字-->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    <!--MapperScannerConfigurer到DAO下的包自动的找到对应DAO接口来创建对象-->
    <!--注意!在通过Spring工厂获取对象时使用的时接口名首字母小写,我们在接口命名时就需要约定接口名首字母大写-->
    <property name="basePackage" value="org.Mybatis"/>
</bean>

调用

只需要new出工厂和实体类就可以对Mybtis操作

		ApplicationContext context=new ClassPathXmlApplicationContext("Mybatis-Spring.xml");
		StudentDAO studentDAO= (StudentDAO) context.getBean("studentDAO");
		StudentEntity studentModle = new StudentEntity();
		studentModle.setId(1);
       	studentModle.setEmail("123@before.com");
        studentModle.setAge(22);
        studentModle.setName("test");
		studentDAO.InsertStuinfo(studentModle);

对比原来直接使用mybatisapi确实少了不少代码、

完整代码:

<?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">
    <!--连接池配置-->
    <bean id="dataSourse" class="com.alibaba.druid.pool.DruidDataSource" >
        <property name="username" value="root"/>
        <property name="url" value="jdbc:mysql://localhost:3306/jdbc_test?useSSL=false&amp;allowPublicKeyRetrieval=true&amp;serverTimezone=UTC"/>
        <property name="password" value="123456"/>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    </bean>
    <!--sqlseesion工厂创建-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定数据源-->
        <property name="dataSource" ref="dataSourse"/>
        <!--类型别名,日后直接用类名充当-->
        <property name="typeAliases" value="org.Mybatis.StudentEntity"/>
        <property name="mapperLocations" >
            <list>
                <value>
                    <!--通配写法,按照这个命名规范书写-->
                    classpath:Mapper/*Mapper.xml
                </value>
            </list>
        </property>
    </bean>
    <!--创建DAO 对象-->
    <bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--对应上面的SqlSessionFactoryBean的名字-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
        <!--MapperScannerConfigurer到DAO下的包自动的找到对应DAO接口来创建对象-->
        <!--注意!在通过Spring工厂获取对象时使用的时接口名首字母小写,我们在接口命名时就需要约定接口名首字母大写-->
        <property name="basePackage" value="org.Mybatis"/>
    </bean>
</beans>
posted on 2021-06-11 14:14  NathenJames  阅读(70)  评论(0编辑  收藏  举报