这篇文章主要讲解了“Fluent mybatis怎么实现动态sql”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Fluent MyBatis怎么实现动态SQL”吧!目录数据准备代码生成在 W
这篇文章主要讲解了“Fluent mybatis怎么实现动态sql”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Fluent MyBatis怎么实现动态SQL”吧!
数据准备
代码生成
在 WHERE 条件中使用动态条件
在 UPDATE 使用动态更新
choose 标签
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"; static final String basePackage = "cn.org.fluent.mybatis.dynamic"; @BeforeAll static void runDbScript() { DataSourceCreatorFactory.create("dataSource"); } @Test void test() { FileGenerator.build(Nothing.class); } @Tables( url = url, username = "root", passWord = "password", basePack = basePackage, srcDir = "src/main/java", gmtCreated = "gmt_created", gmtModified = "gmt_modified", logicDeleted = "is_deleted", tables = @Table(value = {"student"}) ) public static class Nothing { }}
编译项目,ok,下面我们开始动态SQL构造旅程
在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动态写法
@Repositorypublic class StudentDaoImpl extends StudentBaseDao implements StudentDao { @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}); }}
只更新有变化的字段, 空值不更新
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实现
@Repositorypublic class StudentDaoImpl extends StudentBaseDao implements StudentDao { @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}); }}
在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实现方式
@Repositorypublic class StudentDaoImpl extends StudentBaseDao implements StudentDao { @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怎么实现动态SQL”的内容了,经过本文的学习后,相信大家对Fluent MyBatis怎么实现动态SQL这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: Fluent MyBatis怎么实现动态SQL
本文链接: https://www.lsjlt.com/news/299660.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0