Fluent MyBatis实现动态SQL

 更新时间:2021年8月5日 10:17  点击:1497

MyBatis 令人喜欢的一大特性就是动态 SQL。在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的,
MyBatis虽然提供了动态拼装的能力,但这些写xml文件,也确实折磨开发。Fluent MyBatis提供了更贴合Java语言特质的,对程序员友好的Fluent拼装能力。

Fluent MyBatis动态SQL,写SQL更爽

数据准备

为了后面的演示, 创建了一个 Maven 项目 fluent-mybatis-dynamic, 创建了对应的数据库和表

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student`
(
    `id`           bigint(21) unsigned NOT NULL AUTO_INCREMENT COMMENT '编号',
    `name`         varchar(20) DEFAULT NULL COMMENT '姓名',
    `phone`        varchar(20) DEFAULT NULL COMMENT '电话',
    `email`        varchar(50) DEFAULT NULL COMMENT '邮箱',
    `gender`       tinyint(2)  DEFAULT NULL COMMENT '性别',
    `locked`       tinyint(2)  DEFAULT NULL COMMENT '状态(0:正常,1:锁定)',
    `gmt_created`  datetime    DEFAULT CURRENT_TIMESTAMP COMMENT '存入数据库的时间',
    `gmt_modified` datetime    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的时间',
    `is_deleted`   tinyint(2)  DEFAULT 0,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_0900_ai_ci COMMENT ='学生表';

代码生成

使用Fluent Mybatis代码生成器,生成对应的Entity文件

public class Generator {
    static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8";
    /**
     * 生成代码的package路径
     */
    static final String basePackage = "cn.org.fluent.mybatis.dynamic";

    /**
     * 使用 test/resource/init.sql文件自动初始化测试数据库
     */
    @BeforeAll
    static void runDbScript() {
        DataSourceCreatorFactory.create("dataSource");
    }

    @Test
    void test() {
        FileGenerator.build(Nothing.class);
    }

    @Tables(
        /** 数据库连接信息 **/
        url = url, username = "root", password = "password",
        /** Entity类parent package路径 **/
        basePack = basePackage,
        /** Entity代码源目录 **/
        srcDir = "src/main/java",
        /** 如果表定义记录创建,记录修改,逻辑删除字段 **/
        gmtCreated = "gmt_created", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
        /** 需要生成文件的表 ( 表名称:对应的Entity名称 ) **/
        tables = @Table(value = {"student"})
    )

    public static class Nothing {
    }
}

编译项目,ok,下面我们开始动态SQL构造旅程

在 WHERE 条件中使用动态条件

在mybatis中,if 标签是大家最常使用的。在查询、删除、更新的时候结合 test 属性联合使用。

示例:根据输入的学生信息进行条件检索

  • 当只输入用户名时, 使用用户名进行模糊检索;
  • 当只输入性别时, 使用性别进行完全匹配
  • 当用户名和性别都存在时, 用这两个条件进行查询匹配查询

mybatis动态 SQL写法

<select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">
    select
    <include refid="Base_Column_List" />
    from student
    <where>
        <if test="name != null and name !=''">
          and name like concat('%', #{name}, '%')
        </if>
        <if test="sex != null">
          and sex=#{sex}
        </if>
    </where>
</select>

fluent mybatis动态写法

@Repository
public class StudentDaoImpl extends StudentBaseDao implements StudentDao {
   /**
    * 根据输入的学生信息进行条件检索
    * 1. 当只输入用户名时, 使用用户名进行模糊检索;
    * 2. 当只输入性别时, 使用性别进行完全匹配
    * 3. 当用户名和性别都存在时, 用这两个条件进行查询匹配的用
    *
    * @param name   姓名,模糊匹配
    * @param isMale 性别
    * @return
    */
    @Override
    public List<StudentEntity> selectByNameOrEmail(String name, Boolean isMale) {
        return super.defaultQuery()
            .where.name().like(name, If::notBlank)
            .and.gender().eq(isMale, If::notNull).end()
            .execute(super::listEntity);
    }
}

FluentMyBatis的实现方式至少有下面的好处

  • 逻辑就在方法实现上,不需要额外维护xml,割裂开来
  • 所有的编码通过IDE智能提示,没有字符串魔法值编码
  • 编译检查,拼写错误能立即发现

测试

@SpringBootTest(classes = AppMain.class)
public class StudentDaoImplTest extends Test4J {
    @Autowired
    StudentDao studentDao;

    @DisplayName("只有名字时的查询")
    @Test
    void selectByNameOrEmail_onlyName() {
        studentDao.selectByNameOrEmail("明", null);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE name LIKE ?",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"%明%"});
    }

    @DisplayName("只有性别时的查询")
    @Test
    void selectByNameOrEmail_onlyGender() {
        studentDao.selectByNameOrEmail(null, false);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE gender = ?",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{false});
    }

    @DisplayName("姓名和性别同时存在的查询")
    @Test
    void selectByNameOrEmail_both() {
        studentDao.selectByNameOrEmail("明", false);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE name LIKE ? " +
                "AND gender = ?",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"%明%", false});
    }
}

在 UPDATE 使用动态更新

只更新有变化的字段, 空值不更新

mybatis xml写法

<update id="updateByPrimaryKeySelective" parameterType="...">
    update student
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="gender != null">
        gender = #{gender,jdbcType=TINYINT},
      </if>
      <if test="gmtModified != null">
        gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
</update>

fluent mybatis实现

@Repository
public class StudentDaoImpl extends StudentBaseDao implements StudentDao {
    /**
     * 根据主键更新非空属性
     *
     * @param student
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(StudentEntity student) {
        return super.defaultUpdater()
            .update.name().is(student.getName(), If::notBlank)
            .set.phone().is(student.getPhone(), If::notBlank)
            .set.email().is(student.getEmail(), If::notBlank)
            .set.gender().is(student.getGender(), If::notNull)
            .end()
            .where.id().eq(student.getId()).end()
            .execute(super::updateBy);
    }    
}

测试

@SpringBootTest(classes = AppMain.class)
public class StudentDaoImplTest extends Test4J {
    @Autowired
    StudentDao studentDao;

    @Test
    void updateByPrimaryKeySelective() {
        StudentEntity student = new StudentEntity()
            .setId(1L)
            .setName("test")
            .setPhone("13866668888");
        studentDao.updateByPrimaryKeySelective(student);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "UPDATE student " +
                "SET gmt_modified = now(), " +
                "name = ?, " +
                "phone = ? " +
                "WHERE id = ?",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"test", "13866668888", 1L});
    }
}

choose 标签

在mybatis中choose when otherwise 标签可以帮我们实现 if else 的逻辑。

查询条件,假设 name 具有唯一性, 查询一个学生

  • 当 id 有值时, 使用 id 进行查询;
  • 当 id 没有值时, 使用 name 进行查询;
  • 否则返回空

mybatis xml实现

<select id="selectByIdOrName" resultMap="BaseResultMap" parameterType="...">
    select
    <include refid="Base_Column_List" />
    from student
    <where>
        <choose>
          <when test="id != null">
            and id=#{id}
          </when>
          <when test="name != null and name != ''">
            and name=#{name}
          </when>
          <otherwise>
            and 1=2
          </otherwise>
        </choose>
    </where>
</select>

fluent mybatis实现方式

@Repository
public class StudentDaoImpl extends StudentBaseDao implements StudentDao {

    /**
     * 1. 当 id 有值时, 使用 id 进行查询;
     * 2. 当 id 没有值时, 使用 name 进行查询;
     * 3. 否则返回空
     */ 
    @Override
    public StudentEntity selectByIdOrName(StudentEntity student) {
       return super.defaultQuery()
           .where.id().eq(student.getId(), If::notNull)
           .and.name().eq(student.getName(), name -> isNull(student.getId()) && notBlank(name))
           .and.apply("1=2", () -> isNull(student.getId()) && isBlank(student.getName()))
           .end()
           .execute(super::findOne).orElse(null);
    }
}

测试

@SpringBootTest(classes = AppMain.class)
public class StudentDaoImplTest extends Test4J {
    @Autowired
    StudentDao studentDao;

    @DisplayName("有 ID 则根据 ID 获取")
    @Test
    void selectByIdOrName_byId() {
        StudentEntity student = new StudentEntity();
        student.setName("小飞机");
        student.setId(1L);

        StudentEntity result = studentDao.selectByIdOrName(student);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE id = ?",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{1L});
    }

    @DisplayName("没有 ID 则根据 name 获取")
    @Test
    void selectByIdOrName_byName() {
        StudentEntity student = new StudentEntity();
        student.setName("小飞机");
        student.setId(null);

        StudentEntity result = studentDao.selectByIdOrName(student);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE name = ?",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"小飞机"});
    }

    @DisplayName("没有 ID 和 name, 返回 null")
    @Test
    void selectByIdOrName_null() {
        StudentEntity student = new StudentEntity();
        StudentEntity result = studentDao.selectByIdOrName(student);
        // 验证执行的sql语句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE 1=2",
            StringMode.SameAsSpace);
        // 验证sql参数
        db.sqlList().wantFirstPara().eqReflect(new Object[]{});
    }
}

参考

示例代码地址
Fluent MyBatis地址
Fluent MyBatis文档
Test4J框架

到此这篇关于Fluent MyBatis实现动态SQL的文章就介绍到这了,更多相关Fluent MyBatis 动态SQL内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • Mybatis Plus select 实现只查询部分字段

    这篇文章主要介绍了Mybatis Plus select 实现只查询部分字段的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-01
  • 解决Mybatis 大数据量的批量insert问题

    这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
  • mybatis的Configuration详解

    这篇文章主要介绍了mybatis的Configuration详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-04
  • mybatis 返回Integer,Double,String等类型的数据操作

    这篇文章主要介绍了mybatis 返回Integer,Double,String等类型的数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-25
  • MyBatis-Plus的物理删除和逻辑删除(使用场景)

    数据库中的数据删除会分为两种:物理删除 和 逻辑删除,接下来通过本文给大家介绍MyBatis-Plus的物理删除和逻辑删除使用场景分析,感兴趣的朋友一起看看吧...2021-09-25
  • Springboot如何使用mybatis实现拦截SQL分页

    这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19
  • mybatis-plus雪花算法自动生成机器id原理及源码

    Mybatis-Plus是一个Mybatis的增强工具,它在Mybatis的基础上做了增强,却不做改变,Mybatis-Plus是为简化开发、提高开发效率而生,但它也提供了一些很有意思的插件,比如SQL性能监控、乐观锁、执行分析等,下面一起看看mybatis-plus雪花算法自动生成机器id原理解析...2021-06-04
  • 解决Mybatis中mapper.xml文件update,delete及insert返回值问题

    这篇文章主要介绍了解决Mybatis中mapper.xml文件update,delete及insert返回值问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-23
  • Mybatis plus中使用in查询出错如何解决

    这篇文章主要介绍了Mybatis plus中使用in查询出错的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-06
  • Mybatis执行update失败的解决

    这篇文章主要介绍了Mybatis执行update失败的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-01
  • Mybatis用注解写in查询的实现

    这篇文章主要介绍了Mybatis用注解写in查询的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-13
  • mybatis Map查询结果下划线转驼峰的实例

    这篇文章主要介绍了mybatis Map查询结果下划线转驼峰的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-24
  • Mybatis之Select Count(*)的获取返回int的值操作

    这篇文章主要介绍了Mybatis之Select Count(*)的获取返回int的值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-23
  • Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解

    这篇文章主要介绍了Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-18
  • Mybatis和Mybatis-Plus时间范围查询方式

    这篇文章主要介绍了Mybatis和Mybatis-Plus时间范围查询方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-06
  • 关于IDEA 2020使用 mybatis-log-plugin插件的问题

    这篇文章主要介绍了关于IDEA 2020使用 mybatis-log-plugin插件的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-10
  • Mybatis Plus 字段为空值时执行更新方法未更新解决方案

    这篇文章主要介绍了Mybatis Plus 字段为空值时执行更新方法未更新解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-03
  • Mybatis返回结果封装map过程解析

    这篇文章主要介绍了Mybatis返回结果封装map过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-19
  • 基于MyBatis的parameterType传入参数类型

    这篇文章主要介绍了基于MyBatis的parameterType传入参数类型,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-29
  • springboot配置多数据源后mybatis拦截器失效的解决

    这篇文章主要介绍了springboot配置多数据源后mybatis拦截器失效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-23