上节为通过id、idList、idSet、Map设置条件进行等值查询,但在实际使用中,还需要使用> <等连接符进行查询,那么此次则介绍条件构造器这一使用方法。
这个是自己学习时候记得笔记要是想详细了解可以去MP官网,上边有更详细的配置流程以及视频教学:MyBatis-Plus
public List selectByWrapper() {
// 创建方式1. new
QueryWrapper queryWrapper = new QueryWrapper<>();
// 创建方式2. Wappers.query()
QueryWrapper queryWrapper1 = Wrappers.query();
// 构造条件 : 单表查询使用and连接
queryWrapper.like("name", "a") // like 模糊查询
.lt("parentId", 5) // lt: lessThan 小于
.isNotNull("id"); // 不为空
// 以上条件均and进行连接;
// 构造条件: 单表查询使用or连接
queryWrapper1.likeRight("name", "王") // 姓王, 王%
.or() // 使用 or 进行连接
.ge("parentId", 3) // ge : great Than , 大于
.orderByDesc("id") // 根据 id 降序
.orderByAsc("parentId"); // 根据 parentId升序
// 传入参数
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperIn() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(productDate,'%Y-%m-%d)={0}", "2011-05-09")
// 添加条件,{0}为选择后面参数第0个位置,形成SQL使用?占位符,防止SQL注入
.inSql("type_id", "select type_id from type where name like '生%'");
// 添加 in 语句
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperAndOr() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "生") // like '生%'
.and(new Consumer>() {
@Override
public void accept(QueryWrapper typeQueryWrapper) {
typeQueryWrapper.lt("parentId", 2)
.or()
.isNotNull("productDate");
}
}); // and ( parentId = ? or productDate is not null)
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperOrAnd() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "生") // name like '生%'
.or(new Consumer>() {
@Override
public void accept(QueryWrapper typeQueryWrapper) {
typeQueryWrapper.lt("parentId", 20).gt("parentId", 0)
.isNotNull("productDate");
} // or (parentId < 20 and parentId > 0 and productDate is not null)
});
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperOrAnd1() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.nested(new Consumer>() {
@Override
public void accept(QueryWrapper typeQueryWrapper) {
typeQueryWrapper.lt("parentId", 5)
.or().isNotNull("productDate");
} // (parentId < 5 or productDate is not null)
}).likeRight("name", "生"); // and name like '生%'
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperInCol() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.in("parentId", Arrays.asList(2, 3, 4));
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperLimit() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.in("parentId", Arrays.asList(2, 3, 4))
.last("limit 1");// .last("") 有sql注入风险
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperSuperIdName() {
QueryWrapper queryWrapper = new QueryWrapper<>();
// 指定查询 方法1
queryWrapper.select("type_id", "name") // 只查询 type_id , name
.in("parentId", Arrays.asList(2, 3, 4))
.last("limit 1");// .last("") 有sql注入风险
// 指定查询 方法2
queryWrapper.select(Type.class, new Predicate() {
@Override
public boolean test(TableFieldInfo tableFieldInfo) {
return !tableFieldInfo.getColumn().equals("productDate")
&&
!tableFieldInfo.getColumn().equals("parentId");
}
}) // 得到查询结果时,排除 productDate 与 parentId 两条记录
.in("parentId", Arrays.asList(2, 3, 4))
.last("limit 1");// .last("") 有sql注入风险
return this.typeMapper.selectList(queryWrapper);
}
/**
* 条件构造器:condition 作用,
*/
public List selectByWrapperCondition(String name, int parentid) {
QueryWrapper queryWrapper = new QueryWrapper<>();
// 使用condition前
if (StringUtils.isNotEmpty(name)) {
queryWrapper.like("name", name);
}
if (parentid > 0) {
queryWrapper.eq("parentId", parentid);
}
// 使用condition后
queryWrapper.like(StringUtils.isNotEmpty(name), "name", name)
.eq(parentid > 0, "parentId", parentid);
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperEntity(){
Type type = new Type();
type.setName("abc");
QueryWrapper queryWrapper = new QueryWrapper<>(type);
// 注意1:重复设置name: 此时一个sql里边查询了两次name,用 and 连接
// queryWrapper.eq("name","abc");
// 注意2:实体类中在@TableField中配置condition属性,可控制在sql中使用什么条件
//@TableField(condition = SqlCondition.EQUAL)
//private int parentId;
/**
* public static final String EQUAL = "%s=#{%s}";
* public static final String NOT_EQUAL = "%s<>#{%s}";
* public static final String LIKE = "%s LIKE CONCAT('%%',#{%s},'%%')";
* public static final String LIKE_LEFT = "%s LIKE CONCAT('%%',#{%s})";
* public static final String LIKE_RIGHT = "%s LIKE CONCAT(#{%s},'%%')";
*/
// 可自行构造condition常量 : 大于 condition = "%s>#{%s}"
return this.typeMapper.selectList(queryWrapper);
}
public List selectByWrapperAllEq(){
QueryWrapper queryWrapper = new QueryWrapper<>();
// Map map = new HashMap<>();
// map.put("name","生日");
// map.put("publishDate",null);
// queryWrapper.allEq(map); // allEq方法只有一个参数时,默认 isNull 为 true
Map map = new HashMap<>();
map.put("name","生日");
map.put("publishDate","2011-05-09");
queryWrapper.allEq(new BiPredicate() {
@Override
public boolean test(String s, Object o) {
return !s.equals("publishDate");
}
},map); // 指定publishDate不进入sql
return this.typeMapper.selectList(queryWrapper);
}
public List
public List
public Integer selectWrapperCount(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","巧克力")
.lt("parentId",20);
return this.typeMapper.selectCount(queryWrapper);
}
public Type selectWrapperOne(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","巧克力")
.lt("parentId",20);
return this.typeMapper.selectOne(queryWrapper);
}
public List selectLambda(){
// 三种构造方式
// LambdaQueryWrapper lambda = new QueryWrapper().lambda();
// LambdaQueryWrapper lambdaQuery = new LambdaQueryWrapper<>();
LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery();
// 可以有效防止查询字段误写
// lambdaQueryWrapper.like(Type::getName,'巧');// ==> where name like '%雨%'
// a and (b or c)
lambdaQueryWrapper.likeRight(Type::getName,"巧").and(new Consumer>() {
@Override
public void accept(LambdaQueryWrapper typeLambdaQueryWrapper) {
typeLambdaQueryWrapper.lt(Type::getParentId,5)
.or().ge(Type::getTypeId,0);
}
}); // ==> where name like '巧%' and (parentId > 5 or type_id >= 0)
return this.typeMapper.selectList(lambdaQueryWrapper);
}
// @Select("select * from user ${ew.customSqlSegment}")
public List selectAll(@Param(Constants.WRAPPER) Wrapper wrapper);
Mapper.xml文件中自定义SQL(千万别跟我一样用select * 偷懒)也可以用注解的方式创建SQL语句
Service层代码
public List selectAllNative(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("id");
return this.typeMapper.selectAll(queryWrapper);
}