|
@@ -0,0 +1,119 @@
|
|
|
+package com.liangjian11.wx.mp.config;
|
|
|
+
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
|
|
+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.autoconfigure.jdbc.DataSourceProperties;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.SQLException;
|
|
|
+
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@MapperScan(basePackages = {"com.liangjian11.wx.mp.mapperUE"}, sqlSessionTemplateRef = "sqlSessionTemplateUE")
|
|
|
+public class MyBatisPlusUEConfig {
|
|
|
+
|
|
|
+
|
|
|
+ * 数据源配置对象
|
|
|
+ * Primary 表示默认的对象,Autowire可注入,不是默认的得明确名称注入
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ @ConfigurationProperties("spring.ue.datasource")
|
|
|
+ public DataSourceProperties barDataSourceProperties() {
|
|
|
+ return new DataSourceProperties();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 数据源配置对象
|
|
|
+ * Primary 表示默认的对象,Autowire可注入,不是默认的得明确名称注入
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ @ConfigurationProperties("spring.ue.druid")
|
|
|
+ public DruidSource druidProperties() {
|
|
|
+ return new DruidSource();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "dataSourceUE")
|
|
|
+ public DataSource dataSourceUE() throws SQLException {
|
|
|
+ DruidSource druidSource = druidProperties();
|
|
|
+
|
|
|
+ DruidDataSource dataSource = new DruidDataSource();
|
|
|
+ DataSourceProperties dataSourceProperties = this.barDataSourceProperties();
|
|
|
+
|
|
|
+ dataSource.setUrl(dataSourceProperties.getUrl());
|
|
|
+ System.out.println(dataSourceProperties.getUrl());
|
|
|
+ dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
|
|
|
+ dataSource.setUsername(dataSourceProperties.getUsername());
|
|
|
+ dataSource.setPassword(dataSourceProperties.getPassword());
|
|
|
+
|
|
|
+
|
|
|
+ dataSource.setInitialSize(druidSource.getInitialSize());
|
|
|
+ dataSource.setMinIdle(druidSource.getMinIdle());
|
|
|
+ dataSource.setMaxActive(druidSource.getMaxActive());
|
|
|
+ dataSource.setMaxWait(druidSource.getMaxWait());
|
|
|
+ dataSource.setTimeBetweenEvictionRunsMillis(druidSource.getTimeBetweenEvictionRunsMillis());
|
|
|
+ dataSource.setMinEvictableIdleTimeMillis(druidSource.getMinEvictableIdleTimeMillis());
|
|
|
+ dataSource.setValidationQuery(druidSource.getValidationQuery());
|
|
|
+ dataSource.setTestWhileIdle(druidSource.isTestWhileIdle());
|
|
|
+ dataSource.setTestOnBorrow(druidSource.isTestOnBorrow());
|
|
|
+ dataSource.setTestOnReturn(druidSource.isTestOnReturn());
|
|
|
+ dataSource.setPoolPreparedStatements(druidSource.isPoolPreparedStatements());
|
|
|
+ dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidSource.getMaxPoolPreparedStatementPerConnectionSize());
|
|
|
+
|
|
|
+ dataSource.setFilters(druidSource.getFilters());
|
|
|
+
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 配置事务
|
|
|
+ * @param fooDataSource
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ @Resource
|
|
|
+ public PlatformTransactionManager fooManagerUE(@Qualifier("dataSourceUE")DataSource fooDataSource){
|
|
|
+ return new DataSourceTransactionManager(fooDataSource);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 配置Mapper路径
|
|
|
+ * @param dataSource
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public SqlSessionFactory sqlSessionFactoryUE(@Qualifier("dataSourceUE") DataSource dataSource) throws Exception {
|
|
|
+ MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
|
|
+ bean.setDataSource(dataSource);
|
|
|
+
|
|
|
+ bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
|
|
|
+ bean.setTypeAliasesPackage("com.liangjian11.wx.mp.mapper");
|
|
|
+ return bean.getObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SqlSessionTemplate sqlSessionTemplateUE(@Qualifier("sqlSessionFactoryUE") SqlSessionFactory sqlSessionFactory) throws Exception {
|
|
|
+ SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+}
|