[Spring]JPA優先パラメータ


1)プロジェクトの作成



Vscodeを実行し、F 1-create java project-Mavenをクリックします.
maven-archetype-quickstartクリック-version1.4
パッケージ名:dev.hibernate
プロジェクト名:jpa Select Destination Folderクリック
enter Openクリックすると新しいVscodeウィンドウが実行されます!

App.JavaでF5をクリックし、端末でHello Worldを出力して終了

2) pom.xml設定



pom.xmlの<properties>:1.7から1.8に変更pom.xml<dependencies>に追加
<!-- H2 DB --> 
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
</dependency>

<!-- JPA 구현체 Hibernate -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.4.10.Final</version>
</dependency>

インストールがうまくいかなかったら.
端末mvn -U clean installに入力

pom.右クリックxmlUpdate projectクリック

👍 タグを表示して終了!

3)H 2データベースのインストール


https://www.h2database.com/html/main.html


[すべてダウンロード]をクリックします.

上部Version 2.1.210Platform-independent Zipをクリックします.

必要なファイルに解凍:h2 폴더作成

h 2フォルダにファイルが作成されました!
binフォルダ—h2w実行

ファイアウォールがOKであれば、Web上にH 2コンソールが表示されます연결クリック

このページを開くと完成です!
C:\Users\wlswnパス生成test.mvというDB

4) persistence.xmlの生成




main-フォルダ名:resources作成

resources-フォルダ名:META-INF作成

META-INF-ファイル名: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>
コードをpersistence.xmlに貼り付けます.

戻るを使用して接続を解除するには

JDBC URL:jdbc:h2:tcp://localhost/~/testに変更し、연결をクリック
Connectクリック

5) 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="jpa"> <!-- 프로젝트 이름 -->
      <properties>
        <!-- property name -->
        <!-- javax.persistence 로 시작하는 속성들 : JPA 표준 속성 -->
        <!-- hibernate ~ : JPA의 구현체 중 하나인 hibernate의 전용 속성-->

        <!-- DB와 연결하기 위한 필수 속성 -->
        <property name ="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
        <property name ="javax.persistence.jdbc.user" value="sa"/>
        <property name ="javax.persistence.jdbc.password" value=""/>
        <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>

        <!-- DB Dialect(방언) 설정 -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />


        <!-- 옵션(선택) 속성 -->
        <!-- hibernate의 실행 과정에서 console에 테이블 생성, DDL, DML 등 SQL문을 출력하도록 하는 설정 -->
        <property name ="hibernate.show_sql" value ="true"/>

        <!-- SQL 출력 상태 정렬 -->
        <property name ="hibernate.format_sql" value ="true"/>

        <!-- 주석문도 포함해서 출력하기 -->
        <property name ="hibernate.use_sql_comments" value ="true"/>

        <!-- ***(중요!!)*** 애플리케이션 실행 시 DB테이블을 자동으로 실행할 것인지?? -->
        <!-- create : 기존에 테이블이 존재하면 삭제(drop) 후 새로 생성(create), DROP + CREATE  -->
        <!-- update : DB테이블과 Entity(객체)의 매핑(Mapping) 정보들 비교, 변경된 사항만 수정, 반영 -->
        <property name ="hibernate.hbm2ddl.auto" value ="create"/>

      </properties>
    </persistence-unit>
    
</persistence>