2016년 11월 25일 금요일

Adding sql query log

You can see the sql query log by using log4jdbc.

- Structure
















- application.yml

spring:
  application:
    name: jpa-project
  datasource:
    url: jdbc:log4jdbc:mysql://localhost:3306/test?useSSL=false
    driver-class-name: net.sf.log4jdbc.DriverSpy
    username: user
    password: password
  jpa:
    generate-ddl: true
    hibernate:
      ddl-auto: create-drop
logging:
  level:
    jdbc:
      sqltiming: info
      audit: fatal
      resultset: fatal
      sqlonly: fatal
      connection: fatal

- JpaApplication.java

package org.blog.test;

import org.blog.test.user.entity.User;
import org.blog.test.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class JpaApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }

    @Autowired
    private UserService userService;

    @Override
    public void run(String... args) throws Exception {
        userService.addUser("name");
        User user = userService.getUser("name");
    }
}

- user.java

package org.blog.test.user.entity;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.persistence.*;


@Table
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column
    private String name;

    public User(String name) {
        this.name = name;
    }
}

- UserRepository.java

package org.blog.test.user.repository;

import org.blog.test.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;


public interface UserRepository extends JpaRepository {
    User findByName(String name);
}

- UserService.java

package org.blog.test.user.service;

import org.blog.test.user.entity.User;


public interface UserService {

    User addUser(String name);

    User getUser(String name);
}


- UserServiceImpl.java

package org.blog.test.user.service.impl;

import org.blog.test.user.entity.User;
import org.blog.test.user.repository.UserRepository;
import org.blog.test.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User addUser(String name) {
        return userRepository.save(new User(name));
    }

    @Override
    public User getUser(String name) {
        return userRepository.findByName(name);
    }
}


- build.gradle

apply plugin: 'java'
apply plugin: 'spring-boot'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
    compile group: 'com.googlecode.log4jdbc', name: 'log4jdbc', version: '1.2'
}

- result

2016-11-25 22:33:29.153  INFO 3164 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-11-25 22:33:29.215  INFO 3164 --- [           main] jdbc.sqltiming                           : insert into user (name) values ('name') 
 {executed in 7 msec}
2016-11-25 22:33:29.260  INFO 3164 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2016-11-25 22:33:29.468  INFO 3164 --- [           main] jdbc.sqltiming                           : select user0_.id as id1_0_, user0_.name as name2_0_ from user user0_ where user0_.name='name' 
 {executed in 2 msec}

댓글 없음 :

댓글 쓰기