Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613645
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-03-31 11:28:39


点击(此处)折叠或打开


  1.  1.配置pom

  2.      <!-- druid 数据库连接池 -->
  3.      <dependency>
  4.             <groupId>com.alibaba</groupId>
  5.             <artifactId>druid</artifactId>
  6.             <version>1.1.12</version>
  7.             <exclusions>
  8.          <exclusion>
  9.          <groupId>com.alibaba</groupId>
  10.          <artifactId>jconsole</artifactId>
  11.          </exclusion>
  12.          <exclusion>
  13.          <groupId>com.alibaba</groupId>
  14.          <artifactId>tools</artifactId>
  15.          </exclusion>
  16.          </exclusions>
  17.         </dependency>

  18. 2.配置 application.properties

  19. #druid连接池
  20. spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
  21. #最大活跃数
  22. spring.datasource.maxActive: 20
  23. #初始化数量
  24. spring.datasource.initialSize: 1
  25. #最大连接等待超时时间
  26. spring.datasource.maxWait: 60000
  27. #打开PSCache,并且指定每个连接PSCache的大小
  28. spring.datasource.poolPreparedStatements: true
  29. spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
  30. #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
  31. #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  32. spring.datasource.minIdle: 1
  33. spring.datasource.timeBetweenEvictionRunsMillis: 60000
  34. spring.datasource.minEvictableIdleTimeMillis: 300000
  35. spring.datasource.validationQuery: select 1 from dual
  36. spring.datasource.testWhileIdle: true
  37. spring.datasource.testOnBorrow: false
  38. spring.datasource.testOnReturn: false
  39. #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
  40. filters: stat, wall, log4j

  41. 3.Config配置类



  42. /**
  43.  * 说明:第一数据源配置
  44.  * 作者:FH
  45.  * from:fhadmin.cn
  46.  */
  47. @Configuration
  48. @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") //扫描 Mapper 接口并容器管理
  49. public class MasterDataSourceConfig {

  50.     static final String PACKAGE = "org.fh.mapper.dsno1";                                //master 目录
  51.     static final String MAPPER_LOCATION = "classpath:mybatis/dsno1/*/*.xml";            //扫描的 xml 目录
  52.     static final String CONFIG_LOCATION = "classpath:mybatis/dsno1/mybatis-config.xml"; //自定义的mybatis config 文件位置
  53.     static final String TYPE_ALIASES_PACKAGE = "org.fh.entity";                         //扫描的 实体类 目录
  54.  
  55.     @Value("${datasource.no1.url}")
  56.     private String url;
  57.  
  58.     @Value("${datasource.no1.username}")
  59.     private String user;
  60.  
  61.     @Value("${datasource.no1.password}")
  62.     private String password;
  63.  
  64.     @Value("${datasource.no1.driver-class-name}")
  65.     private String driverClass;
  66.  
  67.     @Bean(name = "masterDataSource")
  68.     @Primary
  69.     public DataSource masterDataSource() {
  70.         DruidDataSource dataSource = new DruidDataSource();
  71.         dataSource.setDriverClassName(driverClass);
  72.         dataSource.setUrl(url);
  73.         dataSource.setUsername(user);
  74.         dataSource.setPassword(password);
  75.         return dataSource;
  76.     }
  77.  
  78.     @Bean(name = "masterTransactionManager")
  79.     @Primary
  80.     public DataSourceTransactionManager masterTransactionManager() {
  81.         return new DataSourceTransactionManager(masterDataSource());
  82.     }
  83.  
  84.     @Bean(name = "masterSqlSessionFactory")
  85.     @Primary
  86.     public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {
  87.         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  88.         sessionFactory.setDataSource(masterDataSource);
  89.         sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
  90.         sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION));
  91.         sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
  92.         return sessionFactory.getObject();
  93.     }
  94. }



  95.  



阅读(799) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~