一、insert
User user = new User();
user.setId(1);
user.setAge(18);
int rows = userMapper.insert(user);
二、select
1、普通查询
User user = userMapper.selectById(11);
2、批量查询
List<Long> ids = Arrays.asList(123L,124L,125L);
List<User> userList = userMapper.selectBatchIds(ids);
3、名字包含小雨并且年龄小于30
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("username","小雨").lt("age",30);
List<User> userList = userMapper.selectList(queryWrapper);
4、名字包含小雨并且年龄大于等于20且小于等于40并且email不为空
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("username","小雨").between("age",20,30).isNotNull("email");
List<User> userList = userMapper.selectList(queryWrapper);
5、名字姓肖或者年龄大雨等于20,按照年龄降序排列,年龄相同按照id生序排列
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.likeRight("username","肖")
.or().ge("age",20).orderByDesc("age").orderByAsc("id");
List<User> userInfoList = userMapper.selectList(queryWrapper);
6、创建日期为2019年10月2日并且直属上级名字为王姓
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2019-10-07")
.inSql("parent_id","select id from user where username like '王%'");
List<User> userList = userMapper.selectList(queryWrapper);
7、只取指定的字段
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.select("id","username");
List<User> userList = userMapper.selectList(queryWrapper);
8、只获取一条、返回数量
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("name","小雨").lt("age",30);
User user = userMapper.selectOne(queryWrapper);
int count = userMapper.selectCount(queryWrapper);
三、update
1、根据id进行更新
//把id为1的用户年龄更新为18,其他属性不变
User user = new User();
user.setId(1);
user.setAge(18);
int rows = userMapper.updateById(user);
2、条件构造器作为参数进行更新
//把名字为rhb的用户年龄更新为18,其他属性不变
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","rhb");
User user = new User();
user.setAge(18);
int rows = userMapper.update(user, updateWrapper);
3、lambda构造器
//把名字为rhb的用户年龄更新为18,其他属性不变
LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, 18);
int rows = userMapper.update(null, lambdaUpdateWrapper);
四、delete
1、根据id进行删除
int rows = userMapper.deleteById(1351456313578713090L);
2、根据map删除
Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", 25);
int rows = userMapper.deleteByMap(map);
3、以条件构造器为参数删除
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("name","张三");
wrapper.eq("age",25);
int rows = userMapper.delete(wrapper);
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
wrapper.eq(User::getName,"张三").eq(User::getAge,25);
int rows = userMapper.delete(wrapper);
setSqlSelect SELECT 查询字段 举例
where WHERE 语句,拼接 + WHERE 条件
and AND 语句,拼接 + AND 字段=值
andNew AND 语句,拼接 + AND (字段=值)
or OR 语句,拼接 + OR 字段=值 .eq(" name “,” 木 子 “). or ( ). eq (” id ", 1 ) => name=“木子” or id=1 注意事项:主动调用or表示下一个方法不是and连接,不调用默认为使用and连接
orNew OR 语句,拼接 + OR (字段=值)
eq 等于= .eq(“name”,“木子”) => name=“木子”
allEq 基于 map 内容等于=
ne 不等于<>
gt 大于>
ge 大于等于>=
lt 小于<
le 小于等于<=
like 模糊查询 LIKE .like(“name”,“木子”) => name like ‘%木子%’
notLike NOT LIKE模糊查询
in IN 查询 .in(“age”,{1,2,3}) => age in (1,2,3)
notIn NOT IN 查询
isNull NULL 值查询
isNotNull IS NOT NULL
groupBy 分组 GROUP BY
having HAVING 关键词
orderBy 排序 ORDER BY
orderAsc Asc 排序 ORDER BY
orderDesc DESC 排序 ORDER BY
exists EXISTS 条件语句
notExists NOT EXISTS 条件语句
between BETWEEN 条件语句 .between (“age”,1,2) => age between 1 and 2
notBetween NOT BETWEEN 条件语句
addFilter 自由拼接 SQL
last 拼接在最后,例如:last(“LIMIT 1”)