mybatisplus的查询所有_MybatisPlus的各种查询方式!

基本查询

/**

* id=?

*/

@Test

void selectById() {

User user = mapper.selectById(1087982257332887553L);

System.out.println(user);

}

/**

* id IN ( ? , ? , ? )

*/

@Test

void retrieveByIds() {

List users = mapper.selectBatchIds(Arrays.asList(8, 9, 10));

users.forEach(System.out::println);

}

/**

* 根据map查询,key为column,value为具体的值

* name = ? AND age = ?

*/

@Test

void retrieveByMap() {

Map map = new HashMap<>();

map.put("name", "大boss");

map.put("age", "40");

List users = mapper.selectByMap(map);

users.forEach(System.out::println);

}

条件构造器查询

/**

* 名字中包含雨并且年龄小于40

* name like '%雨%' and age<40

*/

@Test

void selectByWrapper1() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.like("name", "雨")

.lt("age", 40);

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 名字中包含雨年并且龄大于等于20且小于等于40并且email不为空

* name like '%雨%' and age between 20 and 40 and email is not null

*/

@Test

void selectByWrapper2() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.like("name", "雨")

.between("age", 20, 40)

.isNotNull("email");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 名字为王姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列

* name like '王%' or age>=25 order by age desc,id asc

*/

@Test

void selectByWrapper3() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.likeRight("name", "王")

.or().ge("age", 25)

.orderByDesc("age")

.orderByAsc("id");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 创建日期为2019年2月14日并且直属上级为名字为王姓

* date_format(create_time,'%Y-%m-%d')='2019-02-14' and

* manager_id in (select id from user where name like '王%')

*/

@Test

void selectByWrapper4() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14")

.inSql("manager_id", "select id from user where name like '王%'");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* and中传入lambda

*

* 名字为王姓并且(年龄小于40或邮箱不为空)

* name like '王%' and (age<40 or email is not null)

*/

@Test

void selectByWrapper5() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.likeRight("name", "王").

and(wq -> wq.lt("age", 40)

.or().isNotNull("email"));

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 名字为王姓或者(年龄小于40并且年龄大于20并且邮箱不为空)

* name like '王%' or (age<40 and age>20 and email is not null)

*/

@Test

void selectByWrapper6() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.likeRight("name", "王").

or(wq -> wq.lt("age", 40)

.gt("age", 20)

.isNotNull("email"));

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* ( 年龄小于40或邮箱不为空)并且名字为王姓

* (age<40 or email is not null) and name like '王%'

*/

@Test

void selectByWrapper7() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.nested(wq -> wq.lt("age", 40)

.or().isNotNull("email"))

.likeRight("name", "王");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 年龄为30、31、34、35

*/

@Test

void selectByWrapper8() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.in("age", Arrays.asList(30, 31, 34, 35));

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 只返回满足条件的其中一条语句即可 limit 1

*/

@Test

void selectByWrapper9() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.in("age", Arrays.asList(30, 31, 34, 35)).last("limit 1");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

指定列与排除列

/**

* 查询指定列

* SELECT id,name FROM user WHERE (name LIKE ?)

*/

@Test

void selectPart1() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.select("id", "name").like("name", "雨");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

/**

* 排除指定列

* SELECT id,name,age,manager_id FROM user WHERE (name LIKE ?)

*/

@Test

void selectPart2() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.select(User.class, info -> !info.getColumn().equals("create_time") &&

!info.getColumn().equals("email")

).like("name", "雨");

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

Condition处理参数

@Test

void testCondition(){

String name = "王";

String email = "";

//withOutCondition(name,email);

withCondition(name,email);

}

