package com.gkhy.safePlatform.config.database;
|
|
//import com.alibaba.druid.pool.DruidDataSource;
|
//import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
import org.apache.ibatis.plugin.Interceptor;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.mybatis.spring.SqlSessionTemplate;
|
import org.mybatis.spring.annotation.MapperScan;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
import org.springframework.stereotype.Repository;
|
|
import javax.sql.DataSource;
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
@Configuration
|
@MapperScan(basePackages = "com.gkhy.safePlatform.doublePrevention",sqlSessionTemplateRef = "sqlTemplateDoublePrevent",
|
annotationClass =
|
Repository.class)
|
public class MyBatisConfigDoublePrevent {
|
|
@Autowired
|
@Qualifier("dsDoublePrevent")
|
DataSource dsDoublePrevent;
|
|
@Autowired
|
private MybatisPlusInterceptor mybatisPlusInterceptor;
|
|
@Bean(name = "sqlFactoryDoublePrevent")
|
public SqlSessionFactory sqlSessionFactory() throws Exception {
|
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
|
factoryBean.setDataSource(dsDoublePrevent);
|
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
// factoryBean.setMapperLocations(resolver.getResources("classpath*:config/mapper/doublePrevention/**Mapper.xml"));
|
factoryBean.setMapperLocations(resolveMapperLocations());
|
//设置分页插件
|
Interceptor[] plugins = {mybatisPlusInterceptor};
|
factoryBean.setPlugins(plugins);
|
return factoryBean.getObject();
|
}
|
|
public Resource[] resolveMapperLocations() {
|
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
|
List<String> mapperLocations = new ArrayList<>();
|
mapperLocations.add("classpath*:config/mapper/doublePrevention/**Mapper.xml");
|
List<Resource> resources = new ArrayList();
|
if (mapperLocations != null) {
|
for (String mapperLocation : mapperLocations) {
|
try {
|
Resource[] mappers = resourceResolver.getResources(mapperLocation);
|
resources.addAll(Arrays.asList(mappers));
|
} catch (IOException e) {
|
// ignore
|
}
|
}
|
}
|
return resources.toArray(new Resource[resources.size()]);
|
}
|
|
@Bean(name = "sqlTemplateDoublePrevent")
|
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
|
return new SqlSessionTemplate(sqlSessionFactory());
|
}
|
|
}
|