Mybatis根据Bean生成对应的SQL语句工具类

文章介绍了在使用MyBatis时手动编写大量CRUDSQL的繁琐,特别是当实体类字段增多时,插入、更新和查询语句变得极其复杂。作者提出了使用工具类MybatisCrudHelp自动根据实体类生成SQL语句,以简化工作并提高效率。
摘要由CSDN通过智能技术生成

前言

在进入这一节知道MyBatis-Plus的人可能有人要问了:为什么我不用Mybatis-plus

为什么不用MyBatis-Plus

对于这个问题,我的意见请参考以下几篇文章:

MyBatis-Plus: 谨慎入坑
为什么不建议你使用Mybatis-plus

反正用着就不是很得劲。

Mybatis的困扰

大家在使用Mybatis的时候肯定遇到过这样问题:当你接到一个新的业务,设计好了一个表结构,写好bean以后,这个表至少需要增删改查的功能吧?那是不是需要在对应的xml文件里写插入、更新、删除语句。我们现在以一个简单的Student类为例子:

Student.java

@Data
public class Student {
    //学号
    String studentId;
    //姓名
    String name;
    //年龄
    Integer age;
    //性别
    Integer sex;
}

上述的@Datalombok的注解,主要是用于自动根据属性构建一些setter和getter以及一些常见实体类方法

插入和更新

实体类有了那我们就该开始在xml文件里crud的基本语句了,以下只演示插入和更新。
新增

insert into student(
 studentId
 ,name
 ,age
 ,sex
 ) values(
  #{studentId}
  ,#{name}
  ,#{age}
  ,#{sex}
)

更新

update student
<set>
 studentId=#{studentId}
 ,name=#{name}
 ,age=#{age}
 ,sex=#{sex}
</set>
where xxx = #{xxx}

咋一看是不是还没多少?但需要注意的是,新增和更新的时候有的字段还不是必填,那么就还需要进行一个判空的处理。那么上述语句就需要变成以下这样:
新增

insert into student(
  <if test="studentId != null and studentId != ''">studentId</if>
  <if test="name != null and name != ''">,name</if>
  <if test="age != null">,age</if>
  <if test="sex != null">,sex</if>
 ) values(
  <if test="studentId != null and studentId != ''">#{studentId}</if>
  <if test="name != null and name != ''">,#{name}</if>
  <if test="age != null">,#{age}</if>
  <if test="sex != null">,#{sex}</if>
)

更新

update student
<set>
  <if test="studentId != null and studentId != ''">studentId=#{studentId}</if>
  <if test="name != null and name != ''">,name=#{name}</if>
  <if test="age != null">,age=#{age}</if>
  <if test="sex != null">,sex=#{sex}</if>
</set>
where xxx = #{xxx}

是不是已经有点烦了?别急还有更加绝望的。我们来看看查询,在以下的讨论中我们暂且先抛开性能以及是否走索引的情况

查询

如果查询条件可以是所有字段,并且可以任意搭配

比如我要搜索学号包含2023,年龄为18的学生信息,或者是年龄为18,性别为女的学生信息,又或者是姓张,学号包含2023,性别为女的学生信息等等。你总不能每个功能写一个查询语句吧,那也太麻烦了。

所以我们可以这么写:

select 
studentId,name,age,sex
 from student
