JPAとは何ですか
JPA(Java Persipence API)は、Sunによって正式に提案されたJava Persipence仕様です。 Java開発者に、Javaアプリケーションでリレーショナルデータを管理するオブジェクト/アソシエーションマッピングツールを提供します。彼の外観は、主に既存の持続的な開発努力を簡素化し、ORMテクノロジーを統合することです
Spring Data JPAは、ORMフレームワークとJPA仕様に基づいてSpringでカプセル化されたJPAアプリケーションフレームワークであり、開発者はミニマリストコードを使用してデータにアクセスおよび操作できるようにします。追加、削除、変更、検索などの一般的な機能を提供し、拡張しやすいです! Spring Data JPAの学習と使用は、開発効率を大幅に向上させることができます!
jdbctemplateを使用して、Spring Bootでデータベースにアクセスします
データソース構成
まず、データベースに接続するには、JDBCサポートを導入する必要があり、次の構成がpom.xmlで導入されています。
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-jdbc </artifactid> </dependency>
埋め込みデータベースサポート
埋め込まれたデータベースは、開発環境とテスト環境で一般的に使用されます。 Spring-Bootは、H2、HSQL、Derbyを含む自動構成埋め込みデータベースを提供します。これを使用するために接続構成を提供する必要はありません。
H2の依存関係など
<Dependency> <GroupId> com.H2DataBase </groupId> <Artifactid> H2 </artifactid> <scope> runtime </scope> </dependency>
MySQLデータベースサポート
<Dependency> <GroupId> mysql </groupId> <artifactid> mysql-connector-java </artifactid> <version> 5.1.38 </version> </dependency>
構成情報を編集します
SRC/Main/Resources/Application.Propertiesでデータソース情報を構成します
spring.datasource.url = jdbc:mysql:// localhost:3306/testspring.datasource.username = rootspring.datasource.passpring.datasource.datasource.datasource.datasource.datasource.datasource.datasource.datasource
jdbctemplateを使用してデータベースを操作します
SpringのJDBCTEMPLATEが自動的に構成されているため、@Autowiredを使用して独自のBeanに注入できます。
jdbctemplateを介してDemoserviceで定義されたデータアクセス操作の実装
@servicepublic class demoserivce {@autowired private jdbctemplate jdbctemplate; public void create(string name、integer age){jdbctemplate.update( "inserting into demo(name、age)values(?、?)"、name、age); } public void deletebyname(string name){jdbctemplate.update( "delete from demowhere name =?"、name); } public Integer getalldemo(){return jdbctemplate.queryforobject( "select count(1)from demo"、integer.class); } public void deletealldemo(){jdbctemplate.update( "delete from demo"); }}ユーザーサービスのユニットテストケースを作成して、作成、削除、およびクエリを作成して、データベース操作の正しさを確認します。
テストケースは依存関係を増やす必要があります
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope> test </scope> </dependency>
テストコード
@runwith(springjunit4classrunner.class)@springApplicationConfiguration(main.class)public class applicationtests {@autowired private demoserivce demoserivce; @before public void setup(){//準備し、テーブルをクリアしますdemoserivce.deletealldemo(); } @test public void test()スロー例外{// 5 demoserivce.create( "a"、1); demoserivce.create( "b"、2); demoserivce.create( "c"、3); demoserivce.create( "d"、4); demoserivce.create( "e"、5); assert.assertequals(5、demoserivce.getalldemo()。intvalue()); demoserivce.deletebyname( "a"); demoserivce.deletebyname( "e"); //データベースを見ると、5 assert.assertequals(3、demoserivce.getalldemo()。intvalue()); }}Spring-Data-JPAをSpring Bootで使用します
これらの大量の退屈なデータ操作ステートメントを解決するために、私たちが考える最初のことは、次のようなORMフレームワークを使用することです。 Hibernateを統合した後、最終的にJavaエンティティを操作する方法でデータの変更をデータベーステーブルにマッピングします。
基本的な「追加、削除、修正、および検索」を解決するために、各Javaエンティティを抽象化する操作は、通常、テンプレートを一般的な方法で抽象化して簡素化するための一般的な方法でカプセル化しますが、これはまだあまり便利ではありません。特定のエンティティの一般的なテンプレートから継承されたインターフェイスを作成し、インターフェイスの実装を作成する必要があります。いくつかの基本的なデータアクセスは十分に再利用できますが、コード構造内の各エンティティのインターフェイスと実装がたくさんあります。
テンプレートの実装により、これらの特定のエンティティのレイヤーは非常に「薄く」になりました。一部の特定のエンティティの実装は、テンプレートの単純なプロキシである可能性があり、多くの場合、そのような実装クラスが多くのエンティティに表示される場合があります。 Spring-Data-JPAの出現により、このような「薄い」データアクセスレイヤーがインターフェイスの層を書き込む方法になります。
使い方
依存関係を追加します
<依存関係<GroupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-data-jpa </artifactid> </dependency>
構成情報を編集します
SRC/Main/Resources/Application.Propertiesでデータソース情報を構成します
spring.datasource.url = jdbc:mysql:// localhost:3306/testspring.datasource.username = rootspring.datasource.password = rootspring.datasource.driver-class-name = com.mysql.jdbc.driverspring.jpa.properties.hibernate.hbm2ddl.auto = update
spring.jpa.properties.hibernate.hbm2ddl.autoは、hibernateの構成プロパティです。その主な機能は、データベーステーブル構造を自動的に作成、更新、および検証することです。このパラメーターのいくつかの構成は次のとおりです
エンティティを作成します
@entitypublic class demoentity {@id @generatedValue private long id;プライベートストリングタイトル。プライベート文字列コンテンツ。 public demoentity(){} public demoentity(string title、string content){this.title = title; this.content = content; } //セットを省略します}DAOを作成します
Public Interface DemorepositoryはjparePository <demoentity、long> {demoentity findbytitle(string title); demoentity findbytitleandcontent(string title、string content); // @query( "u.content =:content"からdemoentity uからuse u from content ")@query(" u.content =:content ")demoentity sqlfind(@param(" content ")string content);};}SQLでテーブル名を書き込むのではなく、エンティティ名は自動的にテーブル名に変換されます。
メソッド名を解析してクエリを作成します
上記のfindbytitle(文字列タイトル)とfindbytitleandcontent(文字列タイトル、文字列コンテンツ)は書き込まれていませんが、フレームワークは上記のペアの名前に従ってSQLを自動的に作成します。
単体テスト
@runwith(springjunit4classrunner.class)@springApplicationConfiguration(main.class)public class unittest {@autowired demorepository demorepository; @test public void test(){for(int i = 0; i <10; i ++){demorepository.save(new demoentity( "title"+i、 "content"+i)); } assert.assertequals(10、demorepository.findall()。size()); } @test public void testfindbytitle(){demoentity res = demorepository.findbytitle( "title8"); assert.assertequals( "title8"、res.getTitle()); } @test public void testfindbytitleandcontent(){demoentity res = demorepository.findbytitleandcontent( "title9"、 "content9"); assert.assertequals( "title9"、res.getTitle()); assert.assertequals( "content9"、res.getContent()); } @test public void testsqlfind(){demoentity res = demorepository.sqlfind( "content7"); assert.assertequals( "content7"、res.getContent()); }}要約します
上記は、Spring BootでSpring-Data-JPAを使用するための編集者の紹介で、データベースを迅速にアクセスします。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!