经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Fluent MyBatis实现动态SQL
来源:jb51  时间:2021/8/4 17:55:38  对本文有异议

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

Fluent MyBatis动态SQL,写SQL更爽

数据准备

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

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

代码生成

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

  1. public class Generator {
  2. static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8";
  3. /**
  4. * 生成代码的package路径
  5. */
  6. static final String basePackage = "cn.org.fluent.mybatis.dynamic";
  7.  
  8. /**
  9. * 使用 test/resource/init.sql文件自动初始化测试数据库
  10. */
  11. @BeforeAll
  12. static void runDbScript() {
  13. DataSourceCreatorFactory.create("dataSource");
  14. }
  15.  
  16. @Test
  17. void test() {
  18. FileGenerator.build(Nothing.class);
  19. }
  20.  
  21. @Tables(
  22. /** 数据库连接信息 **/
  23. url = url, username = "root", password = "password",
  24. /** Entity类parent package路径 **/
  25. basePack = basePackage,
  26. /** Entity代码源目录 **/
  27. srcDir = "src/main/java",
  28. /** 如果表定义记录创建,记录修改,逻辑删除字段 **/
  29. gmtCreated = "gmt_created", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
  30. /** 需要生成文件的表 ( 表名称:对应的Entity名称 ) **/
  31. tables = @Table(value = {"student"})
  32. )
  33.  
  34. public static class Nothing {
  35. }
  36. }

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

在 WHERE 条件中使用动态条件

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

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

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

