새 프로젝트를 구축 할 때는 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" xsi : schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.xsd"> <modeversion> 4.0.0 </modelversion> <groupid> com.chhhliu.springboot.h2 </groupid> <Artifactid> springboot-h2 </artifactid> <bersion> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> springboot-h2 </name> <description> 스프링 부트 h2 </descrip> <apures> <groupid> org.springframewort.boot> <artifactid> spring-spring-sparatic <artifactid> <버전> 1.4.3. release </version> <aperativePath/> <!-저장소에서 부모를 조회합니다-> </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputeModing. <java.version> 1.7 </java.version> </properties> <pectionements> <pectionement> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-data-jpa </artifactid> </dependency> <groupd> </groupid> org.springwork.sboot <Artifactid> Spring-Boot-starter-web </artifactid> </fectionency> <pectionement> <groupid> com.h2database </groupid> <artifactid> <scope> 런타임 </scope> </spectency> <groupid> <groupid> org.springframework.boot> <Artifactid> Spring-Boot-Starter-Test </artifactid> </artifactid> </scope> </scope> </fectionency> </feencevency> <build> <flugins> <flugin> <groupId> org.springframework.boot </groupid> </flugin> </juctid> </artifactid> </project>
패키지 com.chhliu.springboot.h2. 엔티티; import java.math.bigdecimal; javax.persistence.column import; javax.persistence.entity import; import javax.persistence.generatedValue; import javax.persistence.generationType; javax.persistence.id import; @entity public class user {@id @generatedvalue (rategy = generationtype.auto) 개인 긴 ID; @Column 개인 문자열 사용자 이름; @Column 개인 문자열 이름; @Column 개인 단기간; @Column Private Bigdecimal Balance; ... GetTter 및 Setter 메소드 생략} 패키지 com.chhliu.springboot.h2.repository; import org.springframework.data.jpa.repository.jparepository; org.springframework.stereotype.repository import; import com.chhliu.springboot.h2.entity.user; @repository public interface userrepository 확장 jparepository <user, long> {} 패키지 com.chhliu.springboot.h2.controller; org.springframework.beans.factory.annotation.autowired; org.springframework.web.bind.annotation.getMapping; import org.springframework.web.bind.annotation.pathvariable; org.springframework.web.bind.annotation.RestController; import com.chhliu.springboot.h2.entity.user; import com.chhliu.springboot.h2.repository.userrepository; @RestController public class userController {@autowired private userrepository userrepository; @getMapping ( "/user/{id}") // getmapping annotation은 여기에서 사용되며,이 주석의 기능은 @requestmapping (value = "/user/{id}", method = requestmethod.get)과 유사합니다. @postmapping annotation은 공개 사용자 findbyid ( @pathvariable long)와 동일합니다. }}# 서버 포트 번호 Server.port = 7900 # ddl 문을 생성할지 spring.jpa.generate-ddl = false # sql 문을 인쇄할지 spring.jpa.show-sql = true # ddl을 자동으로 생성합니다. 특정 DDL이 지정되어 있으므로 spring.jpa.hibernate.ddl-auto = none # h2 데이터베이스 스프링을 사용합니다. datasource.platform = h2 # 데이터베이스 스프링을 생성하기위한 스키마 파일 위치를 지정합니다. spring.datasource.data = classpath : data.sql # 로그 인쇄 정보 로깅을 구성합니다. lelvel.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; org.springframework.beans.factory.annotation.autowired; org.springframework.boot.test.context.springboottest; org.springframework.test.context.junit4.springrunner import; import com.chhliu.springboot.h2.entity.user; import com.chhliu.springboot.h2.repository.userrepository; @runwith (springrunner.class) @springboottest public class springbooth2applicationtest {@autowired private userrepository Repository; @test public void test () {user u = repository.findone (1L); assert.assertequals ( "성공적인 테스트 케이스", "Zhang San", u.getName ()); }} 나는 시험이 괜찮다는 것을 알았다!
H2는 관계형 메모리 데이터베이스이므로 프로그램이 시작되면 테이블이 메모리로 작성되고 데이터는 메모리에 저장됩니다. 프로그램을 다시 시작하면 메모리의 데이터가 자동으로 삭제되며, 이는 DAO 계층에서의 단위 테스트 및 서비스 계층에서의 단위 테스트에 잘 사용할 수 있으므로 전체 프로그램이 특정 데이터베이스에 의존하지 않고 단위 테스트의 효율성을 향상시킬 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.