package com.chinaweal.youfool.devops.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.core.io.ClassPathResource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * youfool基础的数据源 * * @author itluck */ @Configuration @MapperScan(basePackages = "com.chinaweal.youfool.framework.springboot.**.mapper", sqlSessionTemplateRef = "youfoolSqlSessionTemplate") public class YoufoolDataSource { @Bean(name = "youfoolDS", initMethod = "init", destroyMethod = "close") @ConfigurationProperties(prefix = "spring.datasource.youfool") public DruidDataSource dataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "youfoolSqlSessionFactory") public MybatisSqlSessionFactoryBean sqlSessionFactory(@Qualifier("youfoolDS") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setMetaObjectHandler(new CommonMetaObjectHandler()); bean.setGlobalConfig(globalConfig); bean.setConfigLocation(new ClassPathResource("youfool/mybatis/mybatis-config.xml")); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:youfool/mybatis/mapper/**/*.xml")); return bean; } @Bean(name = "youfoolTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("youfoolDS") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "youfoolSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("youfoolSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }