Mybatis-Plus总结

简介

MyBatis-Plus是Mybatis的增强工具,增强Mybatis功能而不改变基本功能,为简化开发、提高效率而生。

配置

添加依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

去掉mybatis配置

# mybatis:
#  mapper-locations: classpath:mapper/*.xml  #对应mapper映射xml文件所在路径
#  type-aliases-package: com.qcbt.lxt.byg0417.entity  #对应实体类路径
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

添加mybatis-plus配置

# mybatis-plus配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # 搜索指定包别名
  typeAliasesPackage: tech.niua.auth.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mybatis/**/*Mapper.xml
  # 加载全局的配置文件
#  configLocation: classpath:mybatis/mybatis-config.xml

添加分页插件

@Configuration
public class MybatisPlusConfig {
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

使用

代码生成

快速生成entity层、mapper层、service层、controller层和mapper映射文件。
在生成的mapper层和service层的接口中,继承mybatis-plus自带的接口。
mapper层接口继承了BaseMapper接口。
service层接口继承IService接口,IService接口封装了BaseMapper接口。
在BaseMapper接口中有许多CRUD相关的方法,可以直接调用。

【添加数据:(增)】
    int insert(T entity);              // 插入一条记录
注:
    T         表示任意实体类型
    entity    表示实体对象

【删除数据:(删)】
    int deleteById(Serializable id);    // 根据主键 ID 删除
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);  // 根据 map 定义字段的条件删除
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); // 根据实体类定义的 条件删除对象
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 进行批量删除
注:
    id        表示 主键 ID
    columnMap 表示表字段的 map 对象
    wrapper   表示实体对象封装操作类,可以为 null。
    idList    表示 主键 ID 集合(列表、数组),不能为 null 或 empty

【修改数据:(改)】
    int updateById(@Param(Constants.ENTITY) T entity); // 根据 ID 修改实体对象。
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); // 根据 updateWrapper 条件修改实体对象
注:
    update 中的 entity 为 set 条件,可以为 null。
    updateWrapper 表示实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)

【查询数据:(查)】
    T selectById(Serializable id); // 根据 主键 ID 查询数据
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 进行批量查询
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 根据表字段条件查询
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根据实体类封装对象 查询一条记录
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询记录的总条数
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询所有记录(返回 entity 集合)
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询所有记录(返回 map 集合)
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询所有记录(但只保存第一个字段的值)
    <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询所有记录(返回 entity 集合),分页
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询所有记录(返回 map 集合),分页
注:
    queryWrapper 表示实体对象封装操作类(可以为 null)
    page 表示分页查询条件

实体类注释

@TableName:用来指定数据库表名和JavaBean映射关系。
@TableId:指定该属性为数据库表中的主键。
@TableField:指定该属性为数据库表中的非主键。

@Builder
@Data
@TableName("ref_vedio_file")
public class VedioFile {
    @TableId(type = IdType.AUTO)
    private Long id;
    IPage
    private Long vedioId;

    private Long fileId;

    private String vedioFile;

    private int sort;
    
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
}

条件构造器

可以使用queryWrapper、updateWrapper 等构造器来构造条件,返回相应的数据,用于查询相关的接口。

/**
    * 查询列表
    *
    * @param currentPage
    * @param pageSize
    * @param vedio
    * @return
    */
    @Log(value = "查询列表", businessType = BusinessType.LIST)
    @PreAuthorize("hasAuthority('/vedio')")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")})
    @PostMapping("/list/{currentPage}/{pageSize}")
    public ResultJson index(@ApiParam(name="currentPage",value="页数",required=true) @PathVariable Integer currentPage, @ApiParam(name="pageSize",value="每页数量",required=true) @PathVariable Integer pageSize, @RequestBody  Vedio vedio) {
        QueryWrapper<Vedio> queryWrapper = new QueryWrapper<>();

         if(StringUtils.isNotBlank(vedio.getVedioIntro())) {
             queryWrapper.like("vedio_intro", vedio.getVedioIntro());
         }

         if(StringUtils.isNotBlank(vedio.getVedioCover())) {
             queryWrapper.like("vedio_cover", vedio.getVedioCover());
         }

         if(StringUtils.isNotBlank(vedio.getOperator())) {
             queryWrapper.like("operator", vedio.getOperator());
         }

         if(vedio.getType()!=null){
            queryWrapper.eq("type",vedio.getType());
         }
         if(vedio.getClassificationId()!=null){
            queryWrapper.eq("type",vedio.getClassificationId());
         }

        IPage<Vedio> pageList = vedioService.page(new Page<>(currentPage, pageSize), queryWrapper);
        return ResultJson.ok(pageList);
    }

分页

使用IPage和Page来进行分页,mapper层和service层的方法加上Page参数,返回参数类型为IPage,泛型为对应的实体类。

IPage<Vedio> selectVedioList(Page<Vedio> page, Vedio vedio);
@Override
public IPage<Vedio> selectVedioList(Page<Vedio> page, Vedio vedio) {
        return this.baseMapper.selectVedioList(page, vedio);
}

controller层中的方法构造Page和IPage对象,IPage对象作为返回值返回,即可实现分页。

/**
 *查询视频列表,可通过分类id查询
 * @param: vedio
 * @return
 */
@ApiOperation(value = "视频列表-查询视频列表,可通过分类id查询")
@PostMapping("/public/selectVedioList")
public ResultJson selectVedioList(@RequestBody Vedio vedio,
                                  @ApiParam(name="page",value="页码",required = true)@RequestParam("page") String page,
                                  @ApiParam(name="limit",value="每页数据量",required = true)@RequestParam("limit") String limit) {
    Page<Vedio> userPage = new Page<>(Integer.valueOf(page), Integer.valueOf(limit));

    IPage<Vedio> vedioList = this.vedioService.selectVedioList(userPage, vedio);
    return ResultJson.ok(vedioList);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值