详解:Mybatis参数获取和动态SQL以及分页功能

前置准备

项目结构

在这里插入图片描述

在pom文件导入依赖

 <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectLombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>


        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.4</version>
        </dependency>
    </dependencies> <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectLombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>


        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.4</version>
        </dependency>
    </dependencies>

创建properties配置文件

创建配置文件,添加数据库运行所需的配置信息,方便给mybatis配置文件赋值

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hqyj03?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimeZone=Asia/Shanghai
password=yjg
username=root

创建Mapper接口

public interface CourseMapper和public interface StudentMapper

创建Mapper映射文件

CourseMapper.xml和StudentMapper.xml

SqlSession对象

创建在测试方法之前生成SqlSession对象

@Before
    public void test1() throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        //SqlSessionFactoryBuilder 是 MyBatis 提供的用于构建 SqlSessionFactory 的建造者类。
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //SqlSessionFactory 是一个线程安全的工厂类,用于创建 SqlSession 实例
        SqlSessionFactory build = builder.build(inputStream);
        //SqlSession 是 MyBatis 提供的核心接口之一,它是用于与数据库进行交互的主要对象。
        //简单来说,SqlSession 提供了执行 SQL 操作、提交事务、获取映射器(Mapper)等功能。
       sqlSession = build.openSession(true);
    }

创建对应的表在数据库中

CREATE TABLE `student` (
  `stu_id` int(11) NOT NULL AUTO_INCREMENT,
  `stu_name` varchar(255) DEFAULT NULL,
  `stu_age` varchar(255) DEFAULT NULL,
  `stu_salary` decimal(10,2) DEFAULT NULL,
  `stu_birth` date DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `course_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
CREATE TABLE `course` (
  `course_id` int(11) NOT NULL AUTO_INCREMENT,
  `course_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`course_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

实体类

import lombok.Data;
import java.util.List;
@Data
public class Course {
    private Integer courseId;
    private String   courseName;
    private List<Student> students;//表示一个课程下有多个学生一对多
}
import lombok.Data;
import org.apache.ibatis.type.Alias;
import java.util.Date;
@Data
//@Alias("Stu")
public class Student {
    private Integer stuId;
    private String  stuName;
    private Integer stuAge;
    private Double  stuSalary;
    private Date  stuBirth;
    private Date createTime;
    private Integer courseId;
}

SQL语句中的参数获取

单个参数

Mapper 接口

//通过id查询学生,传递单个参数
Student queryStudentById(Integer id);

映射文件语句

<!--    拿到方法的形参,单个参数-->
    <select id="queryStudentById" resultType="Student">
          select * from student where stu_id=#{id}
    </select>

测试方法

@Test
    public void test4(){
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.queryStudentById(2);
        System.out.println(student);
    }

使用#{名称}来接收接口方法中的参数值,使用占位符’?'来设置值。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VcBGRTkt-1692499903936)(D:\MoreFile\Hqing\SSM\Note\day2\image-20230809173019421.png)]

两个参数

Mapper 接口

Student queryStudentByNameAndAge(@Param("name") String name,@Param("age") int age);

映射文件语句

使用的是${}取值的方式,name值拼接了两个单引号

<select id="queryStudentByNameAndAge" resultType="student">
      select * from student where stu_name='${name}' and stu_age=${age};
</select>

测试方法

@Test
public void test5(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student = mapper.queryStudentByNameAndAge("kun", 18);
    System.out.println(student);
}

也可以使用${名称}来接收,但是使用该方式是通过字符串拼接方式设值,它是直接插入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xxoSx1PT-1692499903937)(D:\MoreFile\Hqing\SSM\Note\day2\image-20230809173158529.png)]

比较

${} 用于表示直接替换参数的值,而 #{} 则用于进行参数的安全处理和预编译。在映射文件中使用 ${} 会将参数的值直接替换进 SQL 语句中,这样可能存在 SQL 注入的风险。因此,推荐使用 #{} 做为参数占位符。

参数找不到的情况

接口中不设置别名的话

Student queryStudentByNameAndAge(String name,int age);

映射文件语句

<select id="queryStudentByNameAndAge" resultType="student">
      select * from student where stu_name='${name}' and stu_age=${age};
</select>

运行测试方法就会报错

Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]

根据错误信息,在查询数据库时出现了参数找不到的错误。错误消息中提到可用的参数是 [arg1, arg0, param1, param2],而没有找到参数 name

原因

在获取方法上的参数时,会将参数值存入到一个map中,key名为arg1或者param1表示第一个参数,以此类推表示第二个,第三个。。。。因此会通过key名来获取参数值,map中没有name这个key。只能通过arg或者param的形式来获取。

语句的配置如下即可,也可以换成param1.。。。,当然也可以给参数设置别名。

<select id="queryStudentByNameAndAge" resultType="student">
      select * from student where stu_name='${arg0}' and stu_age=${arg1};
</select>

单个参数(类型为实体类)

Mapper 接口

 //插入数据,参数为实体类型
 int insertStu(Student stu);

映射文件语句

<insert id="insertStu">
    insert into student values (null,#{stuName},#{stuAge},#{stuSalary},#{stuBirth},now())
</insert>

:MyBatis会自动根据Mapper接口中定义的方法和映射文件中的SQL语句,获取实体类的属性并进行映射。

实体类的属性名称需要与映射文件中SQL语句中的占位符一致才能正确映射。同时,映射文件中的SQL语句也需要与Mapper接口中的方法名称一致,才能正确执行对应的SQL操作。

测试方法

@Test
public void test2(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student=new Student(null,"pp",45,555.55,new Date(),null);
    int i = mapper.insertStu(student);
    System.out.println(i);
}

多个参数(含实体类)

Mapper 接口

//参数多种类型,指定数量查询
List<Student> queryStuByLimit(@Param("stu") Student student,@Param("count") Integer Count);

映射文件语句

<select id="queryStuByLimit" resultType="Student">
    select * from student where stu_age=#{stu.stuAge} limit #{count};
</select>

测试方法

@Test
public void test6(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
   Student s1=new Student();
   s1.setStuAge(18);
    List<Student> list = mapper.queryStuByLimit(s1, 2);
    list.forEach(System.out::println);
}

添加数据时获取自增id值

在添加数据时,由于主键设置为自增的,会自动生成值,所以在插入新的数据时不需要设置id值。插入数据后如何获取到自动生成主键值

mapper

  • useGeneratedKeys:是一个插入语句的属性,用于指定是否使用数据库自动生成的主键。
  • keyProperty:是一个映射对象的属性,用于指定将生成的主键值赋给哪个对象的属性。

Mapper 接口

int insertStu(Student stu);

映射文件语句

<insert id="insertStu" useGeneratedKeys="true" keyProperty="stuId">
    insert into student values (null,#{stuName},#{stuAge},#{stuSalary},#{stuBirth},now())
</insert>

测试方法

@Test
public void test(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student ss=new Student();
    System.out.println(ss.getStuId());//null
    ss.setStuName("kun");
    ss.setStuAge(20);
    ss.setStuSalary(4563.2);
    ss.setStuBirth(new Date());
    ss.setCourseId(2);
    Integer sss = mapper.insertStu(ss);
    System.out.println(ss.getStuId());//执行插入过后获取到了数据自增的id值
    System.out.println(sss);
}

模糊查询

模糊查询语法:like %值%。因此在xml中编写sql时会进行字符串拼接。可能有时候会导致拼接不成功。因此建议使用concat函数进行%拼接。

CONCAT 函数

SQL 的 CONCAT 函数用于将两个或多个字符串连接在一起。它接受任意数量的参数,并按照参数的顺序将它们连接在一起。

使用 CONCAT 函数的一般语法如下:

CONCAT(string1, string2, ...)

其中,string1string2表示要连接的字符串,可以是列名、常量值或表达式。

Mapper 接口

//模糊查询
List<Student> queryStuByNameLike(String name);

映射文件语句

<select id="queryStuByNameLike" resultType="student">
    select * from student where stu_name like concat('%',#{name},'%');
</select>

测试方法

@Test
public void test7(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    List<Student> nameLike = mapper.queryStuByNameLike("k");
    nameLike.forEach(System.out::println);
}

自定义结果映射集

为什么要使用自定义结果映射集?

当使用实体类来做为返回值类型的时候,会将数据库字段和实体类中的属性进行一一映射。

一张表对应一个实体类,如果此时进行多表查询,那么查询到的记录是包含多张表的记录,而

将一个实体类做为返回值接收是无法获取到多表查询到的所有值的。因此需要手动定义结果

集,来接收来自数据库中多表查询到的值。

多表查询涉及到以下关系:一对一关系,一对多关系,多对多关系

会使用到的元素

使用 <resultMap> 元素:通过定义 <resultMap> 元素,你可以指定如何映射查询结果到自定义的对象。在 <resultMap> 中,你可以使用 <id><result><association><collection> 等元素来描述对象的属性和关联关系。

  • <id> 元素用于指定主键属性的映射。你可以使用它来指定哪个属性对应数据库表的主键列。

  • <result> 元素用于描述普通属性的映射。通过 <result> 元素,你可以将查询结果中的某一列或表达式的结果映射到自定义对象的一个属性上。

  • <association> 元素用于描述对象之间的一对一关联关系。如果你的自定义对象中包含了其他对象作为属性,你可以使用 <association> 元素来定义该关联关系。

  • <collection> 元素用于描述对象之间的一对多关联关系。如果你的自定义对象中包含了一个集合类型的属性,表示与其他对象之间存在一对多的关系,你可以使用 <collection> 元素来定义该关联关系。

  • column 表示数据库表中的列名。通过在 <result><id> 元素中指定 column 属性,你可以告诉 MyBatis 将查询结果中的某个列映射到对象的相应属性上。例如,假设数据库表中有一个列名为 stu_name,你可以通过 column="stu_name" 将其映射到自定义对象的某个属性上。

  • type 表示 Java 类型或 JDBC 类型。在 <result><id> 元素中,你可以使用 type 属性来指定属性的类型或 JDBC 类型。这对于 MyBatis 在处理类型转换时非常有用。例如,假设你的查询结果中的某列是字符串类型,但你想将其映射到自定义对象的 Integer 类型属性上,你可以通过 type="java.lang.Integer" 来指定属性的类型。

  • property 表示对象中的属性名。在 <result><id> 元素中,你可以使用 property 属性来指定查询结果映射到自定义对象的哪个属性上。例如,假设你的自定义对象有一个属性名为 name,你可以通过 property="name" 来指定将查询结果映射到该属性上。

  • javaType 是用来指定属性的 Java 类型的属性。javaType 属性用于告诉 MyBatis 自定义对象中的某个属性的类型是什么。它通常在 <result><id> 元素中使用。

实体类

两个实体类,一个Student,一个Course,两个的属性都写好,当使用数据库查询给对象属性赋值时,如果没有那么对象的属性是null

@Data
public class Student {
    private Integer stuId;
    private String  stuName;
    private Integer stuAge;
    private Double  stuSalary;
    private Date  stuBirth;
    private Date createTime;
    private Integer courseId;
    private Course course;///表示一个学生只有一门课程,多个就要用LIst
    public Student() {}
    public Student(Integer stuId, String stuName, Integer stuAge) {
        this.stuId = stuId;
        this.stuName = stuName;
        this.stuAge = stuAge;
    }
}
@Data
public class Course {
    private Integer courseId;
    private String   courseName;
    private List<Student> students;//表示一个课程下有多个学生一对多
}

一对一

Mapper 接口

//查询学生以及课程信息
List<Student> queryStuAllInfo();

映射文件语句

第一种:使用级联关系

<resultMap id="stuInfoMap1" type="student">
       <!--设置主键映射-->
        <id column="stu_id" property="stuId"></id>
        <result column="stu_name" property="stuName"></result>
        <result column="stu_age" property="stuAge"></result>
        <result column="stu_salary" property="stuSalary"></result>
        <result column="stu_birth" property="stuBirth"></result>
        <result column="create_time" property="createTime"></result>
        <result column="course_id" property="courseId"></result>
        
         <!--设置course表中信息属性肤射-->
        <result column="course_id" property="course.courseId"></result>
        <result column="course_name" property="course.courseName"></result>
    </resultMap>

这段代码定义了一个名为 stuInfoMap1<resultMap>,用于将查询结果映射到 student 对象及其关联的 Course 对象的属性上。其中包含了主键属性、非主键属性以及关联对象的映射关系。

Student 类中的 setCourse 方法(因为引入了lombok依赖)会被 MyBatis 自动调用,传入一个 Course 对象,该对象包含从数据库查询结果中映射得到的 courseIdcourseName 值。然后,setCourse 方法将这些值设置到 Student 对象的关联属性中

第二种:使用association标签自定义引用数据类型映射关系

<resultMap id="stuInfoMap2" type="Student">
    <id column="stu_id" property="stuId"></id>
    <id column="stu_name" property="stuName"></id>
    <id column="stu_age" property="stuAge"></id>

    <association property="course" javaType="Course">
        <id column="course_id" property="courseId"></id>
        <id column="course_name" property="courseName"></id>
    </association>
</resultMap>

<id> 元素特点<id> 元素用于指定主键属性的映射,但同样也可以用于映射普通属性。实际上,<id> 元素是 <result> 元素的一种特殊形式。

stuInfoMap1 一样也是使用了Student 类中的 setCourse 方法,设置值。

第三种:分步查询,查询到学生的信息后,再根据学生的课程id,查询课程信息

<resultMap id="stuInfoMap3" type="student">
<id column="stu_id" property="stuId"></id>
<result column="stu_name" property="stuName"></result>
<result column="stu_age" property="stuAge"></result>
 <association property="course"
 select="com.yjg.mybatis.mapper.CourseMapper.queryCourseById" column="course_id">
        <id column="course_id" property="courseId"></id>
        <result column="course_name" property="courseName"></result>
    </association>
</resultMap>

分析

<association> 元素的 select 属性用于指定一个查询语句的唯一标识符,该语句将在查询主实体时同时加载关联实体。这个查询语句可以是一个独立的 SQL 语句或者引用其它已定义的 <select> 元素。

使用 <association> 元素设置了 property="course",表示关联属性的名称为 course。通过 select 属性指定了查询关联对象 Course 的 SQL 语句的命名空间和方法,这里是 com.yjg.mybatis.mapper.CourseMapper.queryCourseById。通过 column 属性指定了与 student 对象关联的列名 course_id。MyBatis 可以根据 course_id 的值,去查询并获取对应的课程信息,并将其映射到 Course 对象的属性上。然后赋值给主实体对象 Student 的关联属性 course

映射文件语句

<!--    resultMap:指定自定义映射结果集,可以改成stuInfoMap1,stuInfoMap3-->
<select id="queryStuAllInfo" resultMap="stuInfoMap2">
    SELECT s.*,c.course_name from student s left join course c on s.course_id=c.course_id;
</select>

分析

  • <select> 标签表示这是一个查询语句。
  • id="queryStuAllInfo" 表示这个查询语句的唯一标识符是 “queryStuAllInfo”,可以通过这个标识符在代码中进行调用和引用。
  • resultMap="stuInfoMap2" 表示查询结果将使用名为 “stuInfoMap2” 的 ResultMap 进行映射。ResultMap 是一个 MyBatis 的配置,用于将数据库查询结果映射到 Java 对象上。

测试方法

@Test
public void test9(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
    List<Student> list = mapper.queryStuAllInfo();
    list.forEach(System.out::println);
    }

StudentMapper 对应的映射文件中采用stuInfoMap3自定义映射结果集时,有一部分内容涉及到了关联属性 course 的查询。是通过调用 CourseMapper 中的方法来查询关联的 Course 对象。所以需要CourseMapper 接口的实例对象。

第四种:通过set方法暴力传入

以下是暴力传入

映射文件语句

StudentMapper.xml中的语句

    <select id="queryStudents" resultType="Student">
              select * from student
    </select>

CourseMapper.xml中的语句

<select id="queryCourseById" resultType="com.yjg.mybatis.pojo.Course">
       select * from course where course_id=#{courseId}
</select>

测试方法

  @Test
    public void test9(){
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
       
        List<Student> list = mapper.queryStudents();
         //这时候Course对象是null   下面是暴力传入Course对象
        for (int i = 0; i < list.size(); i++) {
            //获取课程id
            Integer courseId = list.get(i).getCourseId();
            //使用courseId查询course信息
            Course course = courseMapper.queryCourseById(courseId);
            //将查询到的course设置到每个student对象
            list.get(i).setCourse(course);
        }
        list.forEach(System.out::println);

    }

一对多

查询每门课程下的有哪些学生

Mapper 接口

//查询课程以及课程包含的学生
List<Course> queryCourse();

自定义结果映射集

collection属性介绍

在 MyBatis 中,“collection” 属性通常用于与 ResultMap 关联使用:

  1. collection: “collection” 属性用于指定子对象集合的字段名或属性名。它指定了查找子对象集合的方式,可以是一个字段名称或对象属性的名称。
  2. ofType(可选): “ofType” 属性用于指定子对象集合的类型。它可以是一个具体的 Java 类型,用于表示子对象的类型。如果未指定 “ofType” 属性,则 MyBatis 会根据返回的结果集自动推断集合的类型。
  3. column(可选): “column” 属性用于指定子对象集合在数据库表中的列名。它用于在查询结果集中定位子对象集合的数据。如果未指定 “column” 属性,则 MyBatis 会根据属性名自动寻找对应的列。
  4. select(可选): “select” 属性用于指定查询子对象集合的 SQL 语句或映射语句的 ID。它定义了从数据库查询子对象集合的逻辑。如果未指定 “select” 属性,则 MyBatis 会根据父对象的属性自动生成查询语句。
  5. resultMap(可选): “resultMap” 属性用于指定子对象集合的 ResultMap ID。它定义了如何将查询结果映射到子对象集合中的对象。如果未指定 “resultMap” 属性,则 MyBatis 会自动寻找与子对象集合类型对应的 ResultMap。

第一种:使用collction标签来处理集合映射

     <!--    自定义结果映射集-->
    <resultMap id="courseMap1" type="course">
        <id column="course_id" property="courseId"></id>
        <result column="course_name" property="courseName"></result>
           <!--ofType:指定该集命存储的是什么类型-->
        <collection property="students" ofType="student">
            <!--设置student表信息属性映射-->
            <id column="stu_id" property="stuId"></id>
            <!--设置其他值映射-->
            <result column="stu_name" property="stuName"></result>
            <result column="stu_age" property="stuAge"></result>
            <result column="stu_salary" property="stuSalary"></result>
            <result column="stu_birth" property="stuBirth"></result>
            <result column="create_time" property="createTime"></result>
            <result column="course_id" property="courseId"></result>
        </collection>
    </resultMap>

第二种:使用分步查询

StudentMapper.xml表中语句配置

<select id="queryStudentByCourseId" resultType="com.yjg.mybatis.pojo.Student">
         select * from student where course_id=#{courseId}
</select>

CourseMapper.xml中语句配置

<resultMap id="courseMap2" type="course">
    <id column="course_id" property="courseId"></id>
    <result column="course_name" property="courseName"></result>

    <collection property="students"
                select="com.yjg.mybatis.mapper.StudentMapper.queryStudentByCourseId" column="course_id">
        <!--设置student表信息属性映射-->
        <id column="stu_id" property="stuId"></id>
        <!--设置其他值映射-->
        <result column="stu_name" property="stuName"></result>
        <result column="stu_age" property="stuAge"></result>
        <result column="stu_salary" property="stuSalary"></result>
        <result column="stu_birth" property="stuBirth"></result>
        <result column="create_time" property="createTime"></result>
        <result column="course_id" property="courseId"></result>
    </collection>
</resultMap>

映射文件语句

<select id="queryCourse" resultMap="courseMap2">
    SELECT * from course c INNER JOIN
     student s on c.course_id=s.course_id
</select>

测试方法

@Test
public void test10(){
    CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
    List<Course> list = courseMapper.queryCourse();
    list.forEach(System.out::println);
}

其他方式

当自定义对象具有带参数的构造函数或工厂方法时,MyBatis 允许在 SQL 查询语句中直接使用它们来创建对象。这使得你可以通过 SQL 查询结果将数据映射到自定义对象的属性上。

假设有一个自定义对象 Student,它有一个带参数的构造函数:

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 其他属性和方法
    // ...
}

现在,可以在 MyBatis 的查询语句中使用这个构造函数来创建 Student 对象并将查询结果映射到它的属性上。以下是一个示例:

<select id="getStudent" resultType="Student">
    SELECT name, age FROM student WHERE id = #{id}
</select>

在上面的示例中,将查询结果映射到 Student 类型的对象上。MyBatis 会查找 Student 类中的带有相应参数的构造函数,并使用该构造函数创建对象实例。然后,将查询结果中的列值分别赋值给构造函数参数所对应的属性。

同样地,如果自定义对象有一个带参数的工厂方法,你也可以在 SQL 查询语句中使用工厂方法来创建对象。以下是一个示例:

public class Student {
    private String name;
    private int age;

    public static Student create(String name, int age) {
        return new Student(name, age);
    }

    // 其他属性和方法
    // ...
}
<select id="getStudent" resultType="Student">
    SELECT name, age FROM student WHERE id = #{id}
</select>

在上面的示例中,MyBatis 会查找 Student 类中的名为 create 的静态工厂方法,并使用该方法创建对象实例。然后,将查询结果中的列值作为参数传递给工厂方法,并将返回的对象实例作为最终的映射结果。

通过使用带参数的构造函数或工厂方法,可以自定义对象的创建过程,并且能够更灵活地与数据库中的数据进行映射。

动态SQL

概述

动态 SQL 是 MyBatis 的强大特性之一。在 JDBC 或其它类似的框架中,开发人员通常

需要手动拼接SQL 语句。根据不同的条件拼接 SQL 语句是一件极其痛苦的工作。动态 SQL 大

大减少了编写代码的工作量,更体现了 MyBatis 的灵活性、高度可配置性和可维护性。

MyBatis 也可以在注解中配置 SQL,但是由于注解功能受限,且对于复杂的 SQL 语句来说可

读性差,所以使用较少。

动态SQL元素

在这里插入图片描述

IF标签

通过条件查询学生信息时,可以通过学生的用户名进行查询,也可以通过年龄进行查询(where age=?),也可以同时使用名字和年龄进行查询(where name=? and age=?)。按照以上的条件,需要写三个不同的SQL操作。实质上,三个SQL的区别在于条件查询的字段不同,因此可以使用动态SQL来动态生成不同查询条件的SQL语句。

格式

<if test="判断条件">
<!--条件为true时执行SQL语句-->
SQL语句
</if>

代码

Mapper 接口

//根据学生属性查询学生
List<Student> queryStudentByParams(Student student);

映射文件语句

用法

<where>:是 MyBatis 中用于构建动态条件的元素,在这里作为条件容器。
<if test="stuName!=null and stuId!=null">:如果传入的参数 stuName 和 stuId 均不为 null,则生成对应的条件语句,并且使用 AND 连接符拼接条件。
<if test="stuId!=null">:如果传入的参数 stuId 不为 null,则生成对应的条件语句,并且使用 AND 连接符拼接条件。
<if test="stuName!=null">:如果传入的参数 stuName 不为 null,则生成对应的条件语句,并且使用 AND 连接符拼接条件。
<select id="queryStudentByParams" resultType="student">
    select * from student
    <where>
        <if test="stuName!=null and stuId!=null">
            and  stu_name=#{stuName} and stu_id=#{stuId}
        </if>
        <if test="stuId!=null">
            and stu_id=#{stuId}
        </if>
        <if test="stuName!=null">
            and  stu_name=#{stuName}
        </if>
    </where>
</select>

测试方法

   @Test
    public void test11(){
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student stu=new Student();
        stu.setStuName("kun");
        stu.setStuId(2);
        List<Student> list = mapper.queryStudentByParams(stu);
        list.forEach(System.out::println);
    }

choose,when,otherwise标签

概述

choose 标签按顺序判断其内部 when 标签中的判断条件是否成立,如果有一个成立,则执行相应的 SQL 语句,choose 执行结束;

如果都不成立,执行 otherwise 中的 SQL 语句。这类似于 Java 的switch 语句,choose为switch,when 为case,otherwise则为default。

格式

<choose>
<when test="判断条件1">
SQL语句1
</when >
<when test="判断条件2">
SQL语句2
</when >
<when test="判断条件3">
SQL语句3
</when >
<otherwise>
SQL语句4
</otherwise>
</choose>

代码

Mapper 接口

//根据学生属性查询学生2
List<Student> queryStudentByParams2(Student student);

映射文件语句

<select id="queryStudentByParams2" resultType="student">
    select * from student
   <where>
       <choose>
           <when test="stuId!=null">
               and stu_id=#{stuId}
           </when>

           <when test="stuName!=null   and stuName!=''">
               and stu_name=#{stuName}
           </when>

           <otherwise>
               and stu_age=#{stuAge}
           </otherwise>
       </choose>
   </where>
</select>

测试方法

@Test
public void test12(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student stu=new Student();

    stu.setStuName("kun");
    List<Student> list = mapper.queryStudentByParams2(stu);
    list.forEach(System.out::println);
}

如果不设置参数值,打印结果是SQL中执行拼接的是otherwise中的SQL语句

where标签

select * from student where 1=1
    <if test="stuName!=null and stuId!=null">
        and  stu_name=#{stuName} and stu_id=#{stuId}
    </if>
    

如果把1=1这个条件去除后,则会拼接成一个错误格式的SQL,SQL中的语句缺少where关键字,如果不写1=1,就还要考虑and的位置。

所以有,where标签在不用写where 1=1这个条件的情况下,拼接条件查询语句时,自动添加where关键字,保证sql格式正确。

<where> 元素可以用于将多个条件语句组合成一个完整的 WHERE 子句。它提供了一种简化 SQL 编写的方式,并且可以动态地根据条件判断生成有效的 WHERE 子句。

<where> 标签会自动处理条件语句之间的逻辑关系,它会移除开头的 AND 或 OR,以免在没有条件的情况下生成无效的语句。

Set标签

MyBatis 的 <set> 标签是一种用于动态生成更新语句中 SET 子句的元素。

在使用 MyBatis 更新记录时,经常需要根据传入的参数来决定更新哪些字段、更新的值是什么。<set> 标签就提供了这样的功能,可以根据条件动态地生成 SET 子句。

格式

<set>
<if test="条件">
字段=更新值
</if>
<if test="条件">
字段=更新值
</if>
<if test="条件">
字段=更新值
</if>
</set>

代码

Mapper 接口

Integer UpdateStudent(Student student);

映射文件语句

<update id="UpdateStudent">
    update student
    <set>
    <if test="stuName != null">stu_name=#{stuName},</if>
    <if test="stuAge != null">stu_age=#{stuAge},</if>
</set>
where stu_id=#{stuId}
</update>

测试方法

@Test
public void test20(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student stu=new Student();

    stu.setStuId(2);
    stu.setStuAge(666);
    stu.setStuName("kun");
    System.out.println(mapper.UpdateStudent(stu));
}

foreach标签

概述

相当于for循环语句,能够在SQL中循环遍历出参数为List或者Set等

集合中的参数值

使用场景:一次性添加/删除多个记录

语法

<foreach item="item" index="index"
collection="list|array|map key" open="("separator="," close=")">
参数值
</foreach>

collection 属性有以下三个取值collection 属性有以下三个取值

  1. list:表示接受一个 List 类型的参数。
  2. array:表示接受一个数组类型的参数。
  3. map key:表示接受一个 Map 类型的参数,并以 Map 的键作为集合元素的属性。

属性说明

item:表示本次迭代获取到的每个元素,若collection为List、

Set或者数组,则表示其中存储的每个元素;若为map,则代表

key-value的value,该参数为必选。

index:表示当前迭代的元素的下标,在map中,则指代的是key

open:表示循环从哪里开始

separator:表示在每次遍历出来的值之间以什么符号作为分隔

符。

close:表示该语以什么结束。

collection:表示迭代集合的名称,或者使用@Param指定的名称

批量插入

Mapper 接口

//批量插入:一次性插入多条数据
Integer insertStudentBatch(@Param("stuList") List<Student> stuList);

映射文件语句

<!--    使用foreach标签批量插入-->
    <insert id="insertStudentBatch">
         insert into student(stu_id,stu_name,stu_age) values
         <foreach collection="stuList"   item="one" separator=",">
             (null ,#{one.stuName},#{one.stuAge})
         </foreach>
    </insert>

测试方法

@Test
public void test13(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    ArrayList<Student> stuList=new ArrayList<>();
    stuList.add(new Student(null, "ee", 66));
    stuList.add(new Student(null, "tt", 99));
    stuList.add(new Student(null, "yy", 88));

    Integer integer = mapper.insertStudentBatch(stuList);
    System.out.println(integer);
}

执行语句
在这里插入图片描述

其他属性值没有输入为什么还可以执行?

如果你在插入语句中没有为某个属性指定具体的属性值,且该属性在数据库表中被定义为可为NULL(nullable),那么默认情况下会将该属性值设置为NULL。

批量删除

Mapper 接口

//批量删除
Integer deleteStudentBatch(@Param("deleteList") List<Integer> deleteList);

映射文件语句

<delete id="deleteStudentBatch">
    delete from student where stu_id in
    <foreach collection="deleteList" item="i" open="(" close=")" separator=",">
        #{i}
    </foreach>
</delete>

测试方法

@Test
public void test14(){
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    ArrayList<Integer> list=new ArrayList<>();
    list.add(16);
    list.add(17);
    list.add(18);

    Integer integer = mapper.deleteStudentBatch(list);
    System.out.println(integer);
}

执行结果

分页功能

分页插件的使用----pageHelper
导入pageHelper依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>

在核心配置文件中配置分页插件

<!-- 配置pageHelper分页插件-->
<!-- 插件版本5.0之前使用PageHelper 5.0之后使用PageInterceptor-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>

代码

Mapper 接口

List<User>queryUserByPage();

映射文件语句

<select id="queryUserByPage" resultType="user">
select * from user
</select>

测试方法

public void test06(){
PageHelper.startPage(1,2);//表示第一页 每页显示两条数据
List<User> users = mapper.queryUserByPage();
PageInfo<User> pageInfo = new PageInfo<>(users);
System.out.println("当前页码:"+pageInfo.getPageNum());
System.out.println("每页显示条数:"+pageInfo.getPageSize());
System.out.println("当前页实际显示条数:"+pageInfo.getSize());
System.out.println("总记录数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("上一页的页码:"+pageInfo.getPrePage());
System.out.println("下一页的页码:"+pageInfo.getNextPage());
System.out.println("是否为第一页:"+pageInfo.isIsFirstPage());
System.out.println("是否为最后一页:"+pageInfo.isIsLastPage());
System.out.println("是否存在上一
页:"+pageInfo.isHasPreviousPage());
System.out.println("是否存在下一页:"+pageInfo.isHasNextPage());
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis 分页插件可以帮助我们在使用 MyBatis 进行分页查询时,更加方便地编写分页查询语句。下面是 MyBatis 分页插件的详细介绍以及使用方法。 ## 什么是 MyBatis 分页插件? MyBatis 分页插件是一个用于简化 MyBatis 分页查询的工具,它可以自动拦截分页查询语句,并根据传入的分页参数进行分页处理,最终返回分页结果。 MyBatis 分页插件支持多种数据库,包括 MySQL、Oracle、SQL Server 等。同时,它还提供了丰富的配置选项,可以让我们根据实际需求进行灵活配置。 ## 如何使用 MyBatis 分页插件? 使用 MyBatis 分页插件需要进行以下几个步骤: 1. 引入 MyBatis 分页插件的依赖 可以使用 Maven 或 Gradle 等工具,将 MyBatis 分页插件的依赖添加到项目中。以 Maven 为例,需要添加以下依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> ``` 2. 配置 MyBatis 分页插件 在 MyBatis 的配置文件中,需要配置 MyBatis 分页插件。以下是一个示例配置: ```xml <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> <property name="reasonable" value="true"/> <property name="supportMethodsArguments" value="true"/> <property name="params" value="count=countSql"/> </plugin> </plugins> ``` 其中,`interceptor` 指定了使用的插件类,`helperDialect` 指定了数据库类型,`reasonable` 指定了是否开启合理化查询,`supportMethodsArguments` 指定了是否支持方法参数作为分页参数,`params` 指定了参数映射规则。 3. 在 Mapper 中编写分页查询语句 在 Mapper 中编写分页查询语句时,需要使用 MyBatis 分页插件提供的分页参数。以下是一个示例: ```xml <select id="getUsers" resultMap="userResultMap"> select * from users <where> <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if> </where> order by id <if test="pageSize != null and pageNum != null"> limit #{pageSize} offset #{pageSize * (pageNum - 1)} </if> </select> ``` 其中,`pageSize` 和 `pageNum` 分别表示每页大小和当前页码。 4. 调用分页查询方法 最后,在 Service 中调用分页查询方法时,需要传入分页参数。以下是一个示例: ```java PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.getUsers(name); PageInfo<User> pageInfo = new PageInfo<>(userList); ``` 其中,`PageHelper.startPage()` 方法用于启动分页查询,`PageInfo` 用于封装分页结果。 ## 总结 MyBatis 分页插件是一个非常实用的工具,可以大大简化 MyBatis 分页查询的编写和调用过程。在使用 MyBatis 进行分页查询时,推荐使用 MyBatis 分页插件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yjg_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值