SpringCloud
一、MyBatisPlus
(1)使用MyBatisPlus的基本操作步骤
①引入MybatisPlus依赖,代替Mybatis依赖
com.baomidou mybatis-plus-boot-starter 3.5.3.1
②定义Mapper接口并继承BaseMapper
@Mapper public interface UserMapper extends BaseMapper { }
(2)常见注解
①MybatisPlus是如何获取实现CRUD的数据库表信息的?
MyBatisPlus通过扫描实体类,并基于反射获取实体类信息作为数据库表信息。
默认类名驼峰转下划线作为表名,名为id的字段作为主键,变量名驼峰转下划线作为表的字段名。
②常用注解:
@TableName:用来指定表名。
@TableId:用来指定表中的主键字段信息
@TableField:用来指定表中的普通字段信息
③IdType(枚举)的常见类型:
AUTO:数据库自增长
ASSIGN_ID:分配ID,接口IdentifierGenerator的方法nextId来生成id,默认实现类为DefaultIdentifierGenerator雪花算法
INPUT:通过set方法自行输入
④使用@TableField的常见场景:
成员变量名与数据库字段名不一致。
成员变量名以is开头,且为布尔值。
成员变量名与数据库关键字冲突。
(3)常见配置
mybatis-plus: type-aliases-package: com.itheima.mp.domain.po # 别名扫描包 mapper-locations: "classpath*:/mapper/**/*.xml" # Mapper.xml文件地址,默认值 configuration: map-underscore-to-camel-case: true # 是否开启下划线和驼峰的映射 cache-enabled: false # 是否开启二级缓存 global-config: db-config: id-type: assign_id # id为雪花算法生成 update-strategy: not_null # 更新策略:只更新非空字段
二、核心功能
(1)条件构造器
①QueryWrapper和LambdaQueryWrapper通常用来构建select、delete、update的where条件部分。
@Test void testQueryWrapper(){ QueryWrapper wrapper = new QueryWrapper() .select("id","username","info","balance") .like("username","o") .ge("balance",1000); //2.查询 List users = userMapper.selectList(wrapper); users.forEach(System.out::println); }
②UpdateWrapper和LambdaUpdateWrapper通常只有在set语句比较特殊才使用。
@Test void testUpdateWrapper() { Listids=new ArrayList(); ids.add(1L); ids.add(2L); ids.add(4L); UpdateWrapper wrapper = new UpdateWrapper() .setSql("balance=balance-200") .in("id",ids); userMapper.update(null, wrapper); }
③尽量使用LambdaQueryWrapper和LambdaUpdateWrapper,避免硬编码。
@Test void testLambdaQueryWrapper(){ LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .select(User::getId,User::getUsername,User::getInfo,User::getBalance) .like(User::getUsername,"o") .ge(User::getBalance,1000); //2.查询 List users = userMapper.selectList(wrapper); users.forEach(System.out::println); }
(2)自定义SQL
我们可以利用MyBatisPlus的Wrapper来构建复杂的Where条件,然后自己定义SQL语句中剩下的部分。
①基于Wrapper构建where条件
@Test void testCustomSqlUpdate(){ // 1.更新条件 Listids=new ArrayList(); ids.add(1L); ids.add(2L); ids.add(4L); int amount=200; // 2.定义条件 QueryWrapperwrapper=new QueryWrapper().in("id" ,ids); // 3.调用自定义的SQL方法 userMapper.updateBalanceByIds(wrapper,amount); }
②在Mapper方法参数中用Param注解声明Wrapper变量名称,必须是ew
void updateBalanceByIds(@Param("ew") QueryWrapper wrapper,@Param("amount") int amount);
③自定义SQL,并使用Wrapper条件
update tb_user set balance=balance-#{amount} ${ew.customSqlSegment}
(3)MP的Service接口使用流程
①自定义Service接口继承IService接口
public interface IUserService extends IService {}
②自定义Service实现类,实现自定义接口并继承ServiceImpl类
@Service public class UserServiceImpl extends ServiceImpl implements IUserService{}
(4)iService开发基础业务接口
@Api(tags = "用户管理接口") @RestController @RequestMapping("/users") @RequiredArgsConstructor public class UserController { private final IUserService userService; // 新增 @ApiOperation("新增用户接口") @PostMapping public void save(@RequestBody UserFormDTO userDTO){ // 1.将DTO拷贝到PO User user = BeanUtil.copyProperties(userDTO, User.class); // 2.新增 userService.save(user); }
(5)iService开发复杂业务接口
①控制层:
@ApiOperation("扣减用户余额接口") @PutMapping("/{id}/deduction/{money}") public void deductBalance( @ApiParam("用户id") @PathVariable("id")Long id, @ApiParam("扣减的金额") @PathVariable("money")Integer money) { userService.deductBalance(id,money); }
②业务层:
void deductBalance(Long id, Integer money); @Override public void deductBalance(Long id, Integer money) { // 1.查询用户 User user = getById(id); // 2.校验用户状态 if (user==null||user.getStatus()==2){ throw new RuntimeException("用户状态异常!"); } // 3.校验余额是否充足 if(user.getBalance()