Mybatis-plus-配置源

core

java 复制代码
package com.mobin.config;

import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.mobin.core.factory.YmlPropertySourceFactory;
import com.mobin.handler.MybatisExceptionHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * mybatis-plus配置类(下方注释有插件介绍)
 *
 * @author mobin
 */
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan("com.mobin.**.domain")
@PropertySource(value = "classpath:common-mybatis.yml", factory = YmlPropertySourceFactory.class)
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 多租户插件 必须放到第一位
//        try {
//            TenantLineInnerInterceptor tenant = SpringUtils.getBean(TenantLineInnerInterceptor.class);
//            interceptor.addInnerInterceptor(tenant);
//        } catch (BeansException ignore) {
//        }
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * 分页插件,自动识别数据库类型
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 分页合理化
        paginationInnerInterceptor.setOverflow(true);
        return paginationInnerInterceptor;
    }

    /**
     * 乐观锁插件
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 使用网卡信息绑定雪花生成器
     * 防止集群雪花ID重复
     */
    @Bean
    public IdentifierGenerator idGenerator() {
        return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
    }

    /**
     * 异常处理器
     */
    @Bean
    public MybatisExceptionHandler mybatisExceptionHandler() {
        return new MybatisExceptionHandler();
    }

    /**
     * PaginationInnerInterceptor 分页插件,自动识别数据库类型
     * https://baomidou.com/pages/97710a/
     * OptimisticLockerInnerInterceptor 乐观锁插件
     * https://baomidou.com/pages/0d93c0/
     * MetaObjectHandler 元对象字段填充控制器
     * https://baomidou.com/pages/4c6bcf/
     * ISqlInjector sql注入器
     * https://baomidou.com/pages/42ea4a/
     * BlockAttackInnerInterceptor 如果是对全表的删除或更新操作,就会终止该操作
     * https://baomidou.com/pages/f9a237/
     * IllegalSQLInnerInterceptor sql性能规范插件(垃圾SQL拦截)
     * IdentifierGenerator 自定义主键策略
     * https://baomidou.com/pages/568eb2/
     * TenantLineInnerInterceptor 多租户插件
     * https://baomidou.com/pages/aef2f2/
     * DynamicTableNameInnerInterceptor 动态表名插件
     * https://baomidou.com/pages/2a45ff/
     */

}

mybatis

java 复制代码
package com.mobin.core.factory;

// 导入所需的类
import com.mobin.core.utils.StringUtils;  // 引入自定义的 StringUtils 工具类
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;  // 引入 Spring 提供的 YamlPropertiesFactoryBean 类,用于处理 YAML 文件
import org.springframework.core.env.PropertiesPropertySource;  // 引入 PropertiesPropertySource 类,用于将属性文件转换为 Spring 环境中的属性源
import org.springframework.core.env.PropertySource;  // 引入 PropertySource 类,这是 Spring 环境中属性源的抽象表示
import org.springframework.core.io.support.DefaultPropertySourceFactory;  // 引入 Spring 的默认属性源工厂类
import org.springframework.core.io.support.EncodedResource;  // 引入 EncodedResource 类,用于处理编码资源

import java.io.IOException;  // 引入 IOException 类,用于处理 IO 异常

/**
 * yml 配置源工厂
 * 这个类用于创建自定义的 PropertySource,用于支持加载 .yml 和 .yaml 文件作为配置源
 */
public class YmlPropertySourceFactory extends DefaultPropertySourceFactory {

    // 重写 createPropertySource 方法,该方法用于创建 PropertySource 对象
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        // 获取资源的文件名
        String sourceName = resource.getResource().getFilename();

        // 如果文件名不为空且以 .yml 或 .yaml 结尾
        if (StringUtils.isNotBlank(sourceName) && StringUtils.endsWithAny(sourceName, ".yml", ".yaml")) {
            // 创建一个 YamlPropertiesFactoryBean 对象,用于将 YAML 文件转换为 Properties 对象
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource.getResource());  // 设置资源
            factory.afterPropertiesSet();  // 初始化工厂,加载 YAML 文件并将其转换为 Properties 对象

            // 返回一个 PropertiesPropertySource 对象,该对象将文件名和加载的 Properties 关联在一起
            return new PropertiesPropertySource(sourceName, factory.getObject());
        }

        // 如果不是 .yml 或 .yaml 文件,则调用父类的 createPropertySource 方法处理
        return super.createPropertySource(name, resource);
    }
}

resource

java 复制代码
# 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖
# MyBatisPlus配置
# https://baomidou.com/config/
mybatis-plus:
  # 启动时是否检查 MyBatis XML 文件的存在,默认不检查
  checkConfigLocation: false
  configuration:
    # 自动驼峰命名规则(camel case)映射
    mapUnderscoreToCamelCase: true
    # MyBatis 自动映射策略
    # NONE:不启用 PARTIAL:只对非嵌套 resultMap 自动映射 FULL:对所有 resultMap 自动映射
    autoMappingBehavior: FULL
    # MyBatis 自动映射时未知列或未知属性处理策
    # NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息
    autoMappingUnknownColumnBehavior: NONE
    # 更详细的日志输出 会有性能损耗 org.apache.ibatis.logging.stdout.StdOutImpl
    # 关闭日志记录 (可单纯使用 p6spy 分析) org.apache.ibatis.logging.nologging.NoLoggingImpl
    # 默认日志输出 org.apache.ibatis.logging.slf4j.Slf4jImpl
    logImpl: org.apache.ibatis.logging.nologging.NoLoggingImpl
  global-config:
    # 是否打印 Logo banner
    banner: true
    dbConfig:
      # 主键类型
      # AUTO 自增 NONE 空 INPUT 用户输入 ASSIGN_ID 雪花 ASSIGN_UUID 唯一 UUID
      idType: ASSIGN_ID
      # 逻辑已删除值(框架表均使用此值 禁止随意修改)
      logicDeleteValue: 2
      # 逻辑未删除值
      logicNotDeleteValue: 0
      insertStrategy: NOT_NULL
      updateStrategy: NOT_NULL
      whereStrategy: NOT_NULL
相关推荐
激昂~逐流3 分钟前
qt操作excel(QAxObject详细介绍)
开发语言·qt·microsoft·excel·qaxobject
花开了¥5 分钟前
go-zero的快速实战(完整)
开发语言·后端·golang
一名在八月份找工作的测试员17 分钟前
数据库的操作:SQL运算符(算法/比较/逻辑/位)
java·数据库·sql
程序员佳倩24 分钟前
jsp+sevlet+mysql实验室设备管理系统2.0
java·开发语言·mysql
我命由我1234528 分钟前
Kotlin 极简小抄 P2(插值表达式、运算符、选择结构赋值)
android·java·开发语言·后端·kotlin·安卓
宝杰X728 分钟前
Compose Multiplatform+kotlin Multiplatfrom第三弹
android·开发语言·kotlin
coffee_baby28 分钟前
深入解析代理模式:静态代理、JDK 动态代理和 CGLIB 的全方位对比!
java·代理模式
Gabriel_wei34 分钟前
腾讯云Ubuntu系统安装宝塔,配置Java环境,运行spring boot项目
java·spring boot·ubuntu·腾讯云·宝塔
积跬步DEV43 分钟前
Claude Prompt 汉语新解
开发语言·前端·javascript
虚无火星车1 小时前
说说停止线程池的执行流程?
java·开发语言