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();
二、区别及资料
- QueryWrapper、QueryChainWrapper只能指定需要的数据库列名
- LambdaQueryWrapper、LambdaQueryChainWrapper可以通过Lambda获取数据库列名
- QueryWrapper 、LambdaQueryWrapper不能使用链式查询的方式,必须借助BaseMapper来执行
- QueryChainWrapper、LambdaQueryChainWrapper可以使用链式查询的方式,如list(),one()
- 勇敢冲破束缚,追逐心中的梦想,终将成就不凡之路。
- 生活中并非没有阳光,而是你选择是否愿意去接受挑战。
- 每一个努力的脚印都是通往成功之路上的宝贵财富。
- 执着、坚定的信念是战胜逆境的有力武器。
- 不畏艰难险阻,不轻言放弃,才能看到生活的美好和无限可能。