はじめに:
時間の長い期間のために、アプリケーションデータを達成するために、アクセス層は、多くの問題となっています。単純なクエリを実行し、ページングおよび監査を行うためにあまりにも多くの定型的なコードを記述する必要があります。春データJPAは大幅に実際に必要な作業量を減らすことにより、データアクセス層を改善することを目指しています。開発者は、カスタム検索メソッドを含むリポジトリのインターフェースを、書き、春には、自動的に実装を提供します。
春データJPAは大幅に実際に必要な作業量を減らすことにより、データアクセス層を改善することを目指しています。開発者は、カスタム検索メソッドを含むリポジトリのインターフェースを、書き、春には、自動的に実装を提供します。
コードが書かれました:
1.設定依存:
<?xml version = "1.0"エンコード= "UTF-8"?> <プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0" のxmlnsを:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス」 のxsi:のschemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion> 4.0.0 </ modelVersion> <groupIdを> com.xhn </ groupIdを> <たartifactId> springbootdemo </たartifactId> <バージョン> 1.0-SNAPSHOT </バージョン> <親> <groupIdを> org.springframework.boot < /のgroupId> <たartifactId>春・ブート・スターター・親</たartifactId> <バージョン> 2.0.2。RELEASE </バージョン> </親> <! - Spring MVCの依存関係を追加します- > <依存性> <依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータデータJPA </たartifactId> </依存> <依存性> <groupIdを>のMySQL </のgroupId> <たartifactId>のMySQL -connector-javaの</たartifactId> </依存関係> <! - FreeMarkerの模板引擎- > <依存> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブート・スターター-FreeMarkerの</たartifactId > </依存関係> <! -整合springboot与MyBatisの的整合- > <依存> <groupIdを> org.mybatis.spring.boot </ groupIdを> <たartifactId> MyBatisのスプリング・ブート・スターター</たartifactId> <バージョン> 1.1.1 </バージョン> </依存関係> <! -测试のJUnit - > <依存> <groupIdを> org.springframework.boot </ groupId> <たartifactId>春・ブート・スターターテスト</たartifactId> <スコープ>テスト</スコープ> </依存関係> <! - Redisの缓存- > <依存> <groupIdを> org.springframework.boot </ groupId> <たartifactId>ばねブートスタータデータRedisの</たartifactId> </依存> <依存性> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>ばねブートスタータデータelasticsearch </たartifactId> </依存関係> <依存> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>スターター・春ブーツ-Webが</たartifactId> </依存関係> </依存関係> <! -マッパー下に置かれたマッパーインターフェイスマッパーマッピングファイル必要なパケットマッパーの下に依存- > <構築する> <リソース> <リソース> <ディレクトリ>のsrc /メイン/ Javaの</ディレクトリ> <>付属 <include>の** / * .propertiesファイル</インクルードには> <含めるを> * * / * .xmlの</ include>の </リソース> <リソース>XML </ include>の </は、> <フィルタ> 偽 </フィルタリング> <ディレクトリ>のsrc /メイン/リソース</ディレクトリ>を <含んで> <include>の** / * 。* </ include>の </含ん> <フィルタリング>偽</フィルタリング> </リソース> </リソース> <プラグイン> <プラグイン> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブートのmaven-pluginの</たartifactId> </プラグイン> </プラグイン> </構築> </プロジェクト>
設定application.propertiesを2.resources:
#DB Configation spring.datasource.driver - クラス -name = はcom.mysql.jdbc.Driver spring.datasource.url = JDBCます。mysql:// localhostを:3306 /テスト spring.datasource.username = ルート spring.datasource.password = ルート #JPA Configation spring.jpa.database = MySQLの spring.jpa.generate -ddl = 真 spring.jpa.show -sql = 真
接続はデータベース上ではなく、この変更の設定その場合
#DB構成ガイドの spring.datasource.driver - クラス -name = com.mysql.cj.jdbc.Driver spring.datasource.url = JDBCます。mysql:// localhostを:3306 /你自己的数据库useUnicode =真&characterEncoding = UTF-8&useSslオプション? =偽&serverTimezone = GMT spring.datasource.username = ルート spring.datasource.password = ルート #JAPConfigration spring.jpa.database = MySQLの spring.jpa.show -sql = 真 spring.jpa.generate -ddl = 真
3.テストコントローラを設定します。
パッケージcom.xhn.controller。 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RestController。 輸入java.util.HashMapを; 輸入java.util.Map; @RestController @RequestMapping( "/テスト" ) パブリック クラスTestController { @RequestMapping( "/ハロー" ) パブリックマップsayHell0(){ 地図の地図 = 新規HashMapの()。 map.put( "JSON数据01"、 "我爱のjava" ); リターンマップ。 } }
レンダリング:
話題に:
1.データベースを作成し、データを追加します。
2.対応するPOJOオブジェクトを作成します。
パッケージcom.xhn.entity、 インポートのjavax.persistence。* ; // このようなエンティティクラスとしてタグ @Entity // に対応するタグ・データベース・テーブル @Table(名=「ユーザー」) パブリック クラスユーザー{ // セットIDをインクリメントの主キー @Id @GeneratedValue(戦略 = GenerationType.IDENTITYを) プライベート整数ID; プライベート文字列名; プライベート文字列のパスワード; プライベート文字列名; 公共の整数のgetId(){ 返すIDを; } パブリック 無効SETID(整数ID){ この .ID = ID。 } パブリック文字列getUsername(){ 戻りユーザ名; } 公共 ボイドsetUsername(文字列名){ この .username = ユーザ名; } パブリック文字列getPasswordに(){ 戻りパスワード。 } 公共 ボイドするsetPassword(文字列のパスワード){ このみましょう。パスワード= パスワード。 } パブリック文字列のgetName(){ 戻り名。 } 公共 のボイドsetName(文字列名){ この .nameの= 名前。 } @Override パブリック文字列のtoString(){ 戻り "ユーザー{" + "ID =" + ID + "ユーザ名=" '+名+ '\'' + "パスワード= '" +パスワード+ '\'' + "名前= '" +名+ '\'' + '}' ; } パブリックユーザ(整数ID、文字列名、文字列のパスワード、文字列名){ この .ID = ID。 この .username = ユーザ名; この .nameの= 名前; } パブリックユーザ(){ スーパー()。 } }
注:対応する必要が非常に優れた構成に関する注意
3.対応するコントローラ・インタフェースを作成します。
パッケージcom.xhn.controller。 輸入com.xhn.dao.UserDao。 輸入com.xhn.entity.User。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.web.bind.annotation.GetMapping。 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RestController。 輸入はjava.util.List; @RestController @RequestMapping( "/ユーザ" ) パブリック クラスUserControllerで{ @Autowired プライベートUserDao userDao。 // 查询出所有数据 @RequestMapping( "/リスト" ) 公共の一覧<ユーザー> getUserList(){ 返す(userDao.findAllを)。 } }
4. DAO対応する層。
パッケージcom.xhn.dao。 輸入com.xhn.entity.User。 輸入org.springframework.data.jpa.repository.JpaRepository; パブリック インターフェース UserDao 延び JpaRepositoryを<ユーザー、整数> { }
コードをテストします。
メインボーに対応するJSONウィジェット:JSON-handle_0.5.6.crxは、ウェブが自己のBaiduすることができ