MybatisPlus QueryWrapper LambdaQueryWrapper LambdaQueryChainWrapper
所属分类 mybatis-plus
浏览量 662
QueryWrapper LambdaQueryWrapper LambdaQueryChainWrapper
SqlSessionFactory sqlSessionFactory = Util.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession(true);
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = new User(1,"cat",3);
userMapper.insert(user);
user = new User(2,"tiger",7);
userMapper.insert(user);
user = new User(3,"dog",5);
userMapper.insert(user);
List users = userMapper.selectList(null);
for(User item:users){
System.out.println(item);
}
QueryWrapper queryWrapper = new QueryWrapper<>();
// %hello%
queryWrapper.like("user_name","hello");
userMapper.selectList(queryWrapper);
queryWrapper = new QueryWrapper<>();
// %hello
queryWrapper.likeLeft("user_name","hello");
userMapper.selectList(queryWrapper);
queryWrapper = new QueryWrapper<>();
// hello%
queryWrapper.likeRight("user_name","hello");
userMapper.selectList(queryWrapper);
queryWrapper = new QueryWrapper<>();
queryWrapper.between("age",1,10);
queryWrapper.or();
queryWrapper.eq("user_name","tiger");
queryWrapper.orderByAsc("age");
// age BETWEEN ? AND ? OR user_name = ?
userMapper.selectList(queryWrapper);
// LambdaQueryWrapper
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
// 避免写错字段名
lambdaQueryWrapper.eq(User::getName,"tiger");
user = userMapper.selectOne(lambdaQueryWrapper);
System.out.println(user);
// LambdaQueryChainWrapper
user = new LambdaQueryChainWrapper<>(userMapper)
.eq(User::getName, "cat")
.one();
System.out.println(user);
session.close();
SELECT user_id AS id,user_name AS name,age FROM t_user WHERE (age BETWEEN ? AND ? OR user_name = ?) ORDER BY age ASC
SELECT user_id AS id,user_name AS name,age FROM t_user WHERE (user_name = ?)
https://gitee.com/dyyx/hellocode/blob/master/demo/mp/mp-nospring-demo/src/main/java/com/dyyx/nospring/QueryWrapperDemo.java
上一篇
下一篇
maven插件编译proto文件
spring data JPA
MybatisPlus简介
mybatis plus 常用注解
mybatis 日志实现要点
Aviator使用说明