springcloud 整合 druid 阿里的数据库连接池-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 470975
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

(298)

  • (298)
文章存档

(96)

(201)

(1)

我的朋友
最近访客
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: java

2022-06-06 11:36:46


点击(此处)折叠或打开


  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. /**
  45.  * 说明:第一数据源配置
  46.  * 作者:fh
  47.  * from:fhadmin.cn
  48.  */
  49. @configuration
  50. @mapperscan(basepackages = masterdatasourceconfig.package, sqlsessionfactoryref = "mastersqlsessionfactory") //扫描 mapper 接口并容器管理
  51. public class masterdatasourceconfig {
  52.  
  53.     static final string package = "org.fh.mapper.dsno1";                                //master 目录
  54.     static final string mapper_location = "classpath:mybatis/dsno1/*/*.xml";            //扫描的 xml 目录
  55.     static final string config_location = "classpath:mybatis/dsno1/mybatis-config.xml"; //自定义的mybatis config 文件位置
  56.     static final string type_aliases_package = "org.fh.entity";                         //扫描的 实体类 目录
  57.  
  58.     @value("${datasource.no1.url}")
  59.     private string url;
  60.  
  61.     @value("${datasource.no1.username}")
  62.     private string user;
  63.  
  64.     @value("${datasource.no1.password}")
  65.     private string password;
  66.  
  67.     @value("${datasource.no1.driver-class-name}")
  68.     private string driverclass;
  69.  
  70.     @bean(name = "masterdatasource")
  71.     @primary
  72.     public datasource masterdatasource() {
  73.         druiddatasource datasource = new druiddatasource();
  74.         datasource.setdriverclassname(driverclass);
  75.         datasource.seturl(url);
  76.         datasource.setusername(user);
  77.         datasource.setpassword(password);
  78.         return datasource;
  79.     }
  80.  
  81.     @bean(name = "mastertransactionmanager")
  82.     @primary
  83.     public datasourcetransactionmanager mastertransactionmanager() {
  84.         return new datasourcetransactionmanager(masterdatasource());
  85.     }
  86.  
  87.     @bean(name = "mastersqlsessionfactory")
  88.     @primary
  89.     public sqlsessionfactory mastersqlsessionfactory(@qualifier("masterdatasource") datasource masterdatasource)throws exception {
  90.         final sqlsessionfactorybean sessionfactory = new sqlsessionfactorybean();
  91.         sessionfactory.setdatasource(masterdatasource);
  92.         sessionfactory.setmapperlocations(new pathmatchingresourcepatternresolver().getresources(masterdatasourceconfig.mapper_location));
  93.         sessionfactory.setconfiglocation(new defaultresourceloader().getresource(masterdatasourceconfig.config_location));
  94.         sessionfactory.settypealiasespackage(masterdatasourceconfig.type_aliases_package);
  95.         return sessionfactory.getobject();
  96.     }
  97. }



阅读(3093) | 评论(0) | 转发(0) |
2

上一篇:

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图