MyBatis-Plus 学习笔记-配置(三) GlobalConfig

GlobalConfig 是 MyBatis-Plus 提供的全局策略配置,它允许开发者对 MyBatis-Plus 的行为进行全局性的定制。

banner(打印Logo)

控制是否在控制台打印 MyBatis-Plus 的 LOGO。

mybatis-plus:
  global-config:
    banner: false

就是这个

这个可以自定义,生成个txt放到src/main/resources/banner.txt

Text to ASCII Art Generator (TAAG)  没事干可以耍耍

enable-sql-runner(始化 SqlRunner)

控制是否初始化 SqlRunner(com.baomidou.mybatisplus.extension.toolkit.SqlRunner)。

mybatis-plus:
  global-config:
    enable-sql-runner: true

SqlRunner 是 MyBatis-Plus 提供的一个简单的 SQL 操作工具类,主要用于执行一些简单的 SQL 语句,而不需要编写复杂的 Mapper 文件

sql-injector(sql注入器)

3.0开始废除此属性,使用@Bean的方式注入至Spring容器

SQL 注入器,用于注入 MyBatis-Plus 提供的通用方法。SQL注入器允许开发者扩展和定制SQL语句的生成,以适应特定的业务逻辑和查询需求。

mybatis-plus:
  global-config:
    sql-injector: com.baomidou.mybatisplus.core.injector.DefaultSqlInjector

sql注入主要用于sql优化,复杂数据处理,数据权限控制等。

里面都是默认可以注入的方法

自定义执行sql 比如删除全部 

package com.baomidou.samples.injector.methods;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;

/**
 * 删除全部
 */
public class DeleteAll extends AbstractMethod {

    public DeleteAll(String methodName) {
        super(methodName);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        /* 执行 SQL ,动态 SQL 参考类 SqlMethod */
        String sql = "delete from " + tableInfo.getTableName();
        /* mapper 接口方法名一致 */
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
    }
}

注入 

package com.baomidou.samples.injector.base;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteByIdWithFill;
import com.baomidou.samples.injector.methods.DeleteAll;
import com.baomidou.samples.injector.methods.FindOne;

import java.util.List;

/**
 * 自定义Sql注入
 *
 */
public class MySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        //增加自定义方法
        methodList.add(new DeleteAll("deleteAll"));
        return methodList;
    }
}

实例化 

package com.baomidou.samples.injector.config;

import com.baomidou.samples.injector.base.MySqlInjector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 配置Mybatis-Plus的SqlInjector
 * 此配置类用于定制Mybatis-Plus的全局SQL注入器,以实现特定数据库的支持或优化
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 创建并返回一个MySqlInjector实例
     * 该方法配置了一个特定于MySQL数据库的SQL注入器,使得Mybatis-Plus能够针对MySQL进行优化处理
     *
     * @return MySqlInjector实例,用于Mybatis-Plus的全局配置
     */
    @Bean
    public MySqlInjector sqlInjector() {
        return new MySqlInjector();
    }
}

公共MybaseMapper 

/**
 * 我的自定义基础映射接口,继承自通用的基础映射接口
 * 用于定义针对特定实体的通用数据操作方法
 *
 * @param <T> 实体类类型
 */
public interface MyBaseMapper<T> extends BaseMapper<T> {

    /**
     * 删除所有实体记录
     *
     * @return 影响的记录数,成功删除所有记录返回非零值
     */
    int deleteAll();
}

 使用的mapper  继承上面的MyBaseMapper

/**
 * Mapper层
 */
@Mapper
public interface StudentMapper extends MyBaseMapper<Student> {
}

测试一下

@Slf4j
@SpringBootTest
public class InjectorTest {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void test() {
        log.info("--------------------------------------deleteAll-------------------------------------------------------");
        studentMapper.deleteAll();
    }
}

meta-object-handler (元对象字段填充控制器)

元对象字段填充控制器,用于自动填充实体类的字段。主要功能是在数据库操作时自动填充实体类中的字段。

它的典型应用场景包括创建时间、更新时间、操作人等公共字段的自动填充。

mybatis-plus:
  global-config:
    meta-object-handler: com.example.MyMetaObjectHandler

配置默认值 



import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * 自定义元对象处理器,用于在插入和更新操作时自动填充特定字段
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 在插入操作时自动填充字段
     *
     * @param metaObject 元对象,包含插入操作的实体信息
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        // 在插入操作时,严格填充"default"字段为"hello world"
        this.strictInsertFill(metaObject, "defaultData", String.class, "hello world");
    }

    /**
     * 在更新操作时自动填充字段
     *
     * @param metaObject 元对象,包含更新操作的实体信息
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        // 在更新操作时,严格填充"default"字段为"bye world"
        this.strictUpdateFill(metaObject, "defaultData", String.class, "bye world");
    }
}

测试

   @Test
    void Test() {
        List<User> userList = Arrays.asList(new User(null, "testMyMethod1", 12, "aa@qq.com", null),
                new User(null, "testMyMethod1", 12, "aa@qq.com", null));
        userMapper.testMyMethod1(userList);

    }

identifier-generator(ID生成器)

ID生成器,老版本没有这个 ,版本需要>=3.3.0。

mybatis-plus:
  global-config:
    identifier-generator: com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator

import java.util.concurrent.atomic.AtomicLong;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;

import lombok.extern.slf4j.Slf4j;

/**
 * 自定义ID生成器,实现IdentifierGenerator接口
 * 用于在实体对象中生成唯一的主键ID
 */
@Slf4j
@Component
public class CustomIdGenerator implements IdentifierGenerator {

    /**
     * 使用AtomicLong来保证多线程环境下ID生成的原子性和唯一性
     * 初始化值为1,确保从1开始计数
     */
    private final AtomicLong al = new AtomicLong(1);

    /**
     * 生成下一个ID的方法
     * 
     * @param entity 实体对象,用于提取类名生成bizKey
     * @return 生成的唯一ID
     */
    @Override
    public Long nextId(Object entity) {
        // 可以将当前传入的class全类名来作为bizKey,或者提取参数来生成bizKey进行分布式Id调用生成.
        // 这里使用实体类的全名作为bizKey,用于日志记录和分布式ID生成的场景
        String bizKey = entity.getClass().getName();
        log.info("bizKey:{}", bizKey);
        
        // 使用MetaObject对实体对象进行元数据操作,这里获取实体对象的"name"属性值
        MetaObject metaObject = SystemMetaObject.forObject(entity);
        String name = (String) metaObject.getValue("name");
        
        // 通过原子操作获取并增加ID值,确保在多线程环境下的唯一性
        final long id = al.getAndAdd(1);
        
        // 记录生成的ID值,用于调试和追踪
        log.info("为{}生成主键值->:{}", name, id);
        
        // 返回生成的ID值
        return id;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

咕德猫宁丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值