Springboot integrates H2 memory databases for unit testing and database independence.
1. Create a spring boot Project
When creating a project, you need to add JPA and H2 dependencies.
2. Engineering Structure
The pom file dependency is 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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. Compile entity classes
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 n Ame; @ Column private Short age; @ Column private BigDecimal balance ;...... The gettter and setter methods are omitted}
4. Write dao
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> { }
5. Write controller
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, this annotation works similarly 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 );}}
Vi. Configuration File
# Server port number. port = 7900 # Whether to generate the ddl statement spring. jpa. generate-ddl = false # Whether to print the SQL statement spring. jpa. show-SQL = true # generate ddl automatically. Because the specific ddl is specified, set this parameter to none spring. jpa. hibernate. ddl-auto = none # Use H2 database spring. datasource. platform = h2 # specify the location of the schema file for database generation spring. datasource. schema = classpath: schema. SQL # specify the script location for inserting database statements 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
7. Start the program
Enter the following URL in the browser: http: // localhost: 7900/user/4
The test result is displayed.
{"Id": 4, "username": "user4", "name": "Ma ", "age": 20, "balance": 100.00}
It indicates that our integration is OK.
8. Test dao Layer
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 SpringbootH2ApplicationTests {@ Autowired private UserRepository repository; @ Test public void test () {User u = repository. findOne (1L); Assert. assertEquals ("successful test cases", "Zhang San", u. getName ());}}
Test is OK!
IX. Summary
Since H2 is a relational memory database, when the program starts, it creates a table in the memory and stores the data in the memory. After the program is restarted, the data in the memory is automatically deleted, therefore, it can be well used for dao layer unit testing and service layer unit testing, so that the entire program does not depend on a specific database, but also improves the efficiency of unit testing.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.