目录
条件构造器
案例
自定义SQL
案例
Service接口
案例
综合案例
条件构造器
案例
@Testvoid testQueryMapper() {// 创建 QueryWrapper 实例QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.select("id," + "username," + "info," + "balance ");// 添加条件:用户名包含 "o",余额大于等于 1000queryWrapper.like("username", "o").ge("balance", 1000);// 使用 MyBatis-Plus 的方法执行查询List<User> users = userMapper.selectList(queryWrapper);users.forEach(System.out::println);}
@Testvoid testUpdateWapper() {UpdateWrapper<User> wrapper = new UpdateWrapper<>();// 使用实体的函数式方式指定要更新的字段wrapper.set("balance", 5000).eq("username", "jack");// 执行更新操作int updated = userMapper.update(null, wrapper);if (updated == 1) {// 更新成功System.out.println("更新成功");} else {// 更新失败或无记录被更新System.out.println("更新失败");}}
@Testvoid testUpdateWapper1(){UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();updateWrapper.in("id", Arrays.asList(1, 2, 4));updateWrapper.setSql("balance = balance-200");userMapper.update(null,updateWrapper);}
自定义SQL
案例
Service接口
案例
@Testvoid testSaveUser() {User user = new User();
// user.setId(5L);user.setUsername("马超");user.setPassword("123");user.setPhone("18688990011");user.setBalance(200);user.setInfo("{\"age\": 24, \"intro\": \"英文老师\", \"gender\": \"female\"}");user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userService.save(user);}@Testvoid testQuery() {List<User> users = userService.listByIds(Arrays.asList(1L, 2l, 3l));users.forEach(System.out::println);}
综合案例
@RequestMapping("/users")
@RestController
@Api(tags = "用户管理接口")
//配合final 完成构造函数注入
@RequiredArgsConstructor
public class UserController {private final IUserService iUserService;@ApiOperation("新增用户管理接口")@PostMappingpublic void saveUser(@RequestBody UserFormDTO userFormDTO) {User user = new User();
// 1.把Dto拷贝到PoBeanUtil.copyProperties(userFormDTO, user);iUserService.save(user);}@ApiOperation("删除用户管理接口")@DeleteMapping("{id}")public void deleteUserById(@ApiParam("用户id") @PathVariable("id") long id) {iUserService.removeById(id);}@ApiOperation("根据id查询用户接口")@GetMapping("{id}")public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") long id) {User user = iUserService.getById(id);
// 拷贝;return BeanUtil.copyProperties(user,UserVO.class) ;}@ApiOperation("根据id批量查询用户接口")@GetMappingpublic List<UserVO> queryUserByIds(@ApiParam("用户集合ids集合") @RequestParam("ids") List<Long> ids) {
// 根据ids集合查询用户集合List<User> users = iUserService.listByIds(ids);
// 拷贝return BeanUtil.copyToList(users, UserVO.class);}@ApiOperation("根据id扣减用户余额")@PutMapping("/{id}/deduction/{money}")public void deductMoneyById(@ApiParam("用户id") @PathVariable("id") Long id, @ApiParam("扣减的金额") @PathVariable("money") Integer money) {iUserService.deductMoneyById(id, money);}}
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {private final UserMapper userMapper;@Overridepublic void deductMoneyById(Long id, Integer money) {
// 查询用用户余额是否支持扣减
// 已经注入mapper了
// User user1 = baseMapper.selectById(id);User user = this.getById(id);if (user == null || user.getStatus() == 2) {throw new RuntimeException("用户状态异常");}// 校验用户余额是否支持扣减if (user.getBalance() - money < 0) {throw new RuntimeException("用户余额不足");}UpdateWrapper<User> wrapper = new UpdateWrapper<>();wrapper.eq("id",id);
// 完成扣减baseMapper.deductMoneyById(wrapper,money);}
}
void deductMoneyById(@Param("ew") UpdateWrapper<User> eq, @Param("money")Integer money);
<update id="deductMoneyById">UPDATE user SET balance = balance-#{money} ${ew.customSqlSegment}</update>