728x90

pom.xml

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

Druid는 기본적으로 spring.datasource.druid 접두사를 사용한다.

application.properties

spring.datasource.druid.url=jdbc:mysql://localhost:3306/test
spring.datasource.druid.username=root
spring.datasource.druid.password=
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
# connection pool
spring.datasource.druid.initialSize=10
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.minIdle=1
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=true
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxOpenPreparedStatements=20
spring.datasource.druid.validationQuery=SELECT 1
spring.datasource.druid.validation-query-timeout=500
spring.datasource.druid.filters=stat

혹은 yml 파일일시

spring:
    datasource:
        druid:
            driver-class-name: com.mysql.cj.jdbc.Driver
            filters: stat
            initialSize: 10
            maxActive: 20
            maxOpenPreparedStatements: 20
            maxWait: 60000
            minEvictableIdleTimeMillis: 300000
            minIdle: 1
            password: ''
            poolPreparedStatements: true
            testOnBorrow: true
            testOnReturn: false
            testWhileIdle: true
            timeBetweenEvictionRunsMillis: 60000
            url: jdbc:mysql://localhost:3306/test
            username: root
            validation-query-timeout: 500
            validationQuery: SELECT 1

Druid 모니터링 라이브러리 적용

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

application.properties 값 추가

spring.datasource.druid.stat-view-servlet.enabled=true          # 
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*  # address 규칙
spring.datasource.druid.stat-view-servlet.reset-enable=true     # 통계데이터 리셋여부
spring.datasource.druid.stat-view-servlet.login-username=admin  # 모니터링 화면로그인 계정
spring.datasource.druid.stat-view-servlet.login-password=admin  # 모니터링 화면로그인 비번

UserService 클래스를 호출할 controller 를 작성

@Data
@AllArgsConstructor
@RestController
public class UserController {

    private UserService userService;

    @PostMapping("/user")
    public int create(@RequestBody User user) {
        return userService.create(user.getName(), user.getAge());
    }

    @GetMapping("/user/{name}")
    public List<User> getByName(@PathVariable String name) {
        return userService.getByName(name);
    }

    @DeleteMapping("/user/{name}")
    public int deleteByName(@PathVariable String name) {
        return userService.deleteByName(name);
    }

    @GetMapping("/user/count")
    public int getAllUsers() {
        return userService.getAllUsers();
    }

    @DeleteMapping("/user/all")
    public int deleteAllUsers() {
        return userService.deleteAllUsers();
    }

}

아래 주소 입력 http://localhost:8080/druid/

'Springboot2.x 강좌 > DB연결' 카테고리의 다른 글

XML 로 Mybatis 설정하기  (0) 2023.01.05
Mybatis 로 Mysql 연결하기  (0) 2023.01.05
Spring Data JPA  (0) 2023.01.05
Hikari 설정  (0) 2023.01.04
JdbcTemplate 로 db 접근해보자  (0) 2023.01.04

+ Recent posts