mybatis-plus-Chain
本文与官网大多数一致,只是在示例中加入了一些实际验证的代码,日志可能和与官网略有出入(预估是因为版本原因)
本文测试代码中mybatis-plus依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
Chain 是 Mybatis-Plus 提供的一种链式编程风格,它允许开发者以更加简洁和直观的方式编写数据库操作代码。Chain 分为 query 和 update 两大类,分别用于查询和更新操作。每类又分为普通链式和 lambda 链式两种风格,其中 lambda 链式提供了类型安全的查询条件构造,但不支持 Kotlin。
下面分别对Service,Mapper,entity这三个不同层操作链式操作数据库做演示:
@Data
@TableName("user")
public class UserPO implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(updateStrategy = FieldStrategy.NOT_NULL)
private String username;
private Integer age;
private Integer gender;
@TableField(fill = FieldFill.UPDATE)
private String email;
private String phoneNumber;
// @TableLogic(value="1",delval = "0")
private String state;
}
@Resource
private IUserService userService;//IUserService实现类继承了Iservice
@Resource
private UserMapper userMapper;//UserMapper实现类继承了BaseMapper
/*************************************************************************/
@Test
public void testQuery() {
userService.query().eq("username", "张三").list();
}
// ==> Preparing: SELECT id,username,age,gender,email,phone_number,state FROM user WHERE (username = ?)
// ==> Parameters: 张三(String)
// <== Columns: id, username, age, gender, email, phone_number, state
// <== Row: 1, 张三, 22, 1, zhangsan@email.com, 1890000001, null
// <== Total: 1
/*************************************************************************/
@Test
public void testUpdate() {
UserPO userPO = new UserPO();
userPO.setUsername("张三");
userPO.setAge(44);
userService.lambdaUpdate().set(true, UserPO::getAge, userPO.getAge()).eq(UserPO::getUsername, userPO.getUsername()).update();
}
// ==> Preparing: UPDATE user SET age=? WHERE (username = ?)
// ==> Parameters: 44(Integer), 张三(String)
// <== Updates: 1
/*************************************************************************/
@Test
public void testQuery2() {
queryChain(userMapper).eq("username","张三").list();
}
// ==> Preparing: SELECT id,username,age,gender,email,phone_number,state FROM user WHERE (username = ?)
// ==> Parameters: 张三(String)
// <== Columns: id, username, age, gender, email, phone_number, state
// <== Row: 1, 张三, 44, 1, zhangsan@email.com, 1890000001, null
// <== Total: 1
/*************************************************************************/
@Test
public void testUpdate2() {
updateChain(userMapper).eq("username","张三").set("age",44).update();
}
// ==> Preparing: UPDATE user SET age=? WHERE (username = ?)
// ==> Parameters: 44(Integer), 张三(String)
// <== Updates: 1
/*************************************************************************/
@Test
public void testQuery33() {
queryChain(UserPO.class).eq("username","张三").list();
}
//==> Preparing: SELECT id,username,age,gender,email,phone_number,state FROM user WHERE (username = ?)
// ==> Parameters: 张三(String)
// <== Columns: id, username, age, gender, email, phone_number, state
// <== Row: 1, 张三, 44, 1, zhangsan@email.com, 1890000001, null
// <== Total: 1
/*************************************************************************/
@Test
public void testUpdate3() {
updateChain(UserPO.class).eq("username","张三").set("age",55).update();
}
// ==> Preparing: UPDATE user SET age=? WHERE (username = ?)
// ==> Parameters: 55(Integer), 张三(String)
// <== Updates: 1
关于操作符说明后期单独说明。
评论区