您好,登錄后才能下訂單哦!
新建工程的時候,需要加入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.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); } }
# 服務器端口號 server.port=7900 # 是否生成ddl語句 spring.jpa.generate-ddl=false # 是否打印sql語句 spring.jpa.show-sql=true # 自動生成ddl,由于指定了具體的ddl,此處設置為none spring.jpa.hibernate.ddl-auto=none # 使用H2數據庫 spring.datasource.platform=h3 # 指定生成數據庫的schema文件位置 spring.datasource.schema=classpath:schema.sql # 指定插入數據庫語句的腳本位置 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
可以看到測試結果
{"id":4,"username":"user4","name":"馬六","age":20,"balance":100.00}
說明,我們的整合是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("成功的測試用例", "張三", u.getName()); } }
發現測試是ok的!
由于H2是關系內存數據庫,當程序啟動的時候,會在內存中創建表,并將數據存儲在內存中,當重啟程序后,會自動刪除內存中的數據,從而可以很好的用來做dao層的單元測試和service層的單元測試,使整個程序不會依賴具體的數據庫,同時也提高了單元測試的效率。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。