springboot整合H2内存数据库实现单元测试与数据库无关性
发布时间:2020-12-14 06:07:02 所属栏目:Java 来源:网络整理
导读:一、新建spring boot工程 新建工程的时候,需要加入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:schem
一、新建spring boot工程新建工程的时候,需要加入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.h2</groupId> <artifactId>springboot-h2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-h2</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.h2database</groupId> <artifactId>h2</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.h2.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方法 } 四、编写daopackage com.chhliu.springboot.h2.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.chhliu.springboot.h2.entity.User; @Repository public interface UserRepository extends JpaRepository<User,Long> { } 五、编写controllerpackage com.chhliu.springboot.h2.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.h2.entity.User; import com.chhliu.springboot.h2.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=h2 # 指定生成数据库的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 七、启动程序
|