스파르타 코딩 클럽 | 자바 심화 과정/Spring Master (입문 주차)

persistence.xml에 넣은 설정 + gradle config 열기 + jpa hibernate 설정

luminous_dev 2025. 2. 2. 01:50
<?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="memo">
        <class>com.sparta.entity.Memo</class>
        <properties>
            <!--Spring이 아닌 순수 java 환경이라 해주는 설정-->
            <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="jakarta.persistence.jdbc.user" value="root"/>
            <property name="jakarta.persistence.jdbc.password" value="Gyeong88!!"/>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/memo"/>

            <!--memo Entity를 읽고 table 만들어줌(DDL의 역할)-->
            <property name="hibernate.hbm2ddl.auto" value="create" />

            <!-- Hibernate에서 SQL 자동으로 만들어주는데 그 SQL이 어떻게 생성되고 요청되는지 직접 확인하기 위해 설정하는 것 -->
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

 

내부에서 만든 쿼리를 쉽게 확인하게 해주는 persistence.xml의 아래 세 개의 설정 실행 결과 

 

 

gradle config 열기 + jpa hibernate 설정 

오른쪽 gradle > 우클릭 > Gradle 구성 열기

 

plugins {
    id 'java'
}

group = 'com.sparta'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {

// JPA 구현체인 hibernate
    implementation 'org.hibernate:hibernate-core:6.1.7.Final'
// MySQL
    implementation 'mysql:mysql-connector-java:8.0.28'
// mysql의 버전이 8.1 이상인 경우
// implementation 'com.mysql:mysql-connector-j:8.2.0'



    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
}