在IDEA中做练习或做demo时,有时候需要使用到数据库,但如果自己机子上本来没有安装数据库(全新安装太麻烦或资源有限),也没有可用的远程数据库时,我们可以直接在IDEA上使用便捷式的内存数据库H2,关于H2更多知识就自己去找一下资料了,接下来主要讲一下如何在IDEA下的Spring Boot项目中把它用起来!
首先在IDEA中新建一个空的Maven项目,编辑项目pom.xml文件,增加Spring Boot和最基础的数据访问相关jar包依赖:
<?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>org.example</groupId> <artifactId>microservice-simple-provider-user</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.4.2</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <!--SpringBoot的Web项目起步依赖--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.2</version> </dependency> <dependency> <!--SpringBoot的jpa数据访问组件起步依赖--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.3.1.RELEASE</version> </dependency> <dependency> <!--自动构建H2嵌入式或内存数据库的依赖--> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.2</version> </plugin> </plugins> </build> </project>
然后在classpath路径范围内增加建表SQL脚本文件和数据初始化文件:
接着再添加Spring Boot项目配置文件application.yml,并添加jpa、datasource和h2的主要配置项:
最后再为项目添加一个Spring Boot启动类,即可将应用跑起来,并进行H2内存数据库的连接了:
注意:自动创建的H2内存数据库的管理地址就是我们在application.yml中配置的h2.console.path地址,进入管理页后如下图所示:
这样我们的应用就相当于自带了一个便捷式内存数据库了!
现在我们来为项目做一个最简单的H2内存数据库的使用样例——为H2数据库中的User表增加对应的实体类User.java、基于JPA的数据访问对象UserRepository.java和API层控制类UserController.java:
User.java类:
package com.example.models; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; import java.math.BigDecimal; @JsonIgnoreProperties(value = {"hibernateLazyInitializer"}) @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; @Column private BigDecimal balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } }
UserRepository.java类:
package com.example.dao; import com.example.models.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { }
UserController.java类:
package com.example.controller; import com.example.dao.UserRepository; import com.example.models.User; 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; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/{id}") public User findById(@PathVariable Long id) { User oneUser = this.userRepository.getOne(id); return oneUser; } }
最后的项目结构如图所示:
来看一下效果: