MybatisPlus常用增删改查
记录下MybatisPlus的简单的增删改查
接口概述
Service和Mapper区别
Mapper简化了单表的sql操作步骤(CRUD),而Serivce则是对Mapper的功能增强。
Service虽然加入了数据库的操作,但还是以业务功能为主,而更加复杂的SQL查询,还是要靠Mapper对应的XML文件里去编写SQL语句
常用增删改查
实体对象说明
人员表:
部门表:
UserVO:
查询所有数据
/*** 查询所有数据*/@GetMapping("/getUserList")public List<TrainUserEntity> getUserList() {return trainUserService.list();}
根据id查询
/*** 根据id查询数据*/@GetMapping("/getUserById")public TrainUserEntity getUserById(@RequestParam String id) {return trainUserService.getById(id);}
根据条件查询
/*** 使用LambdaQueryWrapper查询指定年龄的人员*/@Overridepublic List<TrainUserEntity> getUserByAge(String age) {LambdaQueryWrapper<TrainUserEntity> userEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();userEntityLambdaQueryWrapper.eq(TrainUserEntity::getAge, age);List<TrainUserEntity> list = trainUserMapper.selectList(userEntityLambdaQueryWrapper);return list;}
使用exists查询
查询有所属部门的人员
public List<TrainUserEntity> getUserInfo() {LambdaQueryWrapper<TrainUserEntity> wrapper = new LambdaQueryWrapper<>();wrapper.exists("SELECT 1 FROM DEPT WHERE DEPT.DID = USER.DEPTID");List<TrainUserEntity> userVOList = trainMapper.selectList(wrapper);return userVOList;}
生成的SQL:
SELECT id,name,age,address,deptid FROM user WHERE (EXISTS (SELECT 1 FROM DEPT WHERE DEPT.DID = USER.DEPTID))
使用xml查询
关联查询
MyBatis Plus 不支持联表查询,所以使用第三方插件 mybatis-plus-join来实现联表查询
添加依赖
要注意版本兼容!
dependencies {// MyBatis-Plus 核心依赖(3.4.x)implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.2'// MyBatis-Plus Join 插件(兼容 3.4.x 的版本)implementation 'com.github.yulichang:mybatis-plus-join:1.2.4'
}
left join
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
/*** 人员表左关联部门表,查询人员信息及部门名称*/@Overridepublic List<UserVO> getUserDeptInfo() {MPJLambdaWrapper<TrainUserEntity> wrapper = new MPJLambdaWrapper<>();wrapper.leftJoin(TrainDeptEntity.class, TrainDeptEntity::getDid, TrainUserEntity::getDeptid) // 左连接 dept 表 .selectAll(TrainUserEntity.class) // 查询 User 表的所有字段.select(TrainDeptEntity::getDname, TrainDeptEntity::getPhone);// 查询 dept 表的字段List<UserVO> userVOList = trainMapper.selectJoinList(UserVO.class, wrapper);return userVOList;}
生成的SQL:
SELECT t.id,t.name,t.age,t.address,t.deptid,t1.dname,t1.phone FROM user t LEFT JOIN dept t1 ON (t1.did = t.deptid)
执行结果:
新增
/*** 新增** @param trainUserEntity 需要插入的数据* @return 是否成功*/@PostMapping("/saveUser")public boolean saveUser(@RequestBody TrainUserEntity trainUserEntity) {return trainService.save(trainUserEntity);}
/*** 新增数据*/@Overridepublic boolean add(TrainUserEntity trainUserEntity) {return trainMapper.add(trainUserEntity);}
删除
/*** 根据主键id删除一条数据** @param id 主键id* @return 是否成功*/@PostMapping("/removeById")public boolean deleteById(@RequestParam String id) {return trainService.removeById(id);}
/*** 根据主键id删除一条数据** @param id 主键id* @return 删除数据的条数*/@Overridepublic int deleteById(String id) {LambdaQueryWrapper<TrainUserEntity> userEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();userEntityLambdaQueryWrapper.eq(TrainUserEntity::getId, id);int result = trainMapper.delete(userEntityLambdaQueryWrapper);return result;}
修改
/*** id修改一条数据** @param trainUserEntity 需要修改的数据* @return 修改结果*/@PostMapping("/updateUser")public boolean updateUser(@RequestBody TrainUserEntity trainUserEntity) {return trainService.updateById(trainUserEntity);}
/*** 根据id更新实体数据*/@Overridepublic int modifyById(TrainUserEntity trainUserEntity) {LambdaUpdateWrapper<TrainUserEntity> updateWrapper = new LambdaUpdateWrapper<>();updateWrapper.eq(TrainUserEntity::getId, trainUserEntity.getId()).set(TrainUserEntity::getName, trainUserEntity.getName()).set(TrainUserEntity::getAddress, trainUserEntity.getAddress());int result = trainMapper.update(null, updateWrapper);return result;}