SpringBoot(44) — MyBatis-plus自定义

2021-11-18  本文已影响0人  奔跑的佩恩

前言

之前已经讲过了MyBatis-plus相关查询知识了,有兴趣的可参考以下文章
SpringBoot(40) — SpringBoot整合MyBatis-plus
SpringBoot(41) — MyBatis-plus常用查询
SpringBoot(42) — MyBatis-plus查询数据表中一列数据的部分字段
SpringBoot(43) — MyBatis-plus一些特殊查询
但是有些极端情况,我们用MyBatis-plus条件构造器依然无法满足我们的查询需求,这时候就需要我们去自己组装sql语句进行查询了。今天就让我们来学习下MyBatis-plus自定义sql语句查询的知识。
今天涉及的内容有:

  1. 前期准备
  2. wrapper自定义sql语句代码查询
  3. wrapper自定义sql语句xml文件查询
  4. 自定义sql语句代码查询
  5. 自定义sql语句xml文件查询

一. 前期准备

先要在SpringBoot项目中配置好MyBatis-plus,准备一个数据库(我这里采用的MySql数据库),连接上并开启数据库服务。
准备一个数据表映射实体类Student,然后是继承BaseMapper实现的数据表操作类StudentMapper
先给出数据库test_prodemo表的数据:

image.png
接着给出Student类代码:
/**
 * Title:
 * description:
 * autor:pei
 * created on 2019/9/3
 */
@Data
@Component("Student")
@TableName(value = "demo")
public class Student {

    //主键自增
    @TableId(value = "id",type = IdType.AUTO)
    private int id;

    @TableField(value = "name") //表属性
    private String name;

    @TableField(value = "age") //表属性
    private int age;

}

最后给出数据表操作类StudentMapper代码:

/**
 * Title:
 * description:
 * autor:pei
 * created on 2019/9/3
 */
@Repository
public interface StudentMapper extends BaseMapper<Student> {

}

这样,查询前的准备工作就做好了。

二. wrapper自定义sql语句代码查询

以查询所有数据为例,先在StudentMapper类中新建方法,并在方法上面添加注解,如下:

上一篇 下一篇

猜你喜欢

热点阅读