105 lines
3.2 KiB
Java
105 lines
3.2 KiB
Java
package com.nis.config;
|
|
|
|
import cn.hutool.log.Log;
|
|
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
|
|
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
|
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
|
|
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
|
import com.nis.util.Constant;
|
|
import com.nis.util.ToolUtil;
|
|
|
|
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
|
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.DependsOn;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.sql.SQLException;
|
|
import java.util.Properties;
|
|
|
|
/**
|
|
* mybatis-plus配置
|
|
*
|
|
|
|
*/
|
|
@Configuration
|
|
public class MybatisPlusConfig {
|
|
private static final Log logger = Log.get();
|
|
/**
|
|
* 分页插件
|
|
*/
|
|
@Bean
|
|
public PaginationInterceptor paginationInterceptor() {
|
|
return new PaginationInterceptor();
|
|
}
|
|
|
|
/*@Bean
|
|
public SeqKeyGenerator seqKeyGenerator() {
|
|
return new SeqKeyGenerator();
|
|
}*/
|
|
|
|
@Bean
|
|
public ISqlInjector sqlInjector() {
|
|
return new LogicSqlInjector();
|
|
}
|
|
|
|
/**
|
|
* 自定义主键生成器
|
|
* @author fang
|
|
*
|
|
|
|
static class SeqKeyGenerator implements IKeyGenerator {
|
|
@Autowired
|
|
DatabaseIdProvider databaseIdProvider;
|
|
@Autowired
|
|
DataSource dataSource;
|
|
|
|
@Override
|
|
public String executeSql(String incrementerName) {
|
|
String databaseId = null;
|
|
try {
|
|
databaseId = databaseIdProvider.getDatabaseId(dataSource);
|
|
} catch (SQLException e) {
|
|
logger.error(e);
|
|
}
|
|
|
|
if(ToolUtil.isEmpty(databaseId)){
|
|
throw new RuntimeException("MybatisPlusConfig databaseId error");
|
|
}
|
|
|
|
switch (databaseId){
|
|
case Constant.MYSQL:
|
|
return String.format("select seq_nextval('%s')", incrementerName);
|
|
case Constant.POSTGRESQL:
|
|
return String.format("select seq_nextval('%s')", incrementerName);
|
|
case Constant.ORACLE:
|
|
return String.format("select %s.nextval from dual", incrementerName);
|
|
default:
|
|
return String.format("select seq_nextval('%s')", incrementerName);
|
|
}
|
|
}
|
|
|
|
}*/
|
|
@Bean
|
|
public DatabaseIdProvider getDatabaseIdProvider(){
|
|
DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
|
|
Properties properties = new Properties();
|
|
properties.setProperty("Oracle","oracle");
|
|
properties.setProperty("MySQL","mysql");
|
|
properties.setProperty("DB2","db2");
|
|
properties.setProperty("Derby","derby");
|
|
properties.setProperty("H2","h2");
|
|
properties.setProperty("HSQL","hsql");
|
|
properties.setProperty("Informix","informix");
|
|
properties.setProperty("MS-SQL","ms-sql");
|
|
properties.setProperty("PostgreSQL","postgresql");
|
|
properties.setProperty("Sybase","sybase");
|
|
properties.setProperty("Hana","hana");
|
|
databaseIdProvider.setProperties(properties);
|
|
|
|
return databaseIdProvider;
|
|
}
|
|
}
|