260703_01_FST: First Commit

This commit is contained in:
2026-07-03 16:20:48 +09:00
commit 34b8d41a28
55 changed files with 2127 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package __BASE_PACKAGE__;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
@SpringBootApplication
@ConfigurationPropertiesScan
public class __APPLICATION_CLASS__ {
public static void main(String[] args) {
SpringApplication.run(__APPLICATION_CLASS__.class, args);
}
}

View File

@@ -0,0 +1,9 @@
package __BASE_PACKAGE__.common.consts;
public final class ExampleConstant {
public static final int DEFAULT_BATCH_SIZE = 1_000;
private ExampleConstant() {
}
}

View File

@@ -0,0 +1,6 @@
package __BASE_PACKAGE__.common.enums;
public enum ExampleStatus {
ACTIVE,
INACTIVE
}

View File

@@ -0,0 +1,13 @@
package __BASE_PACKAGE__.common.util;
public final class StringNormalizer {
private StringNormalizer() {
}
public static String trimToNull(String value) {
if (value == null) return null;
String normalized = value.trim();
return normalized.isEmpty() ? null : normalized;
}
}

View File

@@ -0,0 +1,4 @@
/**
* 프로젝트 전용 Bean Validation annotation과 validator를 배치합니다.
*/
package __BASE_PACKAGE__.common.validation;

View File

@@ -0,0 +1,14 @@
package __BASE_PACKAGE__.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Getter
@Setter
@ConfigurationProperties(prefix = "kri.file")
public class FileStorageProperties {
private String baseDir = "./upload-file";
private String resourcePattern = "/files/**";
}

View File

@@ -0,0 +1,15 @@
package __BASE_PACKAGE__.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration
public class JdbcConfig {
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

View File

@@ -0,0 +1,19 @@
package __BASE_PACKAGE__.config;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class QueryDSLConfig {
private final EntityManager entityManager;
@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}

View File

@@ -0,0 +1,26 @@
package __BASE_PACKAGE__.config;
import java.nio.file.Path;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@RequiredArgsConstructor
public class WebResourceConfig implements WebMvcConfigurer {
private final FileStorageProperties fileStorageProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String location = Path.of(fileStorageProperties.getBaseDir())
.toAbsolutePath()
.normalize()
.toUri()
.toString();
registry.addResourceHandler(fileStorageProperties.getResourcePattern())
.addResourceLocations(location.endsWith("/") ? location : location + "/");
}
}

View File

@@ -0,0 +1,38 @@
package __BASE_PACKAGE__.controller;
import __BASE_PACKAGE__.dto.request.CreateExampleRequest;
import __BASE_PACKAGE__.dto.response.ExampleResponse;
import __BASE_PACKAGE__.service.ExampleService;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/examples")
public class ExampleController {
private final ExampleService exampleService;
@GetMapping
public List<ExampleResponse> search(
@RequestParam(defaultValue = "") String keyword,
@RequestParam(defaultValue = "20") int limit
) {
return exampleService.search(keyword, limit);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ExampleResponse create(@Valid @RequestBody CreateExampleRequest request) {
return exampleService.create(request);
}
}

View File

@@ -0,0 +1,14 @@
package __BASE_PACKAGE__.controller;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PingController {
@GetMapping("/ping")
public Map<String, String> ping() {
return Map.of("status", "ok");
}
}

View File

@@ -0,0 +1,12 @@
package __BASE_PACKAGE__.dto.request;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
public record CreateExampleRequest(
@NotNull @Positive Long groupId,
@NotNull @Positive Integer sequence,
@NotBlank String name
) {
}

View File

@@ -0,0 +1,19 @@
package __BASE_PACKAGE__.dto.response;
import __BASE_PACKAGE__.persistence.entity.ExampleEntity;
public record ExampleResponse(
Long groupId,
Integer sequence,
String name,
String status
) {
public static ExampleResponse from(ExampleEntity entity) {
return new ExampleResponse(
entity.getId().getGroupId(),
entity.getId().getSequence(),
entity.getName(),
entity.getStatus().name()
);
}
}

View File

@@ -0,0 +1,42 @@
package __BASE_PACKAGE__.persistence.entity;
import __BASE_PACKAGE__.common.enums.ExampleStatus;
import __BASE_PACKAGE__.persistence.entity.id.ExampleEntityId;
import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@Entity
@Table(name = "t_example")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ExampleEntity {
@EmbeddedId
private ExampleEntityId id;
@Column(name = "name", nullable = false, length = 100)
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false, length = 20)
private ExampleStatus status;
@Builder
public ExampleEntity(ExampleEntityId id, String name, ExampleStatus status) {
this.id = id;
this.name = name;
this.status = status == null ? ExampleStatus.ACTIVE : status;
}
public void changeName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,28 @@
package __BASE_PACKAGE__.persistence.entity.id;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.io.Serial;
import java.io.Serializable;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@Embeddable
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ExampleEntityId implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Column(name = "group_id", nullable = false)
private Long groupId;
@Column(name = "sequence", nullable = false)
private Integer sequence;
}

View File

@@ -0,0 +1,9 @@
package __BASE_PACKAGE__.persistence.jdbc;
public record ExampleBulkItem(
Long groupId,
Integer sequence,
String name,
String status
) {
}

View File

@@ -0,0 +1,47 @@
package __BASE_PACKAGE__.persistence.jdbc;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
@RequiredArgsConstructor
public class ExampleBulkJdbcRepository {
private final JdbcTemplate jdbcTemplate;
public void insertAll(List<ExampleBulkItem> items, int batchSize) {
if (items == null || items.isEmpty()) return;
String sql = """
insert into t_example (group_id, sequence, name, status)
values (?, ?, ?, ?)
""";
int safeBatchSize = Math.max(batchSize, 1);
for (int from = 0; from < items.size(); from += safeBatchSize) {
int to = Math.min(from + safeBatchSize, items.size());
List<ExampleBulkItem> batch = items.subList(from, to);
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement statement, int index) throws SQLException {
ExampleBulkItem item = batch.get(index);
statement.setLong(1, item.groupId());
statement.setInt(2, item.sequence());
statement.setString(3, item.name());
statement.setString(4, item.status());
}
@Override
public int getBatchSize() {
return batch.size();
}
});
}
}
}

View File

@@ -0,0 +1,11 @@
package __BASE_PACKAGE__.persistence.mapper;
import __BASE_PACKAGE__.dto.response.ExampleResponse;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface ExampleMapper {
List<ExampleResponse> findByName(@Param("keyword") String keyword);
}

View File

@@ -0,0 +1,15 @@
package __BASE_PACKAGE__.persistence.query;
import __BASE_PACKAGE__.persistence.entity.QExampleEntity;
import com.querydsl.core.types.dsl.BooleanExpression;
public final class ExamplePredicates {
private ExamplePredicates() {
}
public static BooleanExpression nameContains(QExampleEntity example, String keyword) {
if (keyword == null || keyword.isBlank()) return null;
return example.name.containsIgnoreCase(keyword.trim());
}
}

View File

@@ -0,0 +1,8 @@
package __BASE_PACKAGE__.persistence.repository;
import __BASE_PACKAGE__.persistence.entity.ExampleEntity;
import java.util.List;
public interface ExampleQueryRepository {
List<ExampleEntity> findByNameContains(String keyword, int limit);
}

View File

@@ -0,0 +1,25 @@
package __BASE_PACKAGE__.persistence.repository;
import static __BASE_PACKAGE__.persistence.entity.QExampleEntity.exampleEntity;
import __BASE_PACKAGE__.persistence.entity.ExampleEntity;
import __BASE_PACKAGE__.persistence.query.ExamplePredicates;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class ExampleQueryRepositoryImpl implements ExampleQueryRepository {
private final JPAQueryFactory jpaQueryFactory;
@Override
public List<ExampleEntity> findByNameContains(String keyword, int limit) {
return jpaQueryFactory
.selectFrom(exampleEntity)
.where(ExamplePredicates.nameContains(exampleEntity, keyword))
.orderBy(exampleEntity.id.groupId.asc(), exampleEntity.id.sequence.asc())
.limit(Math.max(limit, 1))
.fetch();
}
}

View File

@@ -0,0 +1,9 @@
package __BASE_PACKAGE__.persistence.repository;
import __BASE_PACKAGE__.persistence.entity.ExampleEntity;
import __BASE_PACKAGE__.persistence.entity.id.ExampleEntityId;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ExampleRepository
extends JpaRepository<ExampleEntity, ExampleEntityId>, ExampleQueryRepository {
}

View File

@@ -0,0 +1,21 @@
package __BASE_PACKAGE__.scheduler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@ConditionalOnProperty(
prefix = "app.scheduler.example",
name = "enabled",
havingValue = "true"
)
public class ExampleScheduler {
@Scheduled(cron = "${app.scheduler.example.cron:0 0 1 * * *}")
public void execute() {
log.info("Example scheduler executed");
}
}

