21

个人学习系列 - Spring Boot 整合 Druid

 3 years ago
source link: https://segmentfault.com/a/1190000024572967
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

1. 搭建一个Spring Boot项目

1.1 pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.24</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
    <scope>compile</scope>
</dependency>

1.2 application.yml

这里将所有的配置都写到了配置文件中了。

server:
  # 端口号
  port: 8002
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://IP地址:3306/数据库名称?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: 用户名
    password: 密码
    #连接池的设置
    druid:
      #初始化时建立物理连接的个数
      initial-size: 5
      #最小连接池数量
      min-idle: 5
      #最大连接池数量
      max-active: 20
      #获取连接时最大等待时间,单位毫秒
      max-wait: 60000
      #申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
      test-while-idle: true
      #既作为检测的间隔时间又作为testWhileIdel执行的依据
      time-between-eviction-runs-millis: 60000
      #销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接
      min-evictable-idle-time-millis: 30000
      #用来检测连接是否有效的sql 必须是一个查询语句
      #mysql中为 select 'x'
      #oracle中为 select 1 from dual
      validation-query: select 'x'
      #申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
      test-on-borrow: false
      #归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
      test-on-return: false
      #是否缓存preparedStatement,mysql5.5+建议开启
      pool-prepared-statements: true
      #当值大于0时poolPreparedStatements会自动修改为true
      max-pool-prepared-statement-per-connection-size: 20
      #配置扩展插件
      filters: stat,wall,log4j
      #通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      #合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true
      # 配置 DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
      # 配置 DruidStatViewServlet
      stat-view-servlet:
        url-pattern: /druid/*
        # IP 白名单,没有配置或者为空,则允许所有访问
        allow: 127.0.0.1
        # IP 黑名单,若白名单也存在,则优先使用
        deny: 192.168.31.253
        # 禁用 HTML 中 Reset All 按钮
        reset-enable: false
        # 登录用户名/密码
        login-username: root
        login-password: 123
        # 启动控制台,默认地址就是ip:端口号/druid/index.html
        enabled: true

  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5Dialect
        format_sql: true
    show-sql: true

1.3 Person实体类

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Integer id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private Integer age;

    @Column(nullable = false)
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

1.4 PersonRepository创建

只是单纯的继承JpaRepository,使用JPA自带的查询方法即可。

public interface PersonRepository extends JpaRepository<Person,Integer> {
}

1.5 PersonController控制器创建

@RestController
public class PersonController {

    @Resource
    PersonRepository personRepository;

    @RequestMapping("/getAll")
    public List<Person> getAll(){
        return personRepository.findAll();
    }

}

2. 测试

2.1 启动项目,访问 http://localhost :8002/druid/

yaMNraj.png!mobile

2.2 运行方法后查看SQL监控

y6z2aeY.png!mobile

恩,大体就这么点事情,别的自己探索吧!

个人博客地址

http://www.zhouzhaodong.xyz

GitHub源码地址

https://github.com/zhouzhaodo...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK