zoukankan      html  css  js  c++  java
  • druid 多数据源配置

    新建两个数据源配置类:DruidDataSourceMasterConfigurer.java和DruidDataSourceAddressBookConfigurer.java

    DruidDataSourceMasterConfigurer.java代码如下

    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
    @Configuration
    @MapperScan(basePackages = DruidDataSourceMasterConfigurer.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactoryMaster")
    @Primary
    public class DruidDataSourceMasterConfigurer {

    /**
    * dao层的包路径
    */
    static final String PACKAGE = "com.dienphuoc.select.user.depart.dao.department_user";

    /**
    * Mapper文件路径
    */
    @Value("${mybatis.mapper-locations}")
    private String mapperLocation;

    @Value("${spring.druid.master.publicKey}")
    private String publicKey;
    @Value("${spring.druid.master.decrypt}")
    private String decrypt;

    /**
    * @ConfigurationProperties(prefix = "配置文件中数据源路径")
    * @Bean(name = "配置文件中数据源名称", initMethod = "init", destroyMethod = "close")
    */
    @ConfigurationProperties(prefix = "spring.druid.master")
    @Bean(name = "master", initMethod = "init", destroyMethod = "close")
    public DruidDataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setProxyFilters(Lists.newArrayList(statFilter(), configFilter()));
    Properties properties = new Properties();
    properties.setProperty("config.decrypt", decrypt);
    properties.setProperty("config.decrypt.key", publicKey);
    dataSource.setConnectProperties(properties);
    return dataSource;
    }

    @Bean("txManager")
    public DataSourceTransactionManager txManager() {
    return new DataSourceTransactionManager(dataSource());
    }

    /**
    * 定义空Config类用于加密类的加载
    */
    @Bean
    public ConfigFilter configFilter() {
    ConfigFilter configFilter = new ConfigFilter();
    return configFilter;
    }

    /**
    * 配置了要打印的sql
    */
    @Bean
    public Filter statFilter() {
    StatFilter filter = new StatFilter();
    filter.setSlowSqlMillis(2000);
    filter.setLogSlowSql(true);
    filter.setMergeSql(true);
    return filter;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
    return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    }


    /**
    * @Bean(name = "Bean实例名")
    * @Qualifier("数据源名称")
    */
    @Bean(name = "sqlSessionFactoryMaster")
    public SqlSessionFactory sqlSessionFactoryMaster(@Qualifier("master") DataSource datasource)
    throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(datasource);
    bean.setMapperLocations(
    new PathMatchingResourcePatternResolver().getResources(mapperLocation));
    return bean.getObject();
    }
    }

    DruidDataSourceAddressBookConfigurer.java代码如下

    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
    @Configuration
    @MapperScan(basePackages = DruidDataSourceAddressBookConfigurer.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactoryAddress")
    public class DruidDataSourceAddressBookConfigurer {

    /**
    * dao层的包路径
    */
    static final String PACKAGE = "com.dienphuoc.select.user.depart.dao.address_book";

    /**
    * Mapper文件路径
    */
    @Value("${mybatis.mapper-locations}")
    private String mapperLocation;

    @Value("${spring.druid.address.publicKey}")
    private String publicKey;
    @Value("${spring.druid.address.decrypt}")
    private String decrypt;

    /**
    * @ConfigurationProperties(prefix = "配置文件中数据源路径")
    * @Bean(name = "配置文件中数据源名称", initMethod = "init", destroyMethod = "close")
    */
    @ConfigurationProperties(prefix = "spring.druid.address")
    @Bean(name = "address", initMethod = "init", destroyMethod = "close")
    public DruidDataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setProxyFilters(Lists.newArrayList(statFilterAddress(), configFilterAddress()));
    Properties properties = new Properties();
    properties.setProperty("config.decrypt", decrypt);
    properties.setProperty("config.decrypt.key", publicKey);
    dataSource.setConnectProperties(properties);
    return dataSource;
    }

    @Bean("txManagerAddress")
    public DataSourceTransactionManager txManagerAddress() {
    return new DataSourceTransactionManager(dataSource());
    }

    /**
    * 定义空Config类用于加密类的加载
    */
    @Bean
    public ConfigFilter configFilterAddress() {
    ConfigFilter configFilter = new ConfigFilter();
    return configFilter;
    }

    /**
    * 配置了要打印的sql
    */
    @Bean
    public Filter statFilterAddress() {
    StatFilter filter = new StatFilter();
    filter.setSlowSqlMillis(2000);
    filter.setLogSlowSql(true);
    filter.setMergeSql(true);
    return filter;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBeanAddress() {
    return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    }


    /**
    * @Bean(name = "Bean实例名")
    * @Qualifier("数据源名称")
    */
    @Bean(name = "sqlSessionFactoryAddress")
    public SqlSessionFactory sqlSessionFactoryAddress(@Qualifier("address") DataSource datasource)
    throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(datasource);
    bean.setMapperLocations(
    new PathMatchingResourcePatternResolver().getResources(mapperLocation));
    return bean.getObject();
    }
    }
    application.xml配置如下
    druid:
    master:
    url: jdbc:mysql://192.168.250.101:3306/bbb?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: pbbbb
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 1 #初始化大小
    minIdle: 5 #最小连接数
    maxActive: 20 #最大连接数
    maxWait: 60000 #最大等待时间
    validationQuery: select '1' #活性校验SQL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    timeBetweenEvictionRunsMillis: 60000 #配置隔多久进行一次检测(检测可以关闭的空闲连接),检测需要关闭的空闲连接,单位是毫秒
    filters: stat,wall,slf4j #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    publicKey: publicKey
    decrypt: false
    address:
    url: jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: eee
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 1 #初始化大小
    minIdle: 5 #最小连接数
    maxActive: 20 #最大连接数
    maxWait: 60000 #最大等待时间
    validationQuery: select '1' #活性校验SQL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    timeBetweenEvictionRunsMillis: 60000 #配置隔多久进行一次检测(检测可以关闭的空闲连接),检测需要关闭的空闲连接,单位是毫秒
    filters: stat,wall,slf4j #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    publicKey: publicKey
    decrypt: false

    配置完成,结束。
  • 相关阅读:
    【分享】使用Vivado,vck190 BIST 测试,遇到错误“IDCODE/SW CHECK: FAILED”,可以忽略。
    公司预算制定/财务信息化/管理层执行
    税款输入不正确 j2
    发票凭证仍然包含信息
    会计暂估
    委托加工\受托加工凭证处理\会计处理
    记录unknown filesystem type ntfs
    c#多进程通讯,今天,它来了
    多线程通信,IPC,进程通信
    go语言跨平台编译
  • 原文地址:https://www.cnblogs.com/niuniu0108/p/14982940.html
Copyright © 2011-2022 走看看