【SpringBoot】整合JPA、H2DB
JPA作为ORM框架一直是我非常喜欢的框架之一。
H2DB一直都是我作用测试用的一款内存数据库。其实也是可以存储在本地文件中的。
好了,废话不多说,咱们开始整合起来吧~
遵循SpringBoot整合策略的三板斧
第一步:添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
|
第二部:添加配置
如果想将H2DB数据持久化,那么spring.datasource.url=jdbc:h2:file:${文件路径}
这样配置。
这样下次启动后,原先保存操作的数据,都还在。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| server: port: 8001 spring: application: name: H2DB datasource: driver-class-name: org.h2.Driver url: jdbc:h2:mem:jpa
username: shuma password: shuma123 h2: console: enabled: true settings: trace: true web-allow-others: true path: /h2db jpa: database: H2 hibernate: ddl-auto: update naming: physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl show-sql: true open-in-view: true
|
第三步:写代码加注解
创建数据库映射的实体类
1 2 3 4 5 6 7 8 9 10 11 12
| @Data @Entity @Table(name = "account") public class Account { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
private String name; private String username; private String password; }
|
创建实体类的JpaRepository接口
1 2 3
| public interface AccountJPA extends JpaRepository<Account, Integer> { }
|
接下来编写Controller API接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
@RestController @RequestMapping("/account") public class AccountController { @Autowired private AccountJPA accountJPA;
@PostMapping("/") public void save(@RequestBody Account account) { accountJPA.save(account); }
@GetMapping("/") public List<Account> getAll() { return accountJPA.findAll(); } }
|
好了,启动项目工程,接下来我们来测试一下吧;可以通过Postman工具进行测试,还有Postwoman,这里我是通过IDEA的http测试工具:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| ### 保存账号信息 POST http://localhost:8001/account/ Content-Type: application/json
{ "name":"李四", "username":"bbb", "password":"bbb" }
### 返回接口 POST http://localhost:8001/account/
HTTP/1.1 200 Content-Length: 0 Date: Wed, 19 Aug 2020 09:34:47 GMT Keep-Alive: timeout=60 Connection: keep-alive
<Response body is empty>
Response code: 200; Time: 39ms; Content length: 0 bytes
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| ### 返回所有账号信息 GET http://localhost:8001/account/
### 返回结果 GET http://localhost:8001/account/
HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Wed, 19 Aug 2020 09:35:41 GMT Keep-Alive: timeout=60 Connection: keep-alive
[ { "id": 1, "name": "张三", "username": "aaa", "password": "aaa" }, { "id": 2, "name": "李四", "username": "bbb", "password": "bbb" }, { "id": 3, "name": "王五", "username": "ccc", "password": "ccc" } ]
Response code: 200; Time: 35ms; Content length: 166 bytes
|