mybtais,是一款优秀的持久层框架,而MybatisPlus则是在mybatis基础上只做增强,不做改变,简称MP,记录一下MP中常用的查询方法。
QueryWrapper与UpdateWrapper共有方法
方法名 |
说明 |
allEq |
基于map内容等于= |
eq |
等于= |
ne |
不等于<> != |
gt |
大于 > |
ge |
大于等于 >= |
lt |
小于 < |
le |
小于等于 <= |
between |
BETWEEN 条件语句 |
notBetween |
NOT BETWEEN 条件语句 |
like |
模糊查询 LIKE ‘%值%’ |
notLike |
Not LIKE ‘%值%’ |
likeRight |
LIKE ‘值%’ |
isNull |
NULL值查询 |
isNotNull |
NOT NULL值查询 |
in |
IN 查询 |
notIn |
NOT IN查询 |
isSql |
IN查询(SQL注入式) |
NotInSql |
NOT IN查询(SQL注入式) |
groupBy |
分支 GROUP BY |
orderByAsc |
排序 ORDER BY ASC |
orderByDesc |
排序 ORDER BY DESC |
orderBy |
排序 ORDER BY |
having |
HAVING 关键词(sql注入式) |
or |
or 拼接 |
apply |
拼接自定义内容(sql注入式) |
last |
拼接在最后(sql注入式) |
exists |
EXISTS 条件语句(sql注入式) |
notExists |
NOT EXISTS 条件语句(sql注入式) |
and(Function) |
AND (嵌套内容) |
or(Function) |
OR (嵌套内容) |
nested(Function) |
(嵌套内容) |
QueryWrapper特有方法
方法名 |
说明 |
select |
指定SQL查询字段,例如:id、name、age |
UpdateWrapper特有方法
方法名 |
说明 |
SQL SET 字段(一个字段使用一次) |
|
实例
package com.xiao.permission_system;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.mysql.cj.util.StringUtils;
import com.xiao.permission_system.entity.UserInfo;
import com.xiao.permission_system.mapper.UserInfoMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PermissionSystemApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
private UserInfoMapper userInfoMapper;
@Test
public void selectById() {
UserInfo userInfo = userInfoMapper.selectById(123);
System.out.println(userInfo);
}
@Test
public void selectByIds() {
List<Long> ids = Arrays.asList(123L,124L,125L);
List<UserInfo> userInfo = userInfoMapper.selectBatchIds(ids);
System.out.println(userInfo);
}
@Test
public void selectByWrapper() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.like("username","娟").lt("age",30);
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper2() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.like("username","娟").between("age",20,30).isNotNull("email");
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper3() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.likeRight("username","肖")
.or().ge("age",20).orderByDesc("age").orderByAsc("id");
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper4() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2019-10-07")
.inSql("parent_id","select id from user where username like '王%'");
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper5() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.likeRight("username","王")
.and(wq->wq.lt("age",40).or().isNotNull("email"));
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper6() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.likeRight("username","王")
.and(wq->wq.lt("age",40).gt("age",20).or().isNotNull("email"));
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper8() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.in("age",Arrays.asList(20,30,40));
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper9() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.in("age",Arrays.asList(20,30,40)).last("limit 1");
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper10() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.select("id","username").like("username","雨").lt("age",40);
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapper11() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.like("username","雨").lt("age",40)
.select(UserInfo.class,info->!info.getColumn().equals("create_time")&&
!info.getColumn().equals("parent_id"));
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
public void testCondition() {
String username = "王";
String email = "";
condition(username,email);
}
private void condition(String username,String email){
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.likeRight(StringUtils.isNotBlank(username),"name",username)
.like(StringUtils.isNotBlank(email),"email",email);
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperEntity() {
UserInfo whereUser = new UserInfo();
whereUser.setUsername("xiaojuan");
whereUser.setAge(22);
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>(whereUser);
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperAllEq() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
Map<String, Object> params = new HashMap<String, Object>();
params.put("nuserame","xiaojuan");
params.put("age",null);
queryWrapper.allEq(params);
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperAllEq2() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
Map<String, Object> params = new HashMap<String, Object>();
params.put("nuserame","xiaojuan");
params.put("age",null);
queryWrapper.allEq((k,v)->!k.equals("name"),params);
List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperMaps() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.like("name","肖").lt("age",40);
List<Map<String,Object>> userInfoList = userInfoMapper.selectMaps(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperMaps2() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.select("avg(age) avg_age","min(min) min_age","max(age) max_age")
.groupBy("parent_id").having("sum(age)<{0}",500);
List<Map<String,Object>> userInfoList = userInfoMapper.selectMaps(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperObjs() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.select("id","name").like("name","肖").lt("age",40);
List<Object> userInfoList = userInfoMapper.selectObjs(queryWrapper);
userInfoList.forEach(System.out::println);
}
@Test
public void selectByWrapperCount() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.like("name","肖").lt("age",40);
Integer count = userInfoMapper.selectCount(queryWrapper);
System.out.println(count);
}
@Test
public void selectByWrapperSelectOne() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.like("name","肖").lt("age",40);
UserInfo user = userInfoMapper.selectOne(queryWrapper);
System.out.println(user);
}
@Test
public void selectLambda() {
LambdaQueryWrapper<UserInfo> lambda = new LambdaQueryWrapper<UserInfo>();
lambda.like(UserInfo::getUsername,"娟").lt(UserInfo::getAge,40);
List<UserInfo> userInfoList = userInfoMapper.selectList(lambda);
userInfoList.forEach(System.out::println);
}
@Test
public void selectLambd2() {
LambdaQueryWrapper<UserInfo> lambda = new LambdaQueryWrapper<UserInfo>();
lambda.like(UserInfo::getUsername,"娟")
.and(lqw->lqw.lt(UserInfo::getAge,40).or().isNotNull(UserInfo::getEmail));
List<UserInfo> userInfoList = userInfoMapper.selectList(lambda);
userInfoList.forEach(System.out::println);
}
@Test
public void selectLambd3() {
List<UserInfo> userInfoList = new LambdaQueryChainWrapper<UserInfo>(userInfoMapper)
.like(UserInfo::getUsername,"娟").ge(UserInfo::getAge,20).list();
userInfoList.forEach(System.out::println);
}
}
参考:https://www.cnblogs.com/steven0325/p/12463138.html