SpringBoot統合SpringDataJpaの手順:
1、依存データJPA(のpom.xml)を添加開始
図2に示すように、データベースドライバ座標を追加し、JUnitの依存性(のpom.xml)を添加開始
図3に示すように、データベース接続情報(application.properties)を追加します
4、ユーザテーブルを作成し、作成されたテーブルを省略することができ
5. Beanの対応するユーザエンティティ(任意のコメントなしではなくMyBatisの、注釈を追加する必要があります)を作成します。
6、UserReporitoryインターフェイスを作成するには、次のものが必要JpaRepository <ユーザー、整数>(MyBatisのようなマッピングファイルを対応するインタフェースと同じクエリを追加する必要はありません)を拡張します
7、application.propertiesに対応関係SpringDataJPAに加え
8、書き込みテストクラス
9、テストを開始
詳細は以下のとおりです。
pom.xml内容:
<! - pringDataJPA - >
<依存>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>スターター・春ブーツ-DATA-JPA </たartifactId>
</依存関係>
<! -データベースドライバ- - >
<依存>
<groupIdを> MySQLの</ groupIdを>
<たartifactId> MySQLの-コネクタは、Javaの</たartifactId>
<! - > -父は判決を与えているので、バージョン番号を記入する必要はありませんに依存しています
。<! - - <バージョン> 5.1.39 </バージョン> - >
</依存>
<のgroupId> org.springframework.boot </のgroupId>
<たartifactId>スタータースプリングブートテスト</たartifactId>
<スコープ>テスト</スコープ>
</依存関係>
application.properties内容:
#データベース接続情報
spring.datasource.url = JDBC:MySQLの:// localhostの:? 3306 / =真useUnicodeにテスト&characterEncoding = UTF8&serverTimezone = UTC
spring.datasource.username =ルート
spring.datasource.password=root@123の
spring.datasource.driver名前=はcom.mysql.jdbc.Driver-級
#springboot統合springDataJPA情報
spring.jpa.hibernate.ddl =自動更新
#印刷コンソールのSQL
にspring.jpa.show真-SQL =
ユーザエンティティの内容:
パッケージcom.myself.domain。
輸入javax.persistence.Entity。
輸入javax.persistence.GeneratedValue;
輸入javax.persistence.GenerationType;
輸入javax.persistence.Id;
@Entity
パブリッククラスユーザー{
@Id
@GeneratedValue(戦略= GenerationType.IDENTITYを)
プライベートint型のID;
プライベート文字列のユーザ名。
プライベート文字列のパスワード。
プライベート文字列名;
公共INTのgetId(){
戻りID。
}
公共ボイドSETID(int型のID){
this.id = ID。
}
パブリック文字列getUsername(){
戻りユーザ名;
}
公共ボイドsetUsername(文字列名){
this.username =ユーザ名;
}
パブリック文字列getPasswordに(){
戻りパスワード。
}
公共ボイドするsetPassword(文字列のパスワード){
this.password =パスワード。
}
パブリック文字列のgetName(){
戻り名。
}
公共ボイドのsetName(文字列名){
this.name =名。
}
@Override
パブリック文字列のtoString(){
戻り"ユーザー{" +
"ID =" + ID +
"ユーザ名=" '+名+ '\'' +
"パスワード= '" +パスワード+ '\''
"名前= '" +名+ '\'' +
'}';
}
}
UserRepository.javaインターフェイス要素:
パッケージcom.myself.repository。
輸入com.myself.domain.User;
輸入org.springframework.data.jpa.repository.JpaRepository。
輸入はjava.util.List;
パブリックインターフェイスUserRepositoryがJpaRepository <ユーザー、整数> {延び
公衆リスト<ユーザー>のfindAll();
}
テストクラスのSpringDataJPATest.jva内容:
com.myselfをパッケージ化。
輸入com.myself.domain.User;
輸入com.myself.repository.UserRepository。
輸入org.junit.Test;
輸入org.junit.runner.RunWith;
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.boot.test.context.SpringBootTest;
輸入org.springframework.test.context.junit4.SpringRunner;
輸入はjava.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(クラス= SpringbootJpaApplication.class)
パブリッククラスSpringDataJPATest {
@Autowired
プライベートUserRepository userRepository。
@Test
公共ボイドqueryUserTest(){
リスト<ユーザー>ユーザー= userRepository.findAll()。
System.out.println(ユーザ);
}
}
次のようにプロジェクトの構造は以下の通りであります:
ご希望の場所で展覧会を理解します。