【SpringBoot】SpringBoot集成mybatis

快速开始

  1. 引入POM

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.3</version>
            </dependency>
    

    如果使用的是mysql数据库,还要引入一下pom

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
  2. 配置文件

    #使用的数据库驱动
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #数据库连接信息
    spring.datasource.url=jdbc:mysql://localhost:3306/dbname?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=12341234112314
    
    #配置自定义pojo时扫描
    mybatis.type-aliases-package=com.example.template.entity
    mybatis.mapper-locations=classpath:mapper/*.xml
    
  3. mapper.xml

    <?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.example.template.dao.StudentMapper">
        <insert id="insertStudent" parameterType="com.example.template.entity.Student">
            insert into student (id,st_name,age,grade,class_id) values
            (#{id},#{stName},#{age},#{grade},#{classId})
        </insert>
    
        <delete id="deleteStudentById" parameterType="com.example.template.entity.Student">
            delete from student t where t.id = #{id}
        </delete>
    
        <update id="updateStudentById" parameterType="com.example.template.entity.Student">
            UPDATE student t
            SET t.st_name =#{stName},t.age =#{age},t.class_id = #{classId},t.grade =#{grade}
            WHERE
                t.id =#{id}
        </update>
    
        <select id="queryStudentById" resultType="com.example.template.entity.Student">
            SELECT t.id,t.st_name as stName,t.age,t.grade,t.class_id as classId from student t where t.id = #{id}
        </select>
    </mapper>
    
  4. mapper

    @Component
    @Mapper
    public interface StudentMapper {
        void insertStudent(Student student);
        void deleteStudentById(Student student);
        void updateStudentById(Student student);
        Student queryStudentById(Student student);
    }
    

其他情况补充

  1. 批量插入多条数据

        <insert id="insertBatchStudent" parameterType="com.example.demo_mybatis.entity.Student">
            insert into student (id,st_name,age,grade,class_id) values
            <foreach item="student" collection="list" separator=",">
                (#{student.id},#{student.stName},#{student.age},#{student.grade},#{student.classId})
            </foreach>
        </insert>
    
    void insertBatchStudent(@Param("list") List<Student> list);
    

    这里虽然传入的是一个list,但是parameterType要指定的是list里的具体对象,而不是list。

    • item:集合中元素迭代时的别名,该参数为必选。
    • index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选
    • open:foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选
    • separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
    • close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
    • collection: 要做foreach的对象,作为入参时,List对象默认用"list"代替作为键,数组对象有"array"代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids".如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"

    在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

    • 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .、
    • 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
    • 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.

    针对最后一条,我们来看一下官方说法:

    注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。
    

​ 所以,不管是多参数还是单参数的list,array类型,都可以封装为map进行传递。如果传递的是一个List,则mybatis会封装为一 个list 为key,list值为object的map,如果是array,则封装成一个array为key,array的值为object的map,如果自己封装呢,则 colloection 里放的是自己封装的map里的key值。


mybatis关于mapper.xml怎么配中文文档:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html

原文地址:https://www.cnblogs.com/pandaNHF/p/15701250.html