mybatis-plus条件构造器
一、mybatis-plus条件构造器
1、条件构造器关系图
1.橙色框为抽象类abstract
2.蓝色框为正常class类,可new对象
3.黄色箭头指向为父子类关系,箭头指向为父类
注意Service接口 都继承了 IService 的接口
Service
public interface SchoolStudentService extends IService<SchoolStudent> {
}
ServiceImpl
@Service
public class SchoolStudentServiceImpl extends ServiceImpl<SchoolStudentMapper, SchoolStudent> implements SchoolStudentService {
@Resource
private SchoolStudentMapper schoolStudentMapper;
}
Mapper
public interface SchoolStudentMapper extends BaseMapper<SchoolStudent> {
}
1、QueryWrapper
继承了AbstractWrapper ,自身属性内的entity实体类可作为where条件,可以通过 new QueryWrapper().lambda() 方法获取到 LambdaQueryWrapper
select方法
public SchoolStudent querySchoolStudent3(Integer id) {
//第一种只查"name"和"sex"两个字段
QueryWrapper<SchoolStudent> schoolStudentQueryWrapper1 = new QueryWrapper<>();
schoolStudentQueryWrapper1.select("name","sex");
List<SchoolStudent> schoolStudentList1 = schoolStudentMapper.selectList(schoolStudentQueryWrapper1);
}

select方法
//第二种只排除"name"字段
QueryWrapper<SchoolStudent> schoolStudentQueryWrapper2 = new QueryWrapper<>();
schoolStudentQueryWrapper2.select(SchoolStudent.class, s -> {
if ("name".equals(s.getColumn())) {
return false;
}
return true;
});
List<SchoolStudent> schoolStudentList2 = schoolStudentMapper.selectList(schoolStudentQueryWrapper2);

2、UpdateWrapper
继承了AbstractWrapper ,自身属性内的entity实体类可作为where条件,可以通过 new UpdateWrapper().lambda() 方法获取到 LambdaQueryWrapper
set方法和setSql方法
//set方法
UpdateWrapper<SchoolStudent> studentUpdateWrapper1 = new UpdateWrapper<>();
studentUpdateWrapper1.eq("id","1");
studentUpdateWrapper1.set("age",id);
// 设置sql语句部分
studentUpdateWrapper1.setSql("sex = '男"+id+"'");
//这里的this是serviceImpl
this.update(studentUpdateWrapper1);

3、LambdaQueryWrapper
LambdaQueryWrapper<SchoolStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(SchoolStudent::getId, id);
List<SchoolStudent> schoolStudentList = this.list(lambdaQueryWrapper);

4、LambdaUpdateWrapper
LambdaUpdateWrapper<SchoolStudent> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SchoolStudent::getId, id)
.set(SchoolStudent::getSex, "男")
.setSql("age = '" + id + "'");
this.update(updateWrapper);

5、LambdaQueryChainWrapper
List<SchoolStudent> list = new LambdaQueryChainWrapper<>(schoolStudentMapper).list();

二、其他

版权声明:本文为weixin_46146718原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。