新しいプロジェクトを構築するときは、JPAとH2の依存関係を追加する必要があります
POMファイルの依存関係は次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modeleversion> 4.0.0 </modelversion> <グループ<artifactid> springboot-h2 </artifactid> <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> springboot-h2 </name> <説明> spring boot h2 </description> <parent> org.springframework.boot </groupid> groupid> <artifactid> spring-boot-starter-parent </artifactid> <version> 1.4.3.release </version> <relativepath/> <! - lookup parent from repository-> </parent> <properties> <build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.7 </java.version> </properties> <dependencies> <shiplencys> <shiplyid> org.springframework.boot </groupid </dependency> <dependency> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <redency> <groupid> com.h2database </groupid> <artifactid> h2 </sucepactid> <scope> scope> scope> scope> scope> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope> test </scope> </dependency> </dependency> </dependency> <bultins> <プラグイン> <プラグイン><groupid> org.springframework.boot </groupid>>グループ<artifactid> spring-boot-maven-plugin </artifactid> </plugin> </plugins> </build> </project>
パッケージcom.chhliu.springboot.h2.entity; java.math.bigdecimalをインポートします。 javax.persistence.columnをインポートします。 javax.persistence.entityをインポートします。 javax.persistence.generatedValueをインポートします。 javax.persistence.generationTypeをインポートします。 javax.persistence.idをインポートします。 @Entity Public Classユーザー{@ID @GeneratedValue(Strategy = GenerationType.auto)private long id; @columnプライベート文字列ユーザー名; @columnプライベート文字列名; @columnプライベートショートエイジ。 @columnプライベートビッグデキマルバランス。 ... gettterとsetterメソッドを省略}パッケージcom.chhliu.springboot.h2.Repository; Import org.springframework.data.jpa.repository.jparePository; Import org.springframework.stereotype.repository; com.chhliu.springboot.h2.entity.userをインポートします。 @Repository Public Interface UserrePository拡張jparePository <user、long> {}パッケージcom.chhliu.springboot.h2.controller; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.web.bind.annotation.getMapping; org.springframework.web.bind.annotation.pathvariableをインポートします。 org.springframework.web.bind.annotation.restControllerをインポートします。 com.chhliu.springboot.h2.entity.userをインポートします。 com.chhliu.springboot.h2.repository.userrepositoryをインポートします。 @RestController public class usercontroller {@autowired private userrepository userrepository; @getMapping( "/user/{id}")// GetMapping Annotationはここで使用されており、このアノテーションの機能は@RequestMapping(value = "/user/{id}"、methodmethod.get)に似ています。 }}#サーバーポート番号サーバー= 7900#ddlステートメントを生成するかどうかspring.jpa.generate-ddl = false#sqlステートメントを印刷するかどうかspring.jpa.show-sql = true#ddlを自動的に生成します。特定のDDLが指定されているため、spring.jpa.hibernate.ddl-auto = noneに設定されています#h2データベースspring.datasource.platform = h2#データベースを生成するためのスキーマファイルの場所を指定しますspring.datasource.schema = classpathのスクリプトの特定の場所を特定するためのSchemaファイルの場所を指定します。 spring.datasource.data = classpath:data.sql#configure log printing information logging.level.root = info logging.level.org.hibernate = info logging.level.org.hibernate.type.descriptor.sql.basicbinder = trace logging.level.org.hibernate.type.descriptor.sql.basicextractor = trace logging.level.com.itmuch = debug
ブラウザに次のURLを入力します:http:// localhost:7900/user/4
テスト結果を確認できます
{"id":4、 "username": "user4"、 "name": "maliu"、 "age":20、 "balance":100.00}
統合が問題ないことを説明してください
パッケージcom.chhliu.springboot.h2; Import org.junit.assert; Import org.junit.test; Import org.junit.runner.runwith; Import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; Import org.springframework.test.context.junit4.springrunner; com.chhliu.springboot.h2.entity.userをインポートします。 com.chhliu.springboot.h2.repository.userrepositoryをインポートします。 @runwith(springrunner.class)@springboottestパブリッククラスSpringbooth2ApplicationTest {@autowired private userrepositoryリポジトリ; @test public void test(){user u = repository.findone(1L); assert.assertequals( "成功したテストケース"、 "Zhang San"、u.getName()); }}テストは大丈夫だとわかりました!
H2はリレーショナルメモリデータベースであるため、プログラムが開始されると、テーブルがメモリに作成され、データはメモリに保存されます。プログラムを再起動した後、メモリ内のデータは自動的に削除されます。これは、DAOレイヤーでのユニットテストとサービスレイヤーでの単位テストに適切に使用でき、プログラム全体が特定のデータベースに依存せず、単位テストの効率を向上させます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。