2020-09-18 13:04:12 +08:00
|
|
|
package com.chinaweal.youfool.prj.config;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
|
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
|
|
|
|
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
|
|
|
|
import com.chinaweal.youfool.framework.springboot.mybatis.plus.CommonMetaObjectHandler;
|
|
|
|
|
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.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
|
2021-05-25 14:12:15 +08:00
|
|
|
import javax.annotation.Resource;
|
2020-09-18 13:04:12 +08:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-04 17:53:04 +08:00
|
|
|
* 项目的数据源
|
2020-09-18 13:04:12 +08:00
|
|
|
*/
|
|
|
|
|
|
2020-10-15 23:59:52 +08:00
|
|
|
@Configuration
|
2021-03-23 16:08:46 +08:00
|
|
|
@MapperScan(basePackages = {"com.chinaweal.youfool.framework.springboot.cms.**.mapper", "com.chinaweal.youfool.prj.**.mapper"}, sqlSessionTemplateRef = "prjSqlSessionTemplate")
|
2020-10-15 23:59:52 +08:00
|
|
|
public class PrjDataSource {
|
2020-09-18 13:04:12 +08:00
|
|
|
|
2021-05-25 14:12:15 +08:00
|
|
|
@Resource
|
|
|
|
|
private CommonMetaObjectHandler commonMetaObjectHandler;
|
2020-09-18 13:04:12 +08:00
|
|
|
|
2020-10-15 23:59:52 +08:00
|
|
|
@Bean(name = "prjDS", initMethod = "init", destroyMethod = "close")
|
|
|
|
|
@ConfigurationProperties(prefix = "spring.datasource.prj")
|
2020-09-18 13:04:12 +08:00
|
|
|
@Primary
|
|
|
|
|
public DruidDataSource dataSource() {
|
|
|
|
|
return DruidDataSourceBuilder.create().build();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 23:59:52 +08:00
|
|
|
@Bean(name = "prjSqlSessionFactory")
|
2020-09-18 13:04:12 +08:00
|
|
|
@Primary
|
2020-10-15 23:59:52 +08:00
|
|
|
public MybatisSqlSessionFactoryBean sqlSessionFactory(@Qualifier("prjDS") DataSource dataSource) throws Exception {
|
2020-09-18 13:04:12 +08:00
|
|
|
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
|
|
|
|
bean.setDataSource(dataSource);
|
|
|
|
|
GlobalConfig globalConfig = new GlobalConfig();
|
2021-05-25 14:12:15 +08:00
|
|
|
globalConfig.setMetaObjectHandler(commonMetaObjectHandler);
|
2020-09-18 13:04:12 +08:00
|
|
|
bean.setGlobalConfig(globalConfig);
|
|
|
|
|
bean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
|
|
|
|
|
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/mapper/**/*.xml"));
|
|
|
|
|
return bean;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 23:59:52 +08:00
|
|
|
@Bean(name = "prjTransactionManager")
|
2020-09-18 13:04:12 +08:00
|
|
|
@Primary
|
2020-10-15 23:59:52 +08:00
|
|
|
public DataSourceTransactionManager transactionManager(@Qualifier("prjDS") DataSource dataSource) {
|
2021-05-25 14:12:15 +08:00
|
|
|
return new DataSourceTransactionManager(dataSource);
|
2020-09-18 13:04:12 +08:00
|
|
|
}
|
|
|
|
|
|
2020-10-15 23:59:52 +08:00
|
|
|
@Bean(name = "prjSqlSessionTemplate")
|
2020-09-18 13:04:12 +08:00
|
|
|
@Primary
|
2020-10-15 23:59:52 +08:00
|
|
|
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("prjSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
|
2020-09-18 13:04:12 +08:00
|
|
|
return new SqlSessionTemplate(sqlSessionFactory);
|
|
|
|
|
}
|
|
|
|
|
}
|