Skip to content

Commit c0ac13a

Browse files
committed
Using Arthur Gavlyukovskiy's decorator
1 parent 0c57da2 commit c0ac13a

File tree

7 files changed

+235
-0
lines changed

7 files changed

+235
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
**[View Binding/Extracted Params Via Log4J 2](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootLog4j2ViewBindingParameters)**
2+
3+
**Description:** View the prepared statement binding/extracted parameters via Log4J 2 logger setting.
4+
5+
**Key points:**
6+
- for Maven, in `pom.xml`, exclude Spring Boot's Default Logging
7+
- for Maven, in `pom.xml`, Add Log4j 2 Dependency
8+
- in `log4j2.xml` add, `<Logger name="org.hibernate.type.descriptor.sql" level="trace"/>`
9+
10+
**Output example:**
11+
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootLog4j2ViewBindingParameters/log4j2%20display%20binding%20and%20extracted%20parameters.png)
12+
13+
-----------------------------------------------------------------------------------------------------------------------
14+
<table>
15+
<tr><td><b>If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"</b></td><td><b>If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.</b></td></tr>
16+
<tr><td>
17+
<a href="https://www.apress.com/us/book/9781484256251"><p align="left"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Spring%20Boot%20Persistence%20Best%20Practices.jpg" height="500" width="450"/></p></a>
18+
</td><td>
19+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="right"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="500" width="450"/></p></a>
20+
</td></tr></table>
21+
22+
-----------------------------------------------------------------------------------------------------------------------
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>HibernateSpringBootDataSourceDecorator</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootDataSourceDecorator</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.5.7</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
26+
<decorator.version>1.8.0</decorator.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-jdbc</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.github.gavlyukovskiy</groupId>
44+
<artifactId>datasource-proxy-spring-boot-starter</artifactId>
45+
<version>${decorator.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>mysql</groupId>
49+
<artifactId>mysql-connector-java</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
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>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.service.BookstoreService;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class MainApplication {
11+
12+
private final BookstoreService bookstoreService;
13+
14+
public MainApplication(BookstoreService bookstoreService) {
15+
this.bookstoreService = bookstoreService;
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(MainApplication.class, args);
20+
}
21+
22+
@Bean
23+
public ApplicationRunner init() {
24+
return args -> {
25+
26+
bookstoreService.persistAuthor();
27+
bookstoreService.displayAuthor();
28+
};
29+
}
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Author implements Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
private int age;
19+
private String name;
20+
private String genre;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getGenre() {
39+
return genre;
40+
}
41+
42+
public void setGenre(String genre) {
43+
this.genre = genre;
44+
}
45+
46+
public int getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(int age) {
51+
this.age = age;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Author{" + "id=" + id + ", age=" + age
57+
+ ", name=" + name + ", genre=" + genre + '}';
58+
}
59+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Author;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Repository
9+
public interface AuthorRepository extends JpaRepository<Author, Long> {
10+
11+
@Transactional(readOnly=true)
12+
public Author findByName(String name);
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.bookstore.service;
2+
3+
import com.bookstore.entity.Author;
4+
import com.bookstore.repository.AuthorRepository;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Service
9+
public class BookstoreService {
10+
11+
private final AuthorRepository authorRepository;
12+
13+
public BookstoreService(AuthorRepository authorRepository) {
14+
this.authorRepository = authorRepository;
15+
}
16+
17+
public void persistAuthor() {
18+
Author author = new Author();
19+
author.setName("Joana Nimar");
20+
author.setGenre("History");
21+
author.setAge(34);
22+
23+
authorRepository.save(author);
24+
}
25+
26+
public void displayAuthor() {
27+
28+
Author author = authorRepository.findById(1L).orElseThrow();
29+
30+
System.out.println(author);
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNotExist=true
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
5+
logging.level.root=DEBUG
6+
7+
spring.jpa.hibernate.ddl-auto=create
8+
spring.jpa.show-sql=true
9+
10+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
11+
12+
spring.jpa.open-in-view=false

0 commit comments

Comments
 (0)