mybatis动态 SQL写法

  1. <select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">
  2. select
  3. <include refid="Base_Column_List" />
  4. from student
  5. <where>
  6. <if test="name != null and name !=''">
  7. and name like concat('%', #{name}, '%')
  8. </if>
  9. <if test="sex != null">
  10. and sex=#{sex}
  11. </if>
  12. </where>
  13. </select>

fluent mybatis动态写法

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

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

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

测试

  1. @SpringBootTest(classes = AppMain.class)
  2. public class StudentDaoImplTest extends Test4J {
  3. @Autowired
  4. StudentDao studentDao;
  5.  
  6. @DisplayName("只有名字时的查询")
  7. @Test
  8. void selectByNameOrEmail_onlyName() {
  9. studentDao.selectByNameOrEmail("明", null);
  10. // 验证执行的sql语句
  11. db.sqlList().wantFirstSql().eq("" +
  12. "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
  13. "FROM student " +
  14. "WHERE name LIKE ?",
  15. StringMode.SameAsSpace);
  16. // 验证sql参数
  17. db.sqlList().wantFirstPara().eqReflect(new Object[]{"%明%"});
  18. }
  19.  
  20. @DisplayName("只有性别时的查询")
  21. @Test
  22. void selectByNameOrEmail_onlyGender() {
  23. studentDao.selectByNameOrEmail(null, false);
  24. // 验证执行的sql语句
  25. db.sqlList().wantFirstSql().eq("" +
  26. "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
  27. "FROM student " +
  28. "WHERE gender = ?",
  29. StringMode.SameAsSpace);
  30. // 验证sql参数
  31. db.sqlList().wantFirstPara().eqReflect(new Object[]{false});
  32. }
  33.  
  34. @DisplayName("姓名和性别同时存在的查询")
  35. @Test
  36. void selectByNameOrEmail_both() {
  37. studentDao.selectByNameOrEmail("明", false);
  38. // 验证执行的sql语句
  39. db.sqlList().wantFirstSql().eq("" +
  40. "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
  41. "FROM student " +
  42. "WHERE name LIKE ? " +
  43. "AND gender = ?",
  44. StringMode.SameAsSpace);
  45. // 验证sql参数
  46. db.sqlList().wantFirstPara().eqReflect(new Object[]{"%明%", false});
  47. }
  48. }

在 UPDATE 使用动态更新

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

mybatis xml写法

  1. <update id="updateByPrimaryKeySelective" parameterType="...">
  2. update student
  3. <set>
  4. <if test="name != null">
  5. `name` = #{name,jdbcType=VARCHAR},
  6. </if>
  7. <if test="phone != null">
  8. phone = #{phone,jdbcType=VARCHAR},
  9. </if>
  10. <if test="email != null">
  11. email = #{email,jdbcType=VARCHAR},
  12. </if>
  13. <if test="gender != null">
  14. gender = #{gender,jdbcType=TINYINT},
  15. </if>
  16. <if test="gmtModified != null">
  17. gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
  18. </if>
  19. </set>
  20. where id = #{id,jdbcType=INTEGER}
  21. </update>
  22.  

fluent mybatis实现

  1. @Repository
  2. public class StudentDaoImpl extends StudentBaseDao implements StudentDao {
  3. /**
  4. * 根据主键更新非空属性
  5. *
  6. * @param student
  7. * @return
  8. */
  9. @Override
  10. public int updateByPrimaryKeySelective(StudentEntity student) {
  11. return super.defaultUpdater()
  12. .update.name().is(student.getName(), If::notBlank)
  13. .set.phone().is(student.getPhone(), If::notBlank)
  14. .set.email().is(student.getEmail(), If::notBlank)
  15. .set.gender().is(student.getGender(), If::notNull)
  16. .end()
  17. .where.id().eq(student.getId()).end()
  18. .execute(super::updateBy);
  19. }
  20. }

测试

  1. @SpringBootTest(classes = AppMain.class)
  2. public class StudentDaoImplTest extends Test4J {
  3. @Autowired
  4. StudentDao studentDao;
  5.  
  6. @Test
  7. void updateByPrimaryKeySelective() {
  8. StudentEntity student = new StudentEntity()
  9. .setId(1L)
  10. .setName("test")
  11. .setPhone("13866668888");
  12. studentDao.updateByPrimaryKeySelective(student);
  13. // 验证执行的sql语句
  14. db.sqlList().wantFirstSql().eq("" +
  15. "UPDATE student " +
  16. "SET gmt_modified = now(), " +
  17. "name = ?, " +
  18. "phone = ? " +
  19. "WHERE id = ?",
  20. StringMode.SameAsSpace);
  21. // 验证sql参数
  22. db.sqlList().wantFirstPara().eqReflect(new Object[]{"test", "13866668888", 1L});
  23. }
  24. }

choose 标签

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

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

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

mybatis xml实现

  1. <select id="selectByIdOrName" resultMap="BaseResultMap" parameterType="...">
  2. select
  3. <include refid="Base_Column_List" />
  4. from student
  5. <where>
  6. <choose>
  7. <when test="id != null">
  8. and id=#{id}
  9. </when>
  10. <when test="name != null and name != ''">
  11. and name=#{name}
  12. </when>
  13. <otherwise>
  14. and 1=2
  15. </otherwise>
  16. </choose>
  17. </where>
  18. </select>

fluent mybatis实现方式

  1. @Repository
  2. public class StudentDaoImpl extends StudentBaseDao implements StudentDao {
  3.  
  4. /**
  5. * 1. 当 id 有值时, 使用 id 进行查询;
  6. * 2. 当 id 没有值时, 使用 name 进行查询;
  7. * 3. 否则返回空
  8. */
  9. @Override
  10. public StudentEntity selectByIdOrName(StudentEntity student) {
  11. return super.defaultQuery()
  12. .where.id().eq(student.getId(), If::notNull)
  13. .and.name().eq(student.getName(), name -> isNull(student.getId()) && notBlank(name))
  14. .and.apply("1=2", () -> isNull(student.getId()) && isBlank(student.getName()))
  15. .end()
  16. .execute(super::findOne).orElse(null);
  17. }
  18. }

测试

  1. @SpringBootTest(classes = AppMain.class)
  2. public class StudentDaoImplTest extends Test4J {
  3. @Autowired
  4. StudentDao studentDao;
  5.  
  6. @DisplayName("有 ID 则根据 ID 获取")
  7. @Test
  8. void selectByIdOrName_byId() {
  9. StudentEntity student = new StudentEntity();
  10. student.setName("小飞机");
  11. student.setId(1L);
  12.  
  13. StudentEntity result = studentDao.selectByIdOrName(student);
  14. // 验证执行的sql语句
  15. db.sqlList().wantFirstSql().eq("" +
  16. "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
  17. "FROM student " +
  18. "WHERE id = ?",
  19. StringMode.SameAsSpace);
  20. // 验证sql参数
  21. db.sqlList().wantFirstPara().eqReflect(new Object[]{1L});
  22. }
  23.  
  24. @DisplayName("没有 ID 则根据 name 获取")
  25. @Test
  26. void selectByIdOrName_byName() {
  27. StudentEntity student = new StudentEntity();
  28. student.setName("小飞机");
  29. student.setId(null);
  30.  
  31. StudentEntity result = studentDao.selectByIdOrName(student);
  32. // 验证执行的sql语句
  33. db.sqlList().wantFirstSql().eq("" +
  34. "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
  35. "FROM student " +
  36. "WHERE name = ?",
  37. StringMode.SameAsSpace);
  38. // 验证sql参数
  39. db.sqlList().wantFirstPara().eqReflect(new Object[]{"小飞机"});
  40. }
  41.  
  42. @DisplayName("没有 ID 和 name, 返回 null")
  43. @Test
  44. void selectByIdOrName_null() {
  45. StudentEntity student = new StudentEntity();
  46. StudentEntity result = studentDao.selectByIdOrName(student);
  47. // 验证执行的sql语句
  48. db.sqlList().wantFirstSql().eq("" +
  49. "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
  50. "FROM student " +
  51. "WHERE 1=2",
  52. StringMode.SameAsSpace);
  53. // 验证sql参数
  54. db.sqlList().wantFirstPara().eqReflect(new Object[]{});
  55. }
  56. }

参考

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号