pzc лет назад: 4
Родитель
Сommit
a97409c69a

+ 11 - 3
mpwechatApp/src/main/java/com/liangjian11/wx/mp/config/MyBatisPlusConfig.java

@@ -24,10 +24,17 @@ import java.sql.SQLException;
 @Configuration
 @MapperScan(basePackages = {"com.liangjian11.wx.mp.mapper"}, sqlSessionTemplateRef = "sqlSessionTemplate")
 public class MyBatisPlusConfig {
-    @Autowired
-    private DataSourceProperties dataSourceProperties;
-
 
+    /**
+     * 数据源配置对象
+     * Primary 表示默认的对象,Autowire可注入,不是默认的得明确名称注入
+     * @return
+     */
+    @Bean
+    @ConfigurationProperties("spring.datasource")
+    public DataSourceProperties barDataSourceProperties() {
+        return new DataSourceProperties();
+    }
 
     /**
      * 数据源配置对象
@@ -45,6 +52,7 @@ public class MyBatisPlusConfig {
         DruidSource druidSource = druidProperties();
 
         DruidDataSource dataSource = new DruidDataSource();
+        DataSourceProperties dataSourceProperties = this.barDataSourceProperties();
 
         dataSource.setUrl(dataSourceProperties.getUrl());
         System.out.println(dataSourceProperties.getUrl());

+ 119 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/config/MyBatisPlusUEConfig.java

@@ -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());
+
+        //configuration
+        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);
+    }
+
+//    public SqlSessionFactory sqlSessionFactory() throws Exception {
+//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
+//        sqlSessionFactoryBean.setDataSource(dataSource());
+//        return sqlSessionFactoryBean.getObject();
+//    }
+
+    /**
+     * 配置Mapper路径
+     * @param dataSource
+     * @return
+     * @throws Exception
+     */
+    @Bean
+    public  SqlSessionFactory sqlSessionFactoryUE(@Qualifier("dataSourceUE") DataSource dataSource) throws Exception  {
+        MybatisSqlSessionFactoryBean bean  =  new  MybatisSqlSessionFactoryBean();
+        bean.setDataSource(dataSource);
+        //添加XML目录
+        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); // 使用上面配置的Factory
+        return template;
+    }
+}

+ 29 - 2
mpwechatApp/src/main/resources/application.yml

@@ -58,8 +58,35 @@ spring:
         url: jdbc:sqlserver://219.128.77.86:1433; DatabaseName=qywechattest
         username: test
         password: 360ljtest!
-
-        #配置面向服务调用
+    ue:
+        #连接池的配置信息
+        ## 初始化大小,最小,最大
+        druid:
+            initialSize: 5
+            minIdle: 5
+            maxActive: 20
+            ## 配置获取连接等待超时的时间
+            maxWait: 60000
+            # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
+            timeBetweenEvictionRunsMillis: 60000
+            # 配置一个连接在池中最小生存的时间,单位是毫秒
+            minEvictableIdleTimeMillis: 300000
+            validationQuery: SELECT 1
+            testWhileIdle: true
+            testOnBorrow: false
+            testOnReturn: false
+            poolPreparedStatements: true
+            maxPoolPreparedStatementPerConnectionSize: 20
+            # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
+            filters: stat,wall,slf4j
+            # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
+            connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
+        datasource:
+            driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
+            url: jdbc:sqlserver://219.128.77.86:1433; DatabaseName=qywechattest
+            username: test
+            password: 360ljtest!
+            #配置面向服务调用
     cloud:
         config:
             discovery:

+ 0 - 1
qiyewechatApp/src/main/java/com/liangjiang11/wx/cp/controller/WxaAddContactController.java

@@ -42,7 +42,6 @@ public class WxaAddContactController {
     @Autowired
     private QiyeUsersService qiyeUsersService;
 
-
     /**
      * 验证测试的:企业微信接收客户变更事件验证接口回调路径
      * @return