View File

@@ -0,0 +1,37 @@
package __BASE_PACKAGE__.service;
import __BASE_PACKAGE__.common.enums.ExampleStatus;
import __BASE_PACKAGE__.dto.request.CreateExampleRequest;
import __BASE_PACKAGE__.dto.response.ExampleResponse;
import __BASE_PACKAGE__.persistence.entity.ExampleEntity;
import __BASE_PACKAGE__.persistence.entity.id.ExampleEntityId;
import __BASE_PACKAGE__.persistence.repository.ExampleRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ExampleService {
private final ExampleRepository exampleRepository;
@Transactional
public ExampleResponse create(CreateExampleRequest request) {
ExampleEntity entity = ExampleEntity.builder()
.id(new ExampleEntityId(request.groupId(), request.sequence()))
.name(request.name())
.status(ExampleStatus.ACTIVE)
.build();
return ExampleResponse.from(exampleRepository.save(entity));
}
public List<ExampleResponse> search(String keyword, int limit) {
return exampleRepository.findByNameContains(keyword == null ? "" : keyword, limit)
.stream()
.map(ExampleResponse::from)
.toList();
}
}

View File

@@ -0,0 +1,10 @@
spring:
datasource:
url: "${DB_URL}"
username: "${DB_USERNAME}"
password: "${DB_PASSWORD}"
driver-class-name: __DATABASE_DRIVER__
logging:
level:
org.hibernate.SQL: DEBUG

View File

@@ -0,0 +1,6 @@
spring:
datasource:
url: "${DB_URL:__DATABASE_LOCAL_URL__}"
username: "${DB_USERNAME:sample}"
password: "${DB_PASSWORD:sample}"
driver-class-name: __DATABASE_DRIVER__

View File

@@ -0,0 +1,10 @@
spring:
datasource:
url: "${DB_URL}"
username: "${DB_USERNAME}"
password: "${DB_PASSWORD}"
driver-class-name: __DATABASE_DRIVER__
kri:
swagger:
enabled: ${KRI_SWAGGER_ENABLED:false}

View File

@@ -0,0 +1,71 @@
server:
port: __PORT__
forward-headers-strategy: framework
spring:
application:
name: __PROJECT_NAME__
profiles:
default: local
jpa:
open-in-view: false
hibernate:
ddl-auto: none
properties:
hibernate:
jdbc:
batch_size: 1000
order_inserts: true
order_updates: true
format_sql: true
servlet:
multipart:
max-file-size: 20MB
max-request-size: 20MB
logging:
file:
path: ./logs
kri:
auth:
apikey:
enabled: ${KRI_API_KEY_ENABLED:false}
key: ${KRI_API_KEY:}
header-name: ${KRI_API_KEY_HEADER_NAME:X-API-Key}
service-code: ${KRI_API_KEY_SERVICE_CODE:__PROJECT_NAME__}
preload-on-startup: ${KRI_API_KEY_PRELOAD_ON_STARTUP:false}
admin-api-enabled: ${KRI_API_KEY_ADMIN_ENABLED:false}
admin-allow-ips: []
security:
enabled: ${KRI_SECURITY_ENABLED:true}
public-paths:
- /v3/api-docs/**
- /swagger-ui.html
- /swagger-ui/**
- /ping
- /files/**
swagger:
enabled: ${KRI_SWAGGER_ENABLED:true}
title: "__PROJECT_NAME__ API"
description: "__DESCRIPTION__"
version: "v1.0.0"
base-package: "__BASE_PACKAGE__"
allowed-ips: []
file:
base-dir: ${FILE_STORAGE_BASE_DIR:./upload-file}
resource-pattern: /files/**
web:
enabled: true
redis:
enabled: false
scheduling:
enabled: true
app:
scheduler:
example:
enabled: ${EXAMPLE_SCHEDULER_ENABLED:false}
cron: ${EXAMPLE_SCHEDULER_CRON:0 0 1 * * *}
__MYBATIS_CONFIG__

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="__BASE_PACKAGE__.persistence.mapper.ExampleMapper">
<select id="findByName" resultType="__BASE_PACKAGE__.dto.response.ExampleResponse">
select
group_id as groupId,
sequence,
name,
status
from t_example
where name like concat('%', #{keyword}, '%')
order by group_id, sequence
</select>
</mapper>

View File

@@ -0,0 +1,13 @@
package __BASE_PACKAGE__;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class PackageStructureTest {
@Test
void templateIsGenerated() {
assertTrue(true);
}
}