private void withOutCondition(String name,String email){

QueryWrapper wrapper = new QueryWrapper<>();

if(StringUtils.isNotBlank(name)){

wrapper.like("name",name);

}

if(StringUtils.isNotBlank(email)){

wrapper.like("email",email);

}

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

private void withCondition(String name,String email){

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.like(StringUtils.isNotBlank(name),"name",name)

.like(StringUtils.isNotBlank(email),"email",email);

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

以实体作为参数

/**

* WHERE name=? AND age=?

* 以实体为参数 和普通设置参数 互不干扰, 都会存在, 使用时需慎重

* 以实体为参数,默认是等值的,需要使用@TableField注解,并注明sqlCondition

*/

@Test

void selectByWrapperEntity(){

User user = new User();

user.setName("天乔巴夏");

user.setAge(20);

QueryWrapper wrapper = new QueryWrapper<>(user);

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

@TableField(value = "name",condition = SqlCondition.LIKE) //指定字段名

private String name;

AllEq的使用

@Test

void selectByWrapperAllEq(){

QueryWrapper wrapper = new QueryWrapper<>();

Map params = new HashMap<>();

params.put("name","天乔巴夏");

params.put("age",null); // age is null , 可以通过 下面这句设置 false

wrapper.allEq(params,false);

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

@Test

void selectByWrapperAllEq2() {

QueryWrapper wrapper = new QueryWrapper<>();

Map params = new HashMap<>();

params.put("name", "天乔巴夏");

params.put("age", null); // age is null , 可以通过 下面这句设置 false

wrapper.allEq((k, v) -> !k.equals("name"), params, false);

List users = mapper.selectList(wrapper);

users.forEach(System.out::println);

}

自定义查询

注解形式

public interface UserMapper extends BaseMapper {

@Select("select * from user ${ew.customSqlSegment}")

List selectAll(@Param(Constants.WRAPPER)Wrapper wrapper);

}

/**

* 测试自定义方法 注解

*/

@Test

void selectByCustomAnno(){

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.eq("name","天乔巴夏");

List users = mapper.selectAll(wrapper);

users.forEach(System.out::println);

}

xml形式

public interface UserMapper extends BaseMapper {

List selectAll2(@Param(Constants.WRAPPER)Wrapper wrapper);

}

select * from user ${ew.customSqlSegment}

其他的查询

/**

* 优雅返回指定字段的结果

*/

@Test

void selectByWrapperMaps1() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.like("name", "雨")

.lt("age", 40)

.select("id","name");

List> mapList = mapper.selectMaps(wrapper);

mapList.forEach(System.out::println);

}

/**

* 按照直属上级分组,查询每组的平均年龄、最大年龄、最小年龄。

* 并且只取年龄总和小于500的组。

*

* select avg(age) avg_age,min(age) min_age,max(age) max_age

* from user

* group by manager_id

* having sum(age) <500

*/

@Test

void selectByWrapperMaps2() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")

.groupBy("manager_id").having("sum(age)

List> mapList = mapper.selectMaps(wrapper);

mapList.forEach(System.out::println);

}

/**

* 只返回一列

*/

@Test

void selectByWrapperObjs() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")

.groupBy("manager_id").having("sum(age)

List objects = mapper.selectObjs(wrapper);

objects.forEach(System.out::println);

}

/**

* 查个数

*/

@Test

void selectCount() {

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.like("name","雨");

Integer cnt = mapper.selectCount(wrapper);

System.out.println(cnt);

}

/**

* 查一个实体

*/

@Test

void selectOne(){

QueryWrapper wrapper = new QueryWrapper<>();

wrapper.eq("name","天乔巴夏");

User user = mapper.selectOne(wrapper);

System.out.println(user);

}

/**

* lambda构造,编译时检查字段信息,防止误写

*/

@Test

void selectLambda(){

//LambdaQueryWrapper lambda = new QueryWrapper().lambda();

//LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();

LambdaQueryWrapper lambdaQuery = Wrappers.lambdaQuery();

lambdaQuery.like(User::getName,"雨").lt(User::getAge,40);

// where name like '%雨%'

List users = mapper.selectList(lambdaQuery);

users.forEach(System.out::println);

}

你可能感兴趣的:(mybatisplus的查询所有_MybatisPlus的各种查询方式!)