2024-04-01 13:45:32 +08:00
|
|
|
package com.zhgd.config;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
2024-04-02 11:13:17 +08:00
|
|
|
import com.wflow.config.MyBatisPlusConfig;
|
2024-04-01 13:45:32 +08:00
|
|
|
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
|
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
|
2024-04-02 11:13:17 +08:00
|
|
|
import javax.annotation.Resource;
|
2024-04-01 13:45:32 +08:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@MapperScan(basePackages = {"com.zhgd.*.*.*.*.mapper", "com.zhgd.*.*.*.mapper"}, sqlSessionTemplateRef = "db1SqlSessionTemplate")
|
|
|
|
|
public class DataSourceOneConfig {
|
2024-04-02 11:13:17 +08:00
|
|
|
@Resource
|
|
|
|
|
private MybatisPlusConfig mybatisPlusConfig;
|
2024-04-01 13:45:32 +08:00
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConfigurationProperties(prefix = "spring.datasource.db1")
|
|
|
|
|
public DataSource db1DataSource() {
|
|
|
|
|
return DataSourceBuilder.create().build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Lazy //这个是懒加载的注解,根据自己项目需要看是否添加
|
|
|
|
|
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
|
|
|
|
|
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
|
|
|
|
bean.setDataSource(dataSource);
|
|
|
|
|
//开启驼峰
|
|
|
|
|
MybatisConfiguration configuration = new MybatisConfiguration();
|
|
|
|
|
configuration.setMapUnderscoreToCamelCase(true);
|
|
|
|
|
configuration.setLogImpl(StdOutImpl.class);
|
|
|
|
|
bean.setConfiguration(configuration);
|
|
|
|
|
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/zhgd/xmgl/**/*.xml"));
|
2024-04-02 11:13:17 +08:00
|
|
|
bean.setPlugins(mybatisPlusConfig.mybatisPlusInterceptor());
|
2024-04-01 13:45:32 +08:00
|
|
|
return bean.getObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Primary
|
|
|
|
|
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
|
|
|
|
|
return new DataSourceTransactionManager(dataSource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Primary
|
|
|
|
|
@Lazy
|
|
|
|
|
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
|
|
|
|
|
return new SqlSessionTemplate(sqlSessionFactory);
|
|
|
|
|
}
|
|
|
|
|
}
|