전반적인 웹 개발에 있어서 JPA를 자주 활용할 것 같아 기록으로 남기려고 한다.
build.gradle
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
application.properties:
hibernate의 경우 sql문 로그 남기는 용도이다.
spring.datasource.url=jdbc:mysql://localhost:3306/{데이터베이스 이름}
spring.datasource.username=root
spring.datasource.password={비밀번호}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="{db이름}">
<class>com.sparta.{파일 이름}.entity.{entity이름}</class>
<properties>
</properties>
</persistence-unit>
</persistence>
이렇게 세팅해주면 JPA를 활용해줄 수 있다.
시간 공용 entity를 사용하기 위한 세팅
programApplication.java 파일에
@EnableJpaAuditing
@SpringBootApplication
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Timestamped {
@CreatedDate
@Column(updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime createdAt;
@LastModifiedDate
@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime modifiedAt;
}
위의 작업을 해준다면, extends Timestamped를 통하여 다른 entitiy에 생성시간, 수정시간을 적용하는 코드를 작성할 수 있다.
'Diary > WIL' 카테고리의 다른 글
2024-03-17 WIL - Websocket 에 대해서 (0) | 2024.03.17 |
---|---|
2024-03-10 WIL (0) | 2024.03.10 |
2024-02-25 WIL (0) | 2024.02.25 |
2024-02-18 WIL (0) | 2024.02.18 |
2024-02-11 WIL (0) | 2024.02.11 |