MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用
假如我们有一张user表,现需要通过user_name查出所有数据
@ApiModel(description = "用户实体")
public class User {
/**
* serialVersionUID
*/
@ApiModelProperty(value = "主键")
private Integer id;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "真实昵称")
private String realname;
@ApiModelProperty(value = "性别")
private String sex;
@ApiModelProperty(value = "工号")
private String jobNum;
@ApiModelProperty(value = "状态 0 启用 1 禁用")
private Integer isDel;
@ApiModelProperty(value = "部门")
private Integer departmentId;
private String departmentName;
@ApiModelProperty(value = "密码")
private String password;// 用户密码
@ApiModelProperty(value = "手机")
private String mobile;// 手机
private Integer createUser;
private Integer editUser;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date gmtCreate;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date gmtUpdate;
}
QueryWrapper
最基础的使用方式是这样
// 查询条件构造器
// 查询操作
List<User > users = userMapper.selectList(new QueryWrapper<User>()
.eq("user_name", id));
然后我们可以引入lambda,避免我们在代码中写类似的于user_name的硬编码
List<User> user=userMapper.selectList(new QueryWrapper<User>()
.lambda()
.eq(User::getuserName, userName));
LambdaQueryWrapper
为了简化lambda的使用,我们可以改写成LambdaQueryWrapper构造器,语法如下:
List<User> users=userMapper.selectList( new LambdaQueryWrapper<User>()
.lambda()
.eq(User::getuserName, userName))