<where>
  <if test="studentId != null and studentId != ''">studentId like  concat('%',#{studentId},'%')</if>
  <if test="name != null and name != ''"> and name like  concat('%',#{name},'%')</if>
  <if test="age != null"> and age = #{age}</if>
  <if test="sex != null"> and sex = #{sex}</if>
</where>

然后查询的方法里还要加上对应的注解

List<Student> getAllStudentInfo(
	@Param("studentId")String studentId
	, @Param("name")String name
	, @Param("age")Integer age
	, @Param("sex")Integer sex
);

你是不是还是觉得也不是很麻烦?很好,现在我们将上述Student类扩充一下,给它的字段再加一~~~~~点点,扩充到37个属性,让学生信息更完整。体验一下遇到多字段的实体类时,编写SQL的赶脚。

一般十几个、二十几个字段的表的很常见,三十几个的也有,所以这可不是我胖虎为难你们。

import lombok.Data;

import java.sql.Date;

/**
 * @Description
 * @Author 三文鱼先生
 * @Data 2023/5/11 10:35
 */
@Data
public class Student {
    //学号
    String studentId;
    //姓名
    String name;
    //年龄
    Integer age;
    //性别
    Integer sex;
    //国籍
    Integer nationality;
    //港澳台侨信息
    Integer hmtc;
    //身份证
    String idCard;
    //身份证类型
    Integer cardType;
    //政治面貌
    Integer politicCountenance;
    //民族
    String ethnicGroup;
    //籍贯
    String nativePlace;
    //学生类别
    Integer studentType;
    //是否独生子女
    Integer justOnly;
    //是否流动人口
    Integer floating;
    //是否留守儿童
    Integer leftoverChildren;
    //是否残疾
    Integer disability;
    //残疾类型
    String disabilityType;
    //入学时间
    Date admissionDate;
    //入学方式
    Integer admissionType;
    //就读方式
    Integer studyMode;
    //学生来源
    Integer studentSource;
    //健康等级
    Integer healthLevel;
    //宗教信仰
    String religion;
    //就读年级
    Integer grade;
    //班级名称
    String className;
    //学生联系电话
    String studentPhone;
    //住址
    String address;
    //出生日期
    String birthday;
    //邮箱
    String email;
    //学生图片存储地址
    String studentImage;
    //监护人姓名
    String guardianName;
    //监护人电话
    String guardianPhone;
    //与监护人关系
    String relationshipWithGuardian;
    //监护人照片存储地址
    String guardianImage;
    //班主任姓名
    String classTeacher;
    //班主任电话
    String teacherPhone;
    //班主任照片存储地址
    String teacherImage;

}

在这里插入图片描述

好了,怕也没用,现在我们再来写之前提到过的新增、更新、查询的语句。大家可以自己试着写一下(当然啊我就不写了,我不打扰,我走了哈),让代码能够正常运行,并且记录从开始到结束的时间,就知道这个过程有多浪费时间了。写出来大概是以下这样:

新增

insert into student(
  <if test="studentId != null and studentId != ''">studentId</if>
  <if test="name != null and name != ''">,name</if>
  <if test="age != null">,age</if>
  <if test="sex != null">,sex</if>
  <if test="nationality != null">,nationality</if>
  <if test="hmtc != null">,hmtc</if>
  <if test="idCard != null and idCard != ''">,idCard</if>
  <if test="cardType != null">,cardType</if>
  <if test="politicCountenance != null">,politicCountenance</if>
  <if test="ethnicGroup != null and ethnicGroup != ''">,ethnicGroup</if>
  <if test="nativePlace != null and nativePlace != ''">,nativePlace</if>
  <if test="studentType != null">,studentType</if>
  <if test="justOnly != null">,justOnly</if>
  <if test="floating != null">,floating</if>
  <if test="leftoverChildren != null">,leftoverChildren</if>
  <if test="disability != null">,disability</if>
  <if test="disabilityType != null and disabilityType != ''">,disabilityType</if>
  <if test="admissionDate != null and admissionDate != ''">,admissionDate</if>
  <if test="admissionType != null">,admissionType</if>
  <if test="studyMode != null">,studyMode</if>
  <if test="studentSource != null">,studentSource</if>
  <if test="healthLevel != null">,healthLevel</if>
  <if test="religion != null and religion != ''">,religion</if>
  <if test="grade != null">,grade</if>
  <if test="className != null and className != ''">,className</if>
  <if test="studentPhone != null and studentPhone != ''">,studentPhone</if>
  <if test="address != null and address != ''">,address</if>
  <if test="birthday != null and birthday != ''">,birthday</if>
  <if test="email != null and email != ''">,email</if>
  <if test="studentImage != null and studentImage != ''">,studentImage</if>
  <if test="guardianName != null and guardianName != ''">,guardianName</if>
  <if test="guardianPhone != null and guardianPhone != ''">,guardianPhone</if>
  <if test="relationshipWithGuardian != null and relationshipWithGuardian != ''">,relationshipWithGuardian</if>
  <if test="guardianImage != null and guardianImage != ''">,guardianImage</if>
  <if test="classTeacher != null and classTeacher != ''">,classTeacher</if>
  <if test="teacherPhone != null and teacherPhone != ''">,teacherPhone</if>
  <if test="teacherImage != null and teacherImage != ''">,teacherImage</if>
 ) values(
  <if test="studentId != null and studentId != ''">#{studentId}</if>
  <if test="name != null and name != ''">,#{name}</if>
  <if test="age != null">,#{age}</if>
  <if test="sex != null">,#{sex}</if>
  <if test="nationality != null">,#{nationality}</if>
  <if test="hmtc != null">,#{hmtc}</if>
  <if test="idCard != null and idCard != ''">,#{idCard}</if>
  <if test="cardType != null">,#{cardType}</if>
  <if test="politicCountenance != null">,#{politicCountenance}</if>
  <if test="ethnicGroup != null and ethnicGroup != ''">,#{ethnicGroup}</if>
  <if test="nativePlace != null and nativePlace != ''">,#{nativePlace}</if>
  <if test="studentType != null">,#{studentType}</if>
  <if test="justOnly != null">,#{justOnly}</if>
  <if test="floating != null">,#{floating}</if>
  <if test="leftoverChildren != null">,#{leftoverChildren}</if>
  <if test="disability != null">,#{disability}</if>
  <if test="disabilityType != null and disabilityType != ''">,#{disabilityType}</if>
  <if test="admissionDate != null and admissionDate != ''">,#{admissionDate}</if>
  <if test="admissionType != null">,#{admissionType}</if>
  <if test="studyMode != null">,#{studyMode}</if>
  <if test="studentSource != null">,#{studentSource}</if>
  <if test="healthLevel != null">,#{healthLevel}</if>
  <if test="religion != null and religion != ''">,#{religion}</if>
  <if test="grade != null">,#{grade}</if>
  <if test="className != null and className != ''">,#{className}</if>
  <if test="studentPhone != null and studentPhone != ''">,#{studentPhone}</if>
  <if test="address != null and address != ''">,#{address}</if>
  <if test="birthday != null and birthday != ''">,#{birthday}</if>
  <if test="email != null and email != ''">,#{email}</if>
  <if test="studentImage != null and studentImage != ''">,#{studentImage}</if>
  <if test="guardianName != null and guardianName != ''">,#{guardianName}</if>
  <if test="guardianPhone != null and guardianPhone != ''">,#{guardianPhone}</if>
  <if test="relationshipWithGuardian != null and relationshipWithGuardian != ''">,#{relationshipWithGuardian}</if>
  <if test="guardianImage != null and guardianImage != ''">,#{guardianImage}</if>
  <if test="classTeacher != null and classTeacher != ''">,#{classTeacher}</if>
  <if test="teacherPhone != null and teacherPhone != ''">,#{teacherPhone}</if>
  <if test="teacherImage != null and teacherImage != ''">,#{teacherImage}</if>
)

要是我手动写,估计这会我已经能看见天堂了
在这里插入图片描述

更新

update student
<set>
  <if test="studentId != null and studentId != ''">studentId=#{studentId}</if>
  <if test="name != null and name != ''">,name=#{name}</if>
  <if test="age != null">,age=#{age}</if>
  <if test="sex != null">,sex=#{sex}</if>
  <if test="nationality != null">,nationality=#{nationality}</if>
  <if test="hmtc != null">,hmtc=#{hmtc}</if>
  <if test="idCard != null and idCard != ''">,idCard=#{idCard}</if>
  <if test="cardType != null">,cardType=#{cardType}</if>
  <if test="politicCountenance != null">,politicCountenance=#{politicCountenance}</if>
  <if test="ethnicGroup != null and ethnicGroup != ''">,ethnicGroup=#{ethnicGroup}</if>
  <if test="nativePlace != null and nativePlace != ''">,nativePlace=#{nativePlace}</if>
  <if test="studentType != null">,studentType=#{studentType}</if>
  <if test="justOnly != null">,justOnly=#{justOnly}</if>
  <if test="floating != null">,floating=#{floating}</if>
  <if test="leftoverChildren != null">,leftoverChildren=#{leftoverChildren}</if>
  <if test="disability != null">,disability=#{disability}</if>
  <if test="disabilityType != null and disabilityType != ''">,disabilityType=#{disabilityType}</if>
  <if test="admissionDate != null and admissionDate != ''">,admissionDate=#{admissionDate}</if>
  <if test="admissionType != null">,admissionType=#{admissionType}</if>
  <if test="studyMode != null">,studyMode=#{studyMode}</if>
  <if test="studentSource != null">,studentSource=#{studentSource}</if>
  <if test="healthLevel != null">,healthLevel=#{healthLevel}</if>
  <if test="religion != null and religion != ''">,religion=#{religion}</if>
  <if test="grade != null">,grade=#{grade}</if>
  <if test="className != null and className != ''">,className=#{className}</if>
  <if test="studentPhone != null and studentPhone != ''">,studentPhone=#{studentPhone}</if>
  <if test="address != null and address != ''">,address=#{address}</if>
  <if test="birthday != null and birthday != ''">,birthday=#{birthday}</if>
  <if test="email != null and email != ''">,email=#{email}</if>
  <if test="studentImage != null and studentImage != ''">,studentImage=#{studentImage}</if>
  <if test="guardianName != null and guardianName != ''">,guardianName=#{guardianName}</if>
  <if test="guardianPhone != null and guardianPhone != ''">,guardianPhone=#{guardianPhone}</if>
  <if test="relationshipWithGuardian != null and relationshipWithGuardian != ''">,relationshipWithGuardian=#{relationshipWithGuardian}</if>
  <if test="guardianImage != null and guardianImage != ''">,guardianImage=#{guardianImage}</if>
  <if test="classTeacher != null and classTeacher != ''">,classTeacher=#{classTeacher}</if>
  <if test="teacherPhone != null and teacherPhone != ''">,teacherPhone=#{teacherPhone}</if>
  <if test="teacherImage != null and teacherImage != ''">,teacherImage=#{teacherImage}</if>
</set>
where xxx = #{xxx}

写完这个,估计:
在这里插入图片描述

查询

select 
studentId,name,age,sex,nationality,hmtc,idCard,cardType,politicCountenance,ethnicGroup,nativePlace,studentType,justOnly,floating,leftoverChildren,disability,disabilityType,admissionDate,admissionType,studyMode,studentSource,healthLevel,religion,grade,className,studentPhone,address,birthday,email,studentImage,guardianName,guardianPhone,relationshipWithGuardian,guardianImage,classTeacher,teacherPhone,teacherImage
 from student
<where>
  <if test="studentId != null and studentId != ''">studentId like  concat('%',#{studentId},'%')</if>
  <if test="name != null and name != ''"> and name like  concat('%',#{name},'%')</if>
  <if test="age != null"> and age = #{age}</if>
  <if test="sex != null"> and sex = #{sex}</if>
  <if test="nationality != null"> and nationality = #{nationality}</if>
  <if test="hmtc != null"> and hmtc = #{hmtc}</if>
  <if test="idCard != null and idCard != ''"> and idCard like  concat('%',#{idCard},'%')</if>
  <if test="cardType != null"> and cardType = #{cardType}</if>
  <if test="politicCountenance != null"> and politicCountenance = #{politicCountenance}</if>
  <if test="ethnicGroup != null and ethnicGroup != ''"> and ethnicGroup like  concat('%',#{ethnicGroup},'%')</if>
  <if test="nativePlace != null and nativePlace != ''"> and nativePlace like  concat('%',#{nativePlace},'%')</if>
  <if test="studentType != null"> and studentType = #{studentType}</if>
  <if test="justOnly != null"> and justOnly = #{justOnly}</if>
  <if test="floating != null"> and floating = #{floating}</if>
  <if test="leftoverChildren != null"> and leftoverChildren = #{leftoverChildren}</if>
  <if test="disability != null"> and disability = #{disability}</if>
  <if test="disabilityType != null and disabilityType != ''"> and disabilityType like  concat('%',#{disabilityType},'%')</if>
  <if test="admissionDate != null and admissionDate != ''"> and admissionDate like  concat('%',#{admissionDate},'%')</if>
  <if test="admissionType != null"> and admissionType = #{admissionType}</if>
  <if test="studyMode != null"> and studyMode = #{studyMode}</if>
  <if test="studentSource != null"> and studentSource = #{studentSource}</if>
  <if test="healthLevel != null"> and healthLevel = #{healthLevel}</if>
  <if test="religion != null and religion != ''"> and religion like  concat('%',#{religion},'%')</if>
  <if test="grade != null"> and grade = #{grade}</if>
  <if test="className != null and className != ''"> and className like  concat('%',#{className},'%')</if>
  <if test="studentPhone != null and studentPhone != ''"> and studentPhone like  concat('%',#{studentPhone},'%')</if>
  <if test="address != null and address != ''"> and address like  concat('%',#{address},'%')</if>
  <if test="birthday != null and birthday != ''"> and birthday like  concat('%',#{birthday},'%')</if>
  <if test="email != null and email != ''"> and email like  concat('%',#{email},'%')</if>
  <if test="studentImage != null and studentImage != ''"> and studentImage like  concat('%',#{studentImage},'%')</if>
  <if test="guardianName != null and guardianName != ''"> and guardianName like  concat('%',#{guardianName},'%')</if>
  <if test="guardianPhone != null and guardianPhone != ''"> and guardianPhone like  concat('%',#{guardianPhone},'%')</if>
  <if test="relationshipWithGuardian != null and relationshipWithGuardian != ''"> and relationshipWithGuardian like  concat('%',#{relationshipWithGuardian},'%')</if>
  <if test="guardianImage != null and guardianImage != ''"> and guardianImage like  concat('%',#{guardianImage},'%')</if>
  <if test="classTeacher != null and classTeacher != ''"> and classTeacher like  concat('%',#{classTeacher},'%')</if>
  <if test="teacherPhone != null and teacherPhone != ''"> and teacherPhone like  concat('%',#{teacherPhone},'%')</if>
  <if test="teacherImage != null and teacherImage != ''"> and teacherImage like  concat('%',#{teacherImage},'%')</if>

写完这个,估计:
在这里插入图片描述

参数设置为

List<Student> getAllStudentInfo(
 @Param("studentId")String studentId
, @Param("name")String name
, @Param("age")Integer age
, @Param("sex")Integer sex
, @Param("nationality")Integer nationality
, @Param("hmtc")Integer hmtc
, @Param("idCard")String idCard
, @Param("cardType")Integer cardType
, @Param("politicCountenance")Integer politicCountenance
, @Param("ethnicGroup")String ethnicGroup
, @Param("nativePlace")String nativePlace
, @Param("studentType")Integer studentType
, @Param("justOnly")Integer justOnly
, @Param("floating")Integer floating
, @Param("leftoverChildren")Integer leftoverChildren
, @Param("disability")Integer disability
, @Param("disabilityType")String disabilityType
, @Param("admissionDate")Date admissionDate
, @Param("admissionType")Integer admissionType
, @Param("studyMode")Integer studyMode
, @Param("studentSource")Integer studentSource
, @Param("healthLevel")Integer healthLevel
, @Param("religion")String religion
, @Param("grade")Integer grade
, @Param("className")String className
, @Param("studentPhone")String studentPhone
, @Param("address")String address
, @Param("birthday")String birthday
, @Param("email")String email
, @Param("studentImage")String studentImage
, @Param("guardianName")String guardianName
, @Param("guardianPhone")String guardianPhone
, @Param("relationshipWithGuardian")String relationshipWithGuardian
, @Param("guardianImage")String guardianImage
, @Param("classTeacher")String classTeacher
, @Param("teacherPhone")String teacherPhone
, @Param("teacherImage")String teacherImage);

写完这个
在这里插入图片描述

我就问你,头大不大?现在怕不怕? 反正我自己看了都头皮发麻
在这里插入图片描述

是的,在字段变多以后,编写xml中的语句花费的时间将会大大增长,而且这还不算出错的情况,要是写错一个字母或者符号导致运行失败,那么排查、解决错误花费的时也会很多

不过作为程序员,学会偷懒是必备的。写个工具类让它自动根据实体生成对应的语句不就好了?
在这里插入图片描述

crud工具类

作为CRUD程序员那自然是需要一个快速实现上述功能的工具类了。我苦练多年的Java技术,不是想证明我有多么了不起,我是要告诉别人,我CV的技术真的很流批!
在这里插入图片描述

MybatisCrudHelp.java

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwl
 * @title
 * @description 用于快速生成实体类的SQL语句
 * @date 2023/5/11
 **/
public class MybatisCrudHelp {
    //所有字段
    public List<String> fieldList = null;
    //所有类型
    public List<String> typeList = null;
    public StringBuilder stringBuilder = new StringBuilder();

    /**
     * @Description  从对应的实体类中得到各个字段的属性和类型
     * @Param cs 类
     * @Param tableName 表名
     * @Return
     * @Author 三文鱼先生
     * @Date 2023/5/11 14:14
     **/
    public void parseClass(Class cs , String tableName) {
        fieldList = getAllParams(cs);
        typeList = getParamsType(cs , fieldList);
    }

    /**
     * @Description 根据实体类和表名 生成对应的SQL语句
     * @Param cs 实体类
     * @Param tableName 表名
     * @Param type 0 - 插入 1-更新 2-查询
     * @Return {@link String}
     * @Author 三文鱼先生
     * @Date 2023/5/11 14:08
     **/
    public String getString(Class cs , String tableName ,int type) {
        parseClass(cs , tableName);
        return getStr(fieldList , typeList , tableName , type);
    }

    /**
     * @Description 获取所有字段的值 凭接成如 a,b,c....
     * @Param cs
     * @Param tableName
     * @Param type
     * @Return {@link String}
     * @Author 三文鱼先生
     * @Date 2023/4/7 10:25
     **/
    public String getAllFieldString(Class cs , String tableName) {
        parseClass(cs , tableName);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i= 0; i < fieldList.size(); i++) {
            if(i !=0 )
                stringBuilder.append(",");
            stringBuilder.append(fieldList.get(i));
        }
        return stringBuilder.toString();
    }

    /**
     * @Description 获取方法查询时的所有条件
     * @Param cs
     * @Param tableName
     * @Return {@link String}
     * @Author 三文鱼先生
     * @Date 2023/4/14 10:36
     **/
    public String getAllFieldTypeWithParamsString(Class cs , String tableName , boolean needTime) {
        parseClass(cs , tableName);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i= 0; i < fieldList.size(); i++) {
           String[] type = typeList.get(i).split("\\.");
           String fType = type[type.length - 1];

           stringBuilder.append("@Param(\"")
                   .append(fieldList.get(i))
                   .append("\")")
                   .append(fType)
                   .append(" ").append(fieldList.get(i)).append("\n");
           if(i != fieldList.size() - 1) {
                stringBuilder.append(", ");
           }
        }

        //需要时间作为查询参数
        if(needTime) {
            stringBuilder.append(", ");
            stringBuilder.append("@Param(\"")
                    .append("beginTime")
                    .append("\")")
                    .append("String")
                    .append(" ")
                    .append("beginTime\n");

            stringBuilder.append(", ");
            stringBuilder.append("@Param(\"")
                    .append("endTime")
                    .append("\")")
                    .append("String")
                    .append(" ")
                    .append("endTime");
        }
        return stringBuilder.toString();
    }

    /**
     * @Description
     * @Param fieldList 所有字段
     * @Param typeList 所有类型
     * @Param tableName 表名称
     * @Param type 类型 0新增 1更新
     * @Return {@link String}
     * @Author 三文鱼先生
     * @Date 2023/4/6 16:00
     **/
    public String getStr(List<String> fieldList,
            List<String> typeList ,
            String tableName,
            int type) {
        //插入
        if(type==0) {
            stringBuilder.delete(0,stringBuilder.length());
            stringBuilder.append("insert into ").append(tableName).append("(\n");
            //写入条件
            for (int i = 0; i < fieldList.size(); i++) {
                if(i == 0)
                    getNullCondition(fieldList.get(i) , typeList.get(i) , false);
                else
                    getNullCondition(fieldList.get(i) , typeList.get(i) , true);
            }
            stringBuilder.append(" ) values(\n");
            //写入值
            for (int i = 0; i < fieldList.size(); i++) {
                if(i == 0)
                    getValue(fieldList.get(i) , typeList.get(i) , false);
                else
                    getValue(fieldList.get(i) , typeList.get(i) , true);
            }
            stringBuilder.append(")\n");
        }
        //更新
        if(type==1) {
            stringBuilder.delete(0,stringBuilder.length());
            stringBuilder.append("update ").append(tableName).append("\n");
            stringBuilder.append("<set>\n");
            //写入更新条件
            //写入值
            for (int i = 0; i < fieldList.size(); i++) {
                if(i == 0)
                    getValueUpdate(fieldList.get(i) , typeList.get(i) , false);
                else
                    getValueUpdate(fieldList.get(i) , typeList.get(i) , true);
            }
            stringBuilder.append("</set>");
        }
        //查询语句
        if(type == 2) {
            stringBuilder.delete(0,stringBuilder.length());
            stringBuilder.append("select \n");
            //所有返回字段
            for (int i= 0; i < fieldList.size(); i++) {
                if(i !=0 )
                    stringBuilder.append(",");
                stringBuilder.append(fieldList.get(i));
            }
            stringBuilder
                    .append("\n from ").append(tableName).append("\n");
            //所有值
            stringBuilder.append("<where>\n");
            for (int i= 0; i < fieldList.size(); i++) {
                if(i == 0)
                    getQuery(fieldList.get(i) , typeList.get(i) , false);
                else
                    getQuery(fieldList.get(i) , typeList.get(i) , true);
            }

			//默认加入时间范围查询条件
            stringBuilder.append("  <if test=\"")
                    .append("beginTime")
                    .append(" != null")
                    .append(" and ")
                    .append("beginTime")
                    .append(" != ''")
                    .append("\">")
                    .append(" and ")
                    .append("beginTime")
                    .append(" >= ")
                    .append("#{beginTime}")
                    .append("</if>\n");

            stringBuilder.append("  <if test=\"")
                    .append("endTime")
                    .append(" != null")
                    .append(" and ")
                    .append("endTime")
                    .append(" != ''")
                    .append("\">")
                    .append(" and ")
                    .append("#{endTime}")
                    .append(" >= ")
                    .append("endTime")
                    .append("</if>\n");


            stringBuilder.append("</where>\n");
        }
        return stringBuilder.toString();
    }

    /**
     * @Description 获取字段的空的字段那一行
     * 如<if test="field != null and field != ''">field</if>
     * @Param field 字段名称
     * @Param type 字段类型
     * @Param needSymbol 字段前是否需要,标点符号
     * @Return
     * @Author 三文鱼先生
     * @Date 2023/4/6 15:49
     **/
    public void getNullCondition(String field , String type , boolean needSymbol) {
//        System.out.println(field + "  " + type);
        stringBuilder.append("  <if test=\"");
        stringBuilder.append(field)
                .append(" != null");
        //非数字字段要进行判空
        if(!type.equals("class java.lang.Integer")) {
            stringBuilder.append(" and ")
                    .append(field)
                    .append(" != ''");
        }
        stringBuilder.append("\">");
        if(needSymbol)
            stringBuilder.append(",");
        stringBuilder.append(field)
                .append("</if>\n");
    }

    /**
     * @Description 获取字段的空的字段那一行
     * 如<if test="field != null and field != ''">#{field}</if>
     * @Param field 字段名称
     * @Param type 字段类型
     * @Param needSymbol 字段前是否需要,标点符号
     * @Return
     * @Author 三文鱼先生
     * @Date 2023/4/6 15:49
     **/
    public void getValue(String field , String type , boolean needSymbol) {
        stringBuilder.append("  <if test=\"");
        stringBuilder.append(field)
                .append(" != null");
        //非数字字段要进行判空
        if(!type.equals("class java.lang.Integer")) {
            stringBuilder.append(" and ")
                    .append(field)
                    .append(" != ''");
        }
        stringBuilder.append("\">");
        if(needSymbol)
            stringBuilder.append(",");
        stringBuilder.append("#{")
                .append(field)
                .append("}</if>\n");
    }

    /**
     * @Description 获取查询字段的拼接
     * @Param field 字段名
     * @Param type 字段类型
     * @Param needSymbol 是否需要 ,
     * @Return
     * @Author 三文鱼先生
     * @Date 2023/5/11 14:11
     **/
    public void getQuery(String field , String type , boolean needSymbol) {
        stringBuilder.append("  <if test=\"");
        stringBuilder.append(field)
                .append(" != null");
        //非数字字段要进行判空
        if(!type.equals("class java.lang.Integer")) {
            stringBuilder.append(" and ")
                    .append(field)
                    .append(" != ''");
            stringBuilder.append("\">");
            if(needSymbol)
                stringBuilder.append(" and ");
            //oracle的模糊查询
//            stringBuilder
//                    .append(field)
//                    .append(" like '%'||#{")
//                    .append(field)
//                    .append("}||'%'</if>\n");

            //mysql的模糊查询
                stringBuilder
                    .append(field)
                    .append(" like  concat('%',#{")
                    .append(field)
                    .append("},'%')</if>\n");
        }
        else {
            stringBuilder.append("\">");
            if(needSymbol)
                stringBuilder.append(" and ");
            stringBuilder
                    .append(field)
                    .append(" = #{")
                    .append(field)
                    .append("}</if>\n");
        }

    }

    /**
     * @Description  更新语句的字符拼接
     * @Param field 字段
     * @Param type 字段类型
     * @Param needSymbol 是否需要 ,
     * @Return
     * @Author 三文鱼先生
     * @Date 2023/5/11 14:10
     **/
    public void getValueUpdate(String field , String type , boolean needSymbol) {
        stringBuilder.append("  <if test=\"");
        stringBuilder.append(field)
                .append(" != null");
        //非数字字段要进行判空
        if(!type.equals("class java.lang.Integer")) {
            stringBuilder.append(" and ")
                    .append(field)
                    .append(" != ''");
        }
        stringBuilder.append("\">");
        if(needSymbol)
            stringBuilder.append(",");
        stringBuilder.append(field)
                .append("=")
                .append("#{")
                .append(field)
                .append("}</if>\n");
    }


    /**
     * @Description 获取所有对象属性值
     * @Param cs 类名
     * @Return {@link List< String>}
     * @Author 三文鱼先生
     * @Date 2023/5/11 14:11
     **/
    public List<String>  getAllParams(Class cs) {
        List<String> list = new ArrayList<>();
        Field[] fs = cs.getDeclaredFields();
        for (Field f : fs) {
            list.add(f.getName());
        }
        return list;
    }

    /**
     * @Description 获取数据的对应字段的类型
     * @Param cs 类名
     * @Param paramsList 字段的list
     * @Return {@link List< String>}
     * @Author 三文鱼先生
     * @Date 2023/5/11 14:12
     **/
    public List<String> getParamsType(Class cs , List<String> paramsList) {
        List<String> typeClass = new ArrayList<>();
        //对象的所有属性
        Field[] fields = cs.getDeclaredFields();
        //临时的属性 - 类型映射
        Map<String , Class> map = new HashMap();
        //获取属性名称及类型
        for (Field field : fields) {
            map.put(field.getName(), field.getType());
        }
        //遍历属性List获取对应的类型List
        for (String s : paramsList) {
            typeClass.add(map.get(s).toString());
        }
        return typeClass;
    }

}

测试类

现在就来测试一下吧。

public class Test {
    public static void main(String[] args) {
        Class<Student> myClass = Student.class;
        String tableName = "student";
        MybatisCrudHelp crudHelp = new MybatisCrudHelp();
        System.out.println("-------------新增语句-------------------");
        System.out.println(crudHelp.getString(myClass, tableName, 0));
        System.out.println("-------------更新语句-------------------");
        System.out.println(crudHelp.getString(myClass, tableName, 1));
        System.out.println("-------------所有字段-------------------");
        System.out.println(crudHelp.getAllFieldString(myClass , tableName));
        System.out.println("-------------查询语句-------------------");
        System.out.println(crudHelp.getString(myClass, tableName, 2));
        System.out.println("-------------查询方法内的所有条件-------------------");
        System.out.println(crudHelp.getAllFieldTypeWithParamsString(myClass, tableName , false));
    }
}

测试结果控制台输出如下:

-------------新增语句-------------------
insert into student(
 	省略具体参考上面的插入语句
)

-------------更新语句-------------------
update student
<set>
	省略
</set>
-------------所有字段-------------------
studentId,name,age,sex,nationality,hmtc,idCard,cardType,politicCountenance,ethnicGroup,nativePlace,studentType,justOnly,floating,leftoverChildren,disability,disabilityType,admissionDate,admissionType,studyMode,studentSource,healthLevel,religion,grade,className,studentPhone,address,birthday,email,studentImage,guardianName,guardianPhone,relationshipWithGuardian,guardianImage,classTeacher,teacherPhone,teacherImage
-------------查询语句-------------------
select 
studentId,name,age,sex,nationality,hmtc,idCard,cardType,politicCountenance,ethnicGroup,nativePlace,studentType,justOnly,floating,leftoverChildren,disability,disabilityType,admissionDate,admissionType,studyMode,studentSource,healthLevel,religion,grade,className,studentPhone,address,birthday,email,studentImage,guardianName,guardianPhone,relationshipWithGuardian,guardianImage,classTeacher,teacherPhone,teacherImage
 from student
<where>
	省略
  <if test="beginTime != null and beginTime != ''"> and beginTime >= #{beginTime}</if>
  <if test="endTime != null and endTime != ''"> and #{endTime} >= endTime</if>
</where>

-------------查询方法内的所有条件-------------------
@Param("studentId")String studentId
//省略
, @Param("teacherImage")String teacherImage

虽然看起来很方便,但还是有一些值得注意的点。

需要注意的点

查询语句中会自动带上时间范围
在这里插入图片描述
这是因为我遇到大部分查询都是需要的,大家不需要的话可以在工具类中去掉。

更新语句没有where条件
在这里插入图片描述
这是因为不同的表或者业务的更新逻辑都不同,加个参数我又觉得麻烦,所以索性不写,大家按自己的逻辑加上即可。

更新或者新增的参数不能为空
如果为空对象会报sql语句错误,所以最好新增或者更新之前对对象做一个判空的操作

这样的话我们就可以从大部分的基础SQL中解脱出来了。
在这里插入图片描述

开始写bug吧!
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值