AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
QueryWrapper : Entity 对象封装操作类,不是用lambda语法
UpdateWrapper : Update 条件封装,用于Entity对象更新操作
AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrapper
注意:以下条件构造器的方法入参中的 column 均表示数据库字段
例如:查询大于80岁的用户
//查询大于80岁的用户
@Test
public void testSelectDemo1(){
//构造条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
//age>80
wrapper.gt("age",80);
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
SQL语句 : SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age > ?
注意:seletOne返回的是一条实体记录,当出现多条时会报错
例如:查询用户名称为jack 并且年龄为20
@Test
public void testSelectDemo2(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
//用户名为jack
wrapper.eq("name","Tom");
wrapper.eq("age",28);
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
SQL : SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name = ? AND age = ?
包含大小边界
例如: 查询年龄为20到40的用户
@Test
public void testSelectDemo3(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
//年龄在20-40
wrapper.between("age",20,40);
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
SQL : SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age BETWEEN ? AND ?
@Test
public void testSelectList(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
Map<String,Object> map = new HashMap<>();
map.put("id",2);
map.put("name","Jack");
map.put("age",20);
wrapper.allEq(map);
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
SQL语句: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name = ? AND id = ? AND age = ?
selectMaps返回Map集合列表
@Test
public void testSelectMaps(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.notLike("name","e")
.likeRight("email","t");
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
}
SQL语句: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name NOT LIKE ? AND email LIKE ?
in、notIn:
notIn(“age”,{1,2,3})—>age not in (1,2,3)
notIn(“age”, 1, 2, 3)—>age not in (1,2,3)
inSql、notinSql:可以实现子查询
@Test
public void testSelectObjs(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.inSql("id","select id from user where id < 3");
List<Object> objects = userMapper.selectObjs(wrapper);
objects.forEach(System.out::println);
}
SQL: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND id IN (select id from user where id < 3)
注意:这里使用的是 UpdateWrapper
不调用or则默认为使用 and 连
@Test
public void testUpdate1(){
//修改值
User user = new User();
user.setAge(99);
user.setName("Andy");
//修改条件
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.like("name","h")
.or()
.between("age",20,30);
int i = userMapper.update(user, userUpdateWrapper);
System.out.println(i);
}
SQL语句: UPDATE user SET name=?, age=?, update_time=? WHERE deleted=0 AND name LIKE ? OR age BETWEEN ? AND ?
// A code block
var foo = 'bar';
直接拼接到 sql 的最后
注意:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
@Test
public void testLast(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.last("limit 1");
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
SQL语句: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 limit 1
@Test
public void testSelectDemo4(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
//查询指定的列
wrapper.select("id","name","age");
wrapper.like("name","a");
wrapper.orderByDesc("age");
List<User> users = userMapper.selectList(wrapper);
System.out.println(users);
}
SQL语句: SELECT id,name,age FROM user WHERE deleted=0 AND name LIKE ? ORDER BY age DESC
最终的sql会合并 user.setAge(),以及 userUpdateWrapper.set() 和 setSql() 中 的字段
@Test
public void testUpdateSet(){
//修改值
User user = new User();
user.setAge(99);
//修改条件
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.like("name","h")
.set("name","李老头")
.setSql("email='[email protected]");
int i = userMapper.update(user,userUpdateWrapper);
}