Mybatis-Plus使用笔记(SpringBoot版)

Mybatis-Plus使用笔记(SpringBoot版)

配置

启动类上配置@MapperScan扫描mapper接口
application.yml


mybatis-plus: #配置文件配置在mybatis-plus
  mapper-locations: classpath:mapper/**/*Mapper.xml # 存放映射mapper文件 扫描Mapper.xml结尾的文件
  type-aliases-package: com.ctc.entity # 对象存放位置
  configLocation: classpath:mybatis/mybatis-config.xml
  type-enums-package: com.ctc.enums.mybatis
  global-config:
    refresh: true

分页

配置上开启分页
Mybatis-Plus自带分页插件使用方式官方文档上有,

如果单独使用插件,page放在第一个参数

MyPage实现Mybatis-Plus的IPage接口(也可以使用插件自带的Page),查询时可自动分页
使用

List<Demo> listByCondition(MyPage page, @Param(Constants.WRAPPER) Wrapper wrapper);

或者

List<Demo> listByCondition(MyPage page, @Param("condition") Condition condition);

自定义SQL

 <select id="listByCondition" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_info i
           left join t_dict d on d.info_id= i.info_id
        <where>
            ${ew.sqlSegment}
        </where>

    </select>

如果使用插件的条件构造器,
构造时注意i为表的别名

 wrapper.eq("i.fk_id", fkId);

重点是sqlSegment ew 是插件默认
ResultMap配置参见Mybatis官方文档

自定义关联关系

pk为关联对象

    @TableField(exist = false)
    private PK pk;

ResultMap配置参见Mybatis官方文档

配置

我使用的配置

@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 性能分析插件 生产环境下不推荐使用
     * @return
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }


    /**
     * 逻辑删除
     * @return
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}

后续会把整合的项目传到GitHub上
项目使用的技术有

  1. SpringBoot
  2. Mybatis
  3. Durid
  4. Fastjson
  5. Mybatis-Plus
  6. Swagger
  7. Hibernate Validate
  8. 文件上传

https://github.com/googalAmbition/myspringboot/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值