Python 官方文档:入门教程 => 点击学习
目录数据准备代码生成在 WHERE 条件中使用动态条件在 UPDATE 使用动态更新choose 标签参考mybatis 令人喜欢的一大特性就是动态 sql。在使用
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动态写法
@Repository
public 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的实现方式至少有下面的好处
@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实现
@Repository
public 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 具有唯一性, 查询一个学生
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 {
@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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Fluent MyBatis实现动态SQL
本文链接: https://www.lsjlt.com/news/131883.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0