springBoot 集成mybatis-plus 实战

springBoot 集成mybatis-plus 实战,第1张

springBoot 集成mybatis-plus 实战

mybatis-plus 是mybatis的升级版,使用很方便,今天我们分享一下实战 *** 作:

1、jar包引入:

        
            com.baomidou
            mybatis-plus
            2.1.9
        
        
            com.baomidou
            mybatisplus-spring-boot-starter
            1.0.5
        
          
            com.baomidou
            mybatis-plus-support
            2.1.9
            compile
        

2、配置文件配置:

mybatis-plus:
  configuration:
    #默认值:true是否开启自动驼峰命名规则(camel case)映射
    map-underscore-to-camel-case: true
    #全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为 true
    cache-enabled: true
#false指定当结果集中值为 null 的时候是否调用映射对象的 Setter(Map 对象时为 put)方法,通常运用于有 Map.keySet() 依赖或 null 值初始化的情况
    call-setters-on-nulls: true
  global-config:
    id-type: 0  # 主键生成策略    
    field-strategy: 2
    db-column-underline: true
    refresh-mapper: true
    capital-mode: true
    logic-delete-value: -1
    logic-not-delete-value: 0
    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector  #sql主入器
  

3、mybatis-pius的业务配置,比如分页,sql注入等:

@Configuration
public class CommonMybatisPlusConfig {

    
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 开启 PageHelper 的支持
        paginationInterceptor.setLocalPage(true);
        return paginationInterceptor;
    }

    @Bean
    public OptimisticLockerInterceptor optimisticLoker() {
        return new OptimisticLockerInterceptor();
    }

    
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}

4、实体类上配置表名

@TableName("user_info")//必加表名
public class Userinfo {
    @TableId(type= IdType.INPUT)//AUTO 设置主键自增?
    private Integer id;
}

主键生成策略,源码:一目了然!

package com.baomidou.mybatisplus.enums;


public enum IdType {
    AUTO(0, "数据库ID自增"), INPUT(1, "用户输入ID"),

    
    ID_WORKER(2, "全局唯一ID"), UUID(3, "全局唯一ID"), NONE(4, "该类型为未设置主键类型"),
    ID_WORKER_STR(5, "字符串全局唯一ID");

    
    private final int key;

    
    private final String desc;

    IdType(final int key, final String desc) {
        this.key = key;
        this.desc = desc;
    }

    
    public static IdType getIdType(int idType) {
        IdType[] its = IdType.values();
        for (IdType it : its) {
            if (it.getKey() == idType) {
                return it;
            }
        }
        return ID_WORKER;
    }

    public int getKey() {
        return this.key;
    }

    public String getDesc() {
        return this.desc;
    }

}

 5、Mapper层继承mybatis-plus的基础API :  baseMapper

public interface UserInfoMapper extends baseMapper 

6、业务层继承mybatis-plus的业务接口: ServiceImpl, T>

public class UserServiceImpl extends ServiceImpl implements UserService

源码如图:

 到此,mybatis-plus配置分享完毕,下篇我们分享其具体业务使用,敬请期待!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5672891.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存