add jpa module

This commit is contained in:
2024-02-28 16:39:48 +09:00
parent 32a7d91a1d
commit d2bb77ca33
17 changed files with 337 additions and 6 deletions

29
jpa/README.md Normal file
View File

@@ -0,0 +1,29 @@
# spring-boot-validation-examples
```kotlin
implementation ("org.springframework.boot:spring-boot-starter-validation")
```
## Annotations
* @Digits
* @Email
* @Max
* @Min
* @Negative
* @NotBlank
* @NotEmpty
* @NotNull
* @Null
* @Pattern
* @Positive
* @Size
https://docs.jboss.org/hibernate/beanvalidation/spec/2.0/api/
---
developed by Elex
https://www.elex-project.com

34
jpa/build.gradle.kts Normal file
View File

@@ -0,0 +1,34 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2024. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-spring-boot")
id("org.springframework.boot") version "3.2.3"
id("io.spring.dependency-management") version "1.1.4"
}
java {
sourceCompatibility = JavaVersion.VERSION_17
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
// https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
implementation ("org.springframework.boot:spring-boot-starter-validation")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

View File

@@ -0,0 +1,20 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024. Elex. All Rights Reesrved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
@Slf4j
@Configuration
@EnableScheduling
public class Config {
@Autowired
private PersonRepository repository;
@Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
public void task1(){
log.info("TASK executed.");
repository.deleteAllInBatch(repository
.findByCreatedAtLessThan(LocalDateTime.now().minusMinutes(2)));
}
}

View File

@@ -0,0 +1,42 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
public class MyController {
@Autowired
private PersonRepository repository;
@GetMapping({"/"})
public String index() {
return "home";
}
@GetMapping({"/list"})
public List<PersonDto> list() {
return repository.findAll().stream()
.map(PersonDto::of)
.collect(Collectors.toList());
}
@GetMapping(value = {"/add/{name}"})
public PersonDto addPerson(@PathVariable String name) {
Person person = repository.save(Person.builder().name(name).build());
return PersonDto.of(person);
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021-2024. Elex. All Rights Reesrved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import java.time.LocalDateTime;
@Data
@Entity
@AllArgsConstructor@NoArgsConstructor
@Builder
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private String name;
@Column
@CreationTimestamp
private LocalDateTime createdAt;
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024. Elex. All Rights Reesrved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor@AllArgsConstructor
@Builder
public class PersonDto {
@JsonProperty
private Long id;
@JsonProperty
private String name;
@JsonProperty
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime createdAt;
public static PersonDto of(Person person){
return PersonDto.builder()
.id(person.getId())
.name(person.getName())
.createdAt(person.getCreatedAt())
.build();
}
}

View File

@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024. Elex. All Rights Reesrved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.List;
@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
public List<Person> findByCreatedAtLessThan(LocalDateTime date);
}

View File

@@ -0,0 +1,8 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,22 @@
spring:
application:
name: My spring-boot project
datasource:
url: jdbc:mariadb://localhost:3306/test
driver-class-name: org.mariadb.jdbc.Driver
username: testuser
password: _test_
jpa:
open-in-view: false
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MariaDBDialect
thymeleaf:
prefix: classpath:/view/
suffix: .html
server:
port: 8080

View File

@@ -0,0 +1,10 @@
('-. ('-. ) (`-.
_( OO) _( OO) ( OO ).
(,------.,--. (,------.(_/. \_)-.
| .---'| |.-') | .---' \ `.' /
| | | | OO ) | | \ /\
(| '--. | |`-' |(| '--. \ \ |
| .--'(| '---.' | .--' .' \_)
| `---.| | | `---. / .'. \
`------'`------' `------''--' '--'
powered by ELEX

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Spring-boot Examples
~
~ Copyright (c) 2020-2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProperty name="LOG_DIR" source="logging.path"
defaultValue="${user.home}/logs"/>
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_PATH}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>60</maxHistory>
</rollingPolicy>
</appender>
<logger name="kr.pe.elex" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</configuration>

View File

@@ -0,0 +1 @@
<h1>Test</h1>