When building a new project, you need to add JPA and H2 dependencies
The pom file dependencies are as follows:
<?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> </dependency> </dependency> <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; ...Omit gettter and setter methods} package 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> { } package 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}")// Note that the GetMapping annotation is used here, and the function of this annotation is similar to @RequestMapping(value="/user/{id}" ,method=RequestMethod.GET), @PostMapping annotation is the same as public User findById(@PathVariable Long id) { return this.userRepository.findOne(id); } }# Server port number server.port=7900 # Whether to generate the ddl statement spring.jpa.generate-ddl=false # Whether to print the sql statement spring.jpa.show-sql=true # Automatically generate ddl. Since specific ddl is specified, it is set to none spring.jpa.hibernate.ddl-auto=none # Use the H2 database spring.datasource.platform=h2 # Specify the schema file location for generating the database spring.datasource.schema=classpath:schema.sql # Specify the script location for inserting the database statement 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
Enter the following URL in your browser: http://localhost:7900/user/4
You can see the test results
{"id":4,"username":"user4","name":"Maliu","age":20,"balance":100.00}
Explain that our integration is OK
package 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; 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("Successful test case", "Zhang San", u.getName()); } } I found the test was OK!
Since H2 is a relational memory database, when the program is started, tables will be created in memory and data will be stored in memory. After restarting the program, the data in memory will be automatically deleted, which can be used well for unit testing at the dao layer and unit testing at the service layer, so that the entire program will not rely on specific databases, and also improve the efficiency of unit testing.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.