栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot 集成mybatis-plus 实战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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配置分享完毕,下篇我们分享其具体业务使用,敬请期待!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/667631.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号