zoukankan      html  css  js  c++  java
  • Spring boot+JPA+Druid

    pom.xml
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.yginsight</groupId>
    6. <artifactId>pm_admin</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <packaging>jar</packaging>
    9. <name>pm_admin</name>
    10. <description>Power Manager Admin</description>
    11. <parent>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-starter-parent</artifactId>
    14. <version>1.5.2.RELEASE</version>
    15. <relativePath/> <!-- lookup parent from repository -->
    16. </parent>
    17. <properties>
    18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    20. <java.version>1.8</java.version>
    21. </properties>
    22. <dependencies>
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-web</artifactId>
    26. </dependency>
    27. <dependency>
    28. <groupId>org.springframework.boot</groupId>
    29. <artifactId>spring-boot-starter-test</artifactId>
    30. <scope>test</scope>
    31. </dependency>
    32. <dependency>
    33. <groupId>com.alibaba</groupId>
    34. <artifactId>druid</artifactId>
    35. <version>1.0.26</version>
    36. </dependency>
    37. <dependency>
    38. <groupId>org.springframework.boot</groupId>
    39. <artifactId>spring-boot-configuration-processor</artifactId>
    40. <optional>true</optional>
    41. </dependency>
    42. <dependency>
    43. <groupId>org.springframework.boot</groupId>
    44. <artifactId>spring-boot-starter-data-jpa</artifactId>
    45. </dependency>
    46. <!-- postgresql连接 -->
    47. <dependency>
    48. <groupId>org.postgresql</groupId>
    49. <artifactId>postgresql</artifactId>
    50. <version>9.4-1201-jdbc41</version>
    51. </dependency>
    52. <!-- Springboot 热部署 -->
    53. <dependency>
    54. <groupId>org.springframework.boot</groupId>
    55. <artifactId>spring-boot-devtools</artifactId>
    56. <optional>true</optional>
    57. </dependency>
    58. </dependencies>
    59. <build>
    60. <plugins>
    61. <plugin>
    62. <groupId>org.springframework.boot</groupId>
    63. <artifactId>spring-boot-maven-plugin</artifactId>
    64. </plugin>
    65. </plugins>
    66. </build>
    67. </project>
    application.properties
    1. # 数据库访问配置
    2. # 主数据源,默认的
    3. #spring.datasource.druid.type=com.alibaba.druid.pool.DruidDataSource
    4. spring.datasource.druid.driver-class-name=org.postgresql.Driver
    5. spring.datasource.druid.url=jdbc:postgresql://127.0.0.1/powermanager
    6. spring.datasource.druid.username=postgres
    7. spring.datasource.druid.password=root
    8. # 下面为连接池的补充设置,应用到上面所有数据源中
    9. # 初始化大小,最小,最大
    10. spring.datasource.druid.initial-size=5
    11. spring.datasource.druid.minIdle=5
    12. spring.datasource.druid.maxActive=20
    13. # 配置获取连接等待超时的时间
    14. spring.datasource.druid.maxWait=60000
    15. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    16. spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
    17. # 配置一个连接在池中最小生存的时间,单位是毫秒
    18. spring.datasource.druid.minEvictableIdleTimeMillis=300000
    19. spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
    20. spring.datasource.druid.testWhileIdle=true
    21. spring.datasource.druid.testOnBorrow=false
    22. spring.datasource.druid.testOnReturn=false
    23. # 打开PSCache,并且指定每个连接上PSCache的大小
    24. spring.datasource.druid.poolPreparedStatements=true
    25. spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
    26. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    27. spring.datasource.druid.filters=stat,wall,log4j
    28. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    29. spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    30. # 合并多个DruidDataSource的监控数据
    31. #spring.datasource.druid.useGlobalDataSourceStat=true

    由于上述配置文件中,Spring.datasource.druid无法被识别,需要自行增加配置项
    增加两个配置相关类
    1. package com.yginsight.druid;
    2. import com.alibaba.druid.pool.DruidDataSource;
    3. import com.alibaba.druid.support.http.StatViewServlet;
    4. import com.alibaba.druid.support.http.WebStatFilter;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    7. import org.springframework.boot.context.properties.EnableConfigurationProperties;
    8. import org.springframework.boot.web.servlet.FilterRegistrationBean;
    9. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    10. import org.springframework.context.annotation.Bean;
    11. import org.springframework.context.annotation.Configuration;
    12. import javax.sql.DataSource;
    13. import java.sql.SQLException;
    14. /**
    15. * Created by dingshuo on 2017/4/13.
    16. */
    17. @Configuration
    18. @EnableConfigurationProperties({DruidDataSourceProperties.class})
    19. public class DruidConfiguration {
    20. @Autowired
    21. private DruidDataSourceProperties properties;
    22. @Bean
    23. @ConditionalOnMissingBean
    24. public DataSource druidDataSource() {
    25. DruidDataSource druidDataSource = new DruidDataSource();
    26. druidDataSource.setDriverClassName(properties.getDriverClassName());
    27. druidDataSource.setUrl(properties.getUrl());
    28. druidDataSource.setUsername(properties.getUsername());
    29. druidDataSource.setPassword(properties.getPassword());
    30. druidDataSource.setInitialSize(properties.getInitialSize());
    31. druidDataSource.setMinIdle(properties.getMinIdle());
    32. druidDataSource.setMaxActive(properties.getMaxActive());
    33. druidDataSource.setMaxWait(properties.getMaxWait());
    34. druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
    35. druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
    36. druidDataSource.setValidationQuery(properties.getValidationQuery());
    37. druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
    38. druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
    39. druidDataSource.setTestOnReturn(properties.isTestOnReturn());
    40. druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
    41. druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
    42. druidDataSource.setConnectionProperties(properties.getConnectionProperties());
    43. try {
    44. druidDataSource.setFilters(properties.getFilters());
    45. druidDataSource.init();
    46. } catch (SQLException e) {
    47. e.printStackTrace();
    48. }
    49. return druidDataSource;
    50. }
    51. @Bean
    52. @ConditionalOnMissingBean
    53. public ServletRegistrationBean druidServlet() {
    54. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    55. //添加初始化参数:initParams
    56. //白名单:
    57. //servletRegistrationBean.addInitParameter("allow","127.0.0.1");
    58. //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
    59. //servletRegistrationBean.addInitParameter("deny","192.168.1.73");
    60. //登录查看信息的账号密码.
    61. servletRegistrationBean.addInitParameter("loginUsername", "admin");
    62. servletRegistrationBean.addInitParameter("loginPassword", "admin");
    63. //是否能够重置数据.
    64. servletRegistrationBean.addInitParameter("resetEnable", "true");
    65. return servletRegistrationBean;
    66. }
    67. @Bean
    68. @ConditionalOnMissingBean
    69. public FilterRegistrationBean filterRegistrationBean() {
    70. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    71. filterRegistrationBean.setFilter(new WebStatFilter());
    72. filterRegistrationBean.addUrlPatterns("/*");
    73. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    74. return filterRegistrationBean;
    75. }
    76. }
    1. package com.yginsight.druid;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. /**
    4. * Created by dingshuo on 2017/4/13.
    5. */
    6. @ConfigurationProperties(prefix = "spring.datasource.druid")
    7. public class DruidDataSourceProperties {
    8. private String driverClassName;
    9. private String url;
    10. private String username;
    11. private String password;
    12. private int initialSize;
    13. private int minIdle;
    14. private int maxActive;
    15. private long maxWait;
    16. private long timeBetweenEvictionRunsMillis;
    17. private long minEvictableIdleTimeMillis;
    18. private String validationQuery;
    19. private boolean testWhileIdle;
    20. private boolean testOnBorrow;
    21. private boolean testOnReturn;
    22. private boolean poolPreparedStatements;
    23. private int maxPoolPreparedStatementPerConnectionSize;
    24. private String filters;
    25. private String connectionProperties;
    26. public int getInitialSize() {
    27. return initialSize;
    28. }
    29. public void setInitialSize(int initialSize) {
    30. this.initialSize = initialSize;
    31. }
    32. public int getMinIdle() {
    33. return minIdle;
    34. }
    35. public void setMinIdle(int minIdle) {
    36. this.minIdle = minIdle;
    37. }
    38. public int getMaxActive() {
    39. return maxActive;
    40. }
    41. public void setMaxActive(int maxActive) {
    42. this.maxActive = maxActive;
    43. }
    44. public long getMaxWait() {
    45. return maxWait;
    46. }
    47. public void setMaxWait(long maxWait) {
    48. this.maxWait = maxWait;
    49. }
    50. public long getTimeBetweenEvictionRunsMillis() {
    51. return timeBetweenEvictionRunsMillis;
    52. }
    53. public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
    54. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    55. }
    56. public long getMinEvictableIdleTimeMillis() {
    57. return minEvictableIdleTimeMillis;
    58. }
    59. public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
    60. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    61. }
    62. public String getValidationQuery() {
    63. return validationQuery;
    64. }
    65. public void setValidationQuery(String validationQuery) {
    66. this.validationQuery = validationQuery;
    67. }
    68. public boolean isTestWhileIdle() {
    69. return testWhileIdle;
    70. }
    71. public void setTestWhileIdle(boolean testWhileIdle) {
    72. this.testWhileIdle = testWhileIdle;
    73. }
    74. public boolean isTestOnBorrow() {
    75. return testOnBorrow;
    76. }
    77. public void setTestOnBorrow(boolean testOnBorrow) {
    78. this.testOnBorrow = testOnBorrow;
    79. }
    80. public boolean isTestOnReturn() {
    81. return testOnReturn;
    82. }
    83. public void setTestOnReturn(boolean testOnReturn) {
    84. this.testOnReturn = testOnReturn;
    85. }
    86. public boolean isPoolPreparedStatements() {
    87. return poolPreparedStatements;
    88. }
    89. public void setPoolPreparedStatements(boolean poolPreparedStatements) {
    90. this.poolPreparedStatements = poolPreparedStatements;
    91. }
    92. public int getMaxPoolPreparedStatementPerConnectionSize() {
    93. return maxPoolPreparedStatementPerConnectionSize;
    94. }
    95. public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
    96. this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
    97. }
    98. public String getFilters() {
    99. return filters;
    100. }
    101. public void setFilters(String filters) {
    102. this.filters = filters;
    103. }
    104. public String getConnectionProperties() {
    105. return connectionProperties;
    106. }
    107. public void setConnectionProperties(String connectionProperties) {
    108. this.connectionProperties = connectionProperties;
    109. }
    110. public String getDriverClassName() {
    111. return driverClassName;
    112. }
    113. public void setDriverClassName(String driverClassName) {
    114. this.driverClassName = driverClassName;
    115. }
    116. public String getUrl() {
    117. return url;
    118. }
    119. public void setUrl(String url) {
    120. this.url = url;
    121. }
    122. public String getUsername() {
    123. return username;
    124. }
    125. public void setUsername(String username) {
    126. this.username = username;
    127. }
    128. public String getPassword() {
    129. return password;
    130. }
    131. public void setPassword(String password) {
    132. this.password = password;
    133. }
    134. }

  • 相关阅读:
    [搬运工系列]-JMeter(十三)压力测试--数据库(mysql)压力测试
    [搬运工系列]-JMeter(十二)处理Cookie与Session
    [搬运工系列]-JMeter(十一)问题与方案--内存溢出解决方法
    [搬运工系列]-JMeter(十) 命令行(非GUI)模式
    [搬运工系列]-JMeter (九)分布式测试
    [搬运工系列]-JMeter(八)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults
    Python-Selenium 之控制浏览器的操作,WebDriver常用方法
    Selenium常见的无法定位到元素的原因
    接口测试实战
    jmeter做websocket协议的接口测试
  • 原文地址:https://www.cnblogs.com/tilv37/p/6703122.html
Copyright © 2011-2022 走看看