cn.meteor.common.database.autoconfiguration.MybatisPlusConfiguration Maven / Gradle / Ivy
package cn.meteor.common.database.autoconfiguration;// Copyright (C), Created on 2021-04-02
import cn.meteor.common.database.handler.MeteorLogicSqlInjector;
import cn.meteor.common.launch.constants.DbConstants;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @author ths
* @since 1.0.0
*/
@EnableTransactionManagement
@MapperScan({DbConstants.MAPPER_PACKAGE})
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisPlusProperties.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
public class MybatisPlusConfiguration {
/**
* 乐观锁插件
*
* 乐观锁实现方式:
* 取出记录时,获取当前version
* 更新时,带上这个version
* 执行更新时, set version = yourVersion+1 where version = yourVersion
* 如果version不对,就更新失败
*
* 注解实体字段 @Version 必须要
*
* @return MybatisPlus拦截器
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
/**
* 自定义 SQL注入器
* 里面包含自定义的全局方法
*
* @return 自定义SQL注入器
*/
@Bean
public MeteorLogicSqlInjector originLogicSqlInjector() {
return new MeteorLogicSqlInjector();
}
}