开启驼峰命名
mybatis-plus.configuration.map-underscore-to-camel-case=true
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_user")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
#表映射全局配置
mybatis-plus.global-config.db-config.table-prefix=t_
场景一
当数据库字段和表实体类的属性不一致时,我们可以使用@TableField()注解改变字段和属性的映射,让注解中的名称和表字段保持一致。
如:此时数据库字段的名称我们改为username,在根据实体类的属性拼接SQL的使用,就会使用@TableField()中指定的名称username进行拼接,完成查询
@TableField("username")
private String name;
SELECT id,username AS name,email FROM t_user
场景二
SELECT id,username AS name,age,email,desc FROM t_user
Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘desc FROM t_user’ at line 1
desc
,就可以解决这个问题@TableField("`desc`")
private String desc;
@TableField(select = false)
private Integer age;
在实际开发中,有些字段不需要数据库存储,但是却需要展示,需要展示也就是意味着实体类中需要存在这个字段,我们称这些实体类中存在但是数据库中不存在这个字段,叫做视图字段。
我们可以通过@TableField(exist=false)来去掉这个字段,不让他作为查询字段。
@TableField(exist = false)
private Integer online;
@Test
void eq() {
//1.创建条件查询对象
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//2.设置查询条件,指定查询的字段和匹配的值
queryWrapper.eq("username","Jack");
//3.进行条件查询
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
@Test
void eq2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName,"Jack");
User user = userMapper.selectOne(lambdaQueryWrapper);
System.out.println(user);
}
@Test
void isNull() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName,null);
User user = userMapper.selectOne(lambdaQueryWrapper);
System.out.println(user);
}
@Test
void isNUll2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//String username="Jone";
String username=null;
lambdaQueryWrapper.eq(username!=null,User::getName,username);
//User user = userMapper.selectOne(lambdaQueryWrapper);
List<User> user = userMapper.selectList(lambdaQueryWrapper);
System.out.println(user);
}
@Test
void allEq1() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName,"Tom");
lambdaQueryWrapper.eq(User::getAge,28);
User user = userMapper.selectOne(lambdaQueryWrapper);
System.out.println(user);
}
@Test
void allEq2() {
//1.构建条件Map
HashMap<String, Object> map = new HashMap<>();
map.put("username","Tom");
//map.put("age",28);
map.put("age",null);
//2.创建QueryWrapper对象
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//3.使用条件完成查询
queryWrapper.allEq(map,false);
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
@Test
void ne() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.ne(User::getName,"Tom");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void gt() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.gt(User::getAge,20);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void ge() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.ge(User::getAge,20);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void lt() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.lt(User::getAge,20);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void le() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.le(User::getAge,20);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void between() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.between(User::getAge,20,28);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void notBetween() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notBetween(User::getAge,20,28);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void like() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.like(User::getName,"J");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void notLike() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notLike(User::getName,"J");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void likeLeft() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.likeLeft(User::getName,"e");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void likeRight() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.likeRight(User::getName,"J");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
Test
void isNull() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.isNull(User::getDesc);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void isNotNull() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.isNotNull(User::getName);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void in() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(User::getAge, 18, 20, 21);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void in2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
ArrayList<Integer> list = new ArrayList<>();
//Collections.addAll(list,18,20,21);
list.add(18);
list.add(20);
list.add(21);
lambdaQueryWrapper.in(User::getAge, list);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void notIn() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notIn(User::getAge, 18, 20, 21);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void notIn2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list,18,20,21);
lambdaQueryWrapper.notIn(User::getAge,list);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void inSql() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.inSql(User::getAge, "18,20,21");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void inSql2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.inSql(User::getAge, "select age from t_user where age>=20");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void notInSql() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notInSql(User::getAge, "18,20,21");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void notInSql2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notInSql(User::getAge, "select age from t_user where age>20");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void groupBy() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//分组字段
queryWrapper.groupBy("age");
//查询字段
queryWrapper.select("age,count(*) as field_count");
List<User> users = userMapper.selectList(queryWrapper);
System.out.println(users);
}
@Test
void having() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//分组字段
queryWrapper.groupBy("age");
//查询字段
queryWrapper.select("age,count(*) as field_count");
//聚合字段
queryWrapper.having("field_count>1");
List<User> users = userMapper.selectList(queryWrapper);
System.out.println(users);
}
@Test
void orderByAsc() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.orderByAsc(User::getAge,User::getId);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void orderByDesc() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.orderByDesc(User::getAge,User::getId);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void orderBy() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
/**
* oderBy:设置排序字段和排序的方式
* 参数一:如果排序字段的值为null,是否还要作为排序字段参与排序
* 参数二:是否升序排序
* 参数三:排序字段
*/
lambdaQueryWrapper.orderBy(true, true, User::getAge);
lambdaQueryWrapper.orderBy(true, false, User::getId);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void func() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//可能会根据不同的情况来选择进行拼接不同的查询条件
/*lambdaQueryWrapper.func(new Consumer>() {
@Override
public void accept(LambdaQueryWrapper userLambdaQueryWrapper) {
if (true){
userLambdaQueryWrapper.eq(User::getId,1);
}else {
userLambdaQueryWrapper.ne(User::getId,1);
}
}
});*/
//使用lambda表达式
lambdaQueryWrapper.func(i -> {
if (true) {
i.eq(User::getId, 1);
} else {
i.ne(User::getId, 1);
}
});
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void and() {
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.gt(User::getAge,20).lt(User::getAge,30);
List users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void and2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName,"Jone").and(i->
i.lt(User::getAge,20).or().gt(User::getAge,30)
);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void or() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.lt(User::getAge, 20).or().gt(User::getAge, 30);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void or2() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.likeRight(User::getName,"J").or(i->i.ge(User::getAge,20).le(User::getAge,30));
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void nested() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.nested(i->i.eq(User::getName,"Tom").ne(User::getAge,23));
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void apply() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.apply("id=2");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void last() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.last("limit 0,2");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void exists() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.exists("select id from t_user where age=18");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
select id from t_user where age=18
语句能查询到结果,执行SELECT id,username AS name,age,email,
desc FROM t_user
语句,否则不执行。@Test
void notExists() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notExists("select id from t_user where age=18");
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
void select() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.select(User::getId, User::getName);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}