Spring集成MyBatis


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

实现 Spring 与 MyBatis 的整合常用的方式:扫描的 Mapper 动态代理

一、建立数据库及新表

二、maven 依赖 pom.xml

  • spring 依赖
  • mybatis 依赖
  • mysql 驱动
  • spring 的事务依赖
  • mybatis 和 spring 集成的依赖

完整的 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.md</groupId>
<artifactId>06-spring-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<span class="hljs-comment">&lt;!-- 单元测试--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>4.11<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

<span class="hljs-comment">&lt;!--spring核心--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-context<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.2.5.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

<span class="hljs-comment">&lt;!--spring事务用到的--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-tx<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.2.5.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-jdbc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.2.5.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>


<span class="hljs-comment">&lt;!--mybatis的--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.mybatis<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.5.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

<span class="hljs-comment">&lt;!--mybatis和spring集成的--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.mybatis<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis-spring<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

<span class="hljs-comment">&lt;!--mysql驱动--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.1.9<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

<span class="hljs-comment">&lt;!--德鲁伊,数据库连接池--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>druid<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.1.12<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

</dependencies>

<build>

<span class="hljs-comment">&lt;!--目的是把src/main/java目录中的xml文件包含到输出结果中,也就是输出到classes目录中--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">resource</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">directory</span>&gt;</span>src/main/java<span class="hljs-tag">&lt;/<span class="hljs-name">directory</span>&gt;</span><span class="hljs-comment">&lt;!--所在的目录--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">includes</span>&gt;</span><span class="hljs-comment">&lt;!--包括目录下的.properties,.xml 文件都会扫描到--&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">include</span>&gt;</span>**/*.properties<span class="hljs-tag">&lt;/<span class="hljs-name">include</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">include</span>&gt;</span>**/*.xml<span class="hljs-tag">&lt;/<span class="hljs-name">include</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">includes</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">filtering</span>&gt;</span>false<span class="hljs-tag">&lt;/<span class="hljs-name">filtering</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">resource</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span>

</build>

</project>

三、创建实体类

package com.md.domain;

/**

  • @author MD

  • @create 2020-08-09 20:02
    */
    public class Student {
    // 属性名和列名一样
    private Integer id;

    private String name;

    private String email;

    private Integer age;

    public Student() {
    }

    public Student(Integer id, String name, String email, Integer age) {
    this.id = id;
    this.name = name;
    this.email = email;
    this.age = age;
    }

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public Integer getAge() {
    return age;
    }

    public void setAge(Integer age) {
    this.age = age;
    }

    @Override
    public String toString() {
    return "Student{" +
    "id=" + id +
    ", name='" + name + ''' +
    ", email='" + email + ''' +
    ", age=" + age +
    '}';
    }
    }

四、创建 dao 接口

package com.md.dao;

import com.md.domain.Student;

import java.util.List;

/**

  • @author MD

  • @create 2020-08-09 20:04
    */
    public interface StudentDao {
    int insertStudent(Student student);

    List<Student> selectStudents();
    }

五、定义映射文件 mapper

和 dao 在同一目录下,名字和 dao 接口的一样,StudentDao.xml

mapper 中的 namespace 取值也为 Dao 接口的全限定性名

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.md.dao.StudentDao">
<span class="hljs-tag">&lt;<span class="hljs-name">insert</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"insertStudent"</span>&gt;</span>
    insert into student values(#{id},#{name},#{email},#{age})
<span class="hljs-tag">&lt;/<span class="hljs-name">insert</span>&gt;</span>


<span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"selectStudents"</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">"com.md.domain.Student"</span>&gt;</span>

    select id,name,email,age from student

<span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span>

</mapper>

六、创建 mybatis 主配置文件

在 src /main/resources 下定义 MyBatis 的主配置文件,命名为 mybatis.xml

注意:

  1. 主配置文件中不再需要数据源的配置了。因为数据源要交给 Spring 容器来管理了
  2. 这里对 mapper 映射文件的注册,使用 <package/> 标签,即只需给出 mapper 映射文件所在的包即可。因为 mapper 的名称与 Dao 接口名相同,可以使用这种简单注册方式。这种方式的好处是,若有多个映射文件,这里的配置也是不用改变的。当然,也可使用原来的 <resource/> 标签方式
<?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>

<span class="hljs-comment">&lt;!--settings:控制mybatis全局行为--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">settings</span>&gt;</span>

    <span class="hljs-comment">&lt;!--设置mybatis输出日志--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">setting</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"logImpl"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"STDOUT_LOGGING"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">settings</span>&gt;</span>


<span class="hljs-comment">&lt;!--设置别名--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">typeAliases</span>&gt;</span>
    <span class="hljs-comment">&lt;!--
		package:把包下面的所有类名作为别名
		name:实体类所在的包名--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">package</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"com.md.domain"</span>/&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">typeAliases</span>&gt;</span>



<span class="hljs-comment">&lt;!-- sql映射文件的位置 --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">mappers</span>&gt;</span>

    <span class="hljs-comment">&lt;!--name是包名,这个包中所有mapper.xml一次加载--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">package</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"com.md.dao"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">mappers</span>&gt;</span>

</configuration>

七、创建 Service 接口和实现类,属性是 dao

package com.md.service;

import com.md.domain.Student;

import java.util.List;

/**

  • @author MD

  • @create 2020-08-09 20:25
    */
    public interface StudentService {

    int addStudent(Student student);

    List<Student> queryStudent();

}

//-------------------------------------
package com.md.service.impl;

import com.md.dao.StudentDao;
import com.md.domain.Student;
import com.md.service.StudentService;

import java.util.List;

/**

  • @author MD

  • @create 2020-08-09 20:27
    */
    public class StudentServiceImpl implements StudentService {

    // 引用类型
    private StudentDao studentDao;

    // 为了使用 set 注入来赋值
    public void setStudentDao(StudentDao studentDao) {
    this.studentDao = studentDao;
    }

    @Override
    public int addStudent(Student student) {
    int i = studentDao.insertStudent(student);
    return i;
    }

    @Override
    public List<Student> queryStudent() {
    List<Student> studentList = studentDao.selectStudents();

     <span class="hljs-keyword">return</span> studentList;
    

    }
    }

八、创建 spring 的配置文件

在 src /main/resources 下定义 Spring 的主配置文件,命名为 applicationContext.xml

声明 mybatis 的对象交给 spring 创建

1. 数据源 DateSource

使用 JDBC 模板,首先需要配置好数据源,数据源直接以 Bean 的形式配置在 Spring 配置文件中。根据数据源的不同,其配置方式不同:

这里使用的 Druid,Druid 是阿里的开源数据库连接池。是 Java 语言中最好的数据库连接池。Druid 能
够提供强大的监控和扩展功能

https://github.com/alibaba/druid

首先在 pom.xml 中加入依赖,上面添加过了

然后是在 spring 的配置文件中

<!-- 声明数据源 DataSource,作用是连接数据库 -->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <span class="hljs-comment">&lt;!--set注入提供连接数据库信息--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"url"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.url}"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.username}"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.password}"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxActive"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.maxActive}"</span> /&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

为了维护方法方便,还可以使用属性文件

在在 src /main/resources 下定义 jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=20

然后再 spring 的配置文件中

 <!--
        把数据库的配置信息写在一个独立的文件中,编译修改数据库的配置内容
        让 spring 知道 jdbc.properties 文件的位置
    -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
<span class="hljs-comment">&lt;!--声明数据源DataSource,作用是连接数据库--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"myDataSource"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.alibaba.druid.pool.DruidDataSource"</span> <span class="hljs-attr">init-method</span>=<span class="hljs-string">"init"</span> <span class="hljs-attr">destroy-method</span>=<span class="hljs-string">"close"</span>&gt;</span>

<!-- 使用属性配置文件中的数据,语法 ${key} -->
<property name="url" value="${jdbc.url}" /><!--setUrl()-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />

<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

这样就可以了

2. 注册 SqlSessionFactoryBean

 <!--SqlSessionFactory-->
    <!-- 声明的是 mybatis 中提供的 SqlSessionFactoryBean 类,这个类内部创建 SqlSessionFactory-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <span class="hljs-comment">&lt;!--set注入,把数据库连接池付给dataSource属性--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"myDataSource"</span>/&gt;</span>
    <span class="hljs-comment">&lt;!--mybatis主配置文件的位置
        configLocation属性是Resource类型,读取配置文件
        它的赋值使用的是value , 指定文件的路径,使用的是classpath:表示文件的位置
    --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"configLocation"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"classpath:mybatis.xml"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

3. 定义 Mapper 扫描配置器 MapperScannerConfigurer

也就是创建 dao 对象

    <!-- 创建 dao 对象
        使用 SqlSession 的 getMapper(StudentDao.class)
        MapperScannerConfigurer 在内部调用 getMapper() 生成每个 dao 接口的代理对象
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定的是 SqlSessionFactory 对象的 id-->
        <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
    <span class="hljs-comment">&lt;!--指定包名,包名是dao接口所在的包名
        MapperScannerConfigurer会扫描这个包中的所有接口,把每个接口都执行
        一次getMapper()方法,得到每个接口的dao对象
        创建好的dao对象放入到spring的容器中

        dao默认对象的名称:是接口名字的首字母小写
    --&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"basePackage"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"com.md.dao"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!--多个包--&gt;</span>
    <span class="hljs-comment">&lt;!--&lt;property name="basePackage" value="com.md.dao,com.md.dao2"/&gt;--&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

4. 向 Service 注入接口名

 <!-- 声明 service-->
    <bean id="studentService" class="com.md.service.impl.StudentServiceImpl">
    <span class="hljs-comment">&lt;!--就是上面通过创建的dao对象--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"studentDao"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"studentDao"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

总:

Spring的配置文件

用的时候直接复制

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<span class="hljs-comment">&lt;!--
    把数据库的配置信息写在一个独立的文件中,编译修改数据库的配置内容
    让spring知道jdbc.properties文件的位置
--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">context:property-placeholder</span> <span class="hljs-attr">location</span>=<span class="hljs-string">"classpath:jdbc.properties"</span>/&gt;</span>


<span class="hljs-comment">&lt;!--声明数据源DataSource,作用是连接数据库--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"myDataSource"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.alibaba.druid.pool.DruidDataSource"</span> <span class="hljs-attr">init-method</span>=<span class="hljs-string">"init"</span> <span class="hljs-attr">destroy-method</span>=<span class="hljs-string">"close"</span>&gt;</span>

    <span class="hljs-comment">&lt;!--set注入提供连接数据库信息--&gt;</span>
    <span class="hljs-comment">&lt;!--&lt;property name="url" value="jdbc:mysql://localhost:3306/ssm" /&gt;--&gt;</span>
    <span class="hljs-comment">&lt;!--&lt;property name="username" value="root" /&gt;--&gt;</span>
    <span class="hljs-comment">&lt;!--&lt;property name="password" value="123456" /&gt;--&gt;</span>
    <span class="hljs-comment">&lt;!--&lt;property name="maxActive" value="20" /&gt;--&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"url"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.url}"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.username}"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.password}"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxActive"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${jdbc.maxActive}"</span> /&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>



<span class="hljs-comment">&lt;!--SqlSessionFactory--&gt;</span>
<span class="hljs-comment">&lt;!--声明的是mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"SqlSessionFactory"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.mybatis.spring.SqlSessionFactoryBean"</span>&gt;</span>

    <span class="hljs-comment">&lt;!--set注入,把数据库连接池付给dataSource属性--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"myDataSource"</span>/&gt;</span>
    <span class="hljs-comment">&lt;!--mybatis主配置文件的位置
        configLocation属性是Resource类型,读取配置文件
        它的赋值使用的是value , 指定文件的路径,使用的是classpath:表示文件的位置
    --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"configLocation"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"classpath:mybatis.xml"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>



<span class="hljs-comment">&lt;!--创建 dao对象
    使用SqlSession的getMapper(StudentDao.class)
    MapperScannerConfigurer在内部调用getMapper()生成每个dao接口的代理对象
--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span>&gt;</span>
    <span class="hljs-comment">&lt;!--指定的是SqlSessionFactory对象的id--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"sqlSessionFactoryBeanName"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"SqlSessionFactory"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!--指定包名,包名是dao接口所在的包名
        MapperScannerConfigurer会扫描这个包中的所有接口,把每个接口都执行
        一次getMapper()方法,得到每个接口的dao对象
        创建好的dao对象放入到spring的容器中

        dao默认对象的名称:是接口名字的首字母小写
    --&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"basePackage"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"com.md.dao"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!--多个包--&gt;</span>
    <span class="hljs-comment">&lt;!--&lt;property name="basePackage" value="com.md.dao,com.md.dao2"/&gt;--&gt;</span>


<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>


<span class="hljs-comment">&lt;!--上面的这个是一个模板,只有最后dao对象的这个包名的value的值是根据自己创建写的--&gt;</span>




<span class="hljs-comment">&lt;!--下面的就是自己定义的service--&gt;</span>



<span class="hljs-comment">&lt;!--声明service--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"studentService"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.md.service.impl.StudentServiceImpl"</span>&gt;</span>

    <span class="hljs-comment">&lt;!--就是上面通过创建的dao对象--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"studentDao"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"studentDao"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

</beans>

九、创建测试类

 @Test
    public void test03(){
        ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 获取到了 spring 容器中的对象
        StudentService studentService = (StudentService) c.getBean("studentService");
        studentService.addStudent(new Student(1008,"冯宝宝","fb@qq.com",20));
    <span class="hljs-comment">// spring和mybatis整合在一起使用,事务是自动提交的</span>

    List&lt;Student&gt; studentList = studentService.queryStudent();
    studentList.forEach(stu-&gt; System.out.println(stu));

}

整体目录结构

十、总结

  1. 新建 maven 项目
  2. 加入 maven 依赖
    • spring 依赖
    • mybatis 依赖
    • mysql 驱动
    • spring 的事务依赖
    • mybatis 和 spring 集成的依赖
    • 德鲁伊,数据库连接池的依赖
  3. 创建实体类
  4. 创建 dao 接口和 mapper 文件
  5. 创建 mybatis 主配置文件
  6. 创建 Service 接口和实现类,属性是 dao
  7. 创建 spring 的配置文件:声明 mybatis 的对象交给 spring 创建
    • 数据源 DateSource
    • SqlSessionFactory
    • Dao 对象
    • 声明自定义的 service
  8. 创建测试类,获取 Service 对象,通过 service 调用 dao 完成数据库的访问