新建工程的時(shí)候,需要加入JPA,H2依賴
在高淳等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷推廣,外貿(mào)網(wǎng)站建設(shè),高淳網(wǎng)站建設(shè)費(fèi)用合理。
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.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chhliu.springboot.h3</groupId> <artifactId>springboot-h3</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-h3</name> <description>Demo project for Spring Boot H2</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h3database</groupId> <artifactId>h3</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.chhliu.springboot.h3.entity; import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Short age; @Column private BigDecimal balance; ……省略gettter和setter方法 }
package com.chhliu.springboot.h3.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.chhliu.springboot.h3.entity.User; @Repository public interface UserRepository extends JpaRepository<User, Long> { }
package com.chhliu.springboot.h3.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.chhliu.springboot.h3.entity.User; import com.chhliu.springboot.h3.repository.UserRepository; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/user/{id}")// 注意,此處使用的是GetMapping注解,該注解的作用類似與@RequestMapping(value="/user/{id}" ,method=RequestMethod.GET),@PostMapping注解同理 public User findById(@PathVariable Long id) { return this.userRepository.findOne(id); } }
# 服務(wù)器端口號(hào) server.port=7900 # 是否生成ddl語(yǔ)句 spring.jpa.generate-ddl=false # 是否打印sql語(yǔ)句 spring.jpa.show-sql=true # 自動(dòng)生成ddl,由于指定了具體的ddl,此處設(shè)置為none spring.jpa.hibernate.ddl-auto=none # 使用H2數(shù)據(jù)庫(kù) spring.datasource.platform=h3 # 指定生成數(shù)據(jù)庫(kù)的schema文件位置 spring.datasource.schema=classpath:schema.sql # 指定插入數(shù)據(jù)庫(kù)語(yǔ)句的腳本位置 spring.datasource.data=classpath:data.sql # 配置日志打印信息 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
可以看到測(cè)試結(jié)果
{"id":4,"username":"user4","name":"馬六","age":20,"balance":100.00}
說(shuō)明,我們的整合是OK的
package com.chhliu.springboot.h3; 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; import com.chhliu.springboot.h3.entity.User; import com.chhliu.springboot.h3.repository.UserRepository; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootH2ApplicationTests { @Autowired private UserRepository repository; @Test public void test(){ User u = repository.findOne(1L); Assert.assertEquals("成功的測(cè)試用例", "張三", u.getName()); } }
發(fā)現(xiàn)測(cè)試是ok的!
由于H2是關(guān)系內(nèi)存數(shù)據(jù)庫(kù),當(dāng)程序啟動(dòng)的時(shí)候,會(huì)在內(nèi)存中創(chuàng)建表,并將數(shù)據(jù)存儲(chǔ)在內(nèi)存中,當(dāng)重啟程序后,會(huì)自動(dòng)刪除內(nèi)存中的數(shù)據(jù),從而可以很好的用來(lái)做dao層的單元測(cè)試和service層的單元測(cè)試,使整個(gè)程序不會(huì)依賴具體的數(shù)據(jù)庫(kù),同時(shí)也提高了單元測(cè)試的效率。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享文章:springboot整合H2內(nèi)存數(shù)據(jù)庫(kù)實(shí)現(xiàn)單元測(cè)試與數(shù)據(jù)庫(kù)無(wú)關(guān)性
鏈接分享:http://muchs.cn/article22/ghgdcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、做網(wǎng)站、搜索引擎優(yōu)化、動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)