1. 程式人生 > 實用技巧 >springboot2+jpa+hikari+oracle例項演示

springboot2+jpa+hikari+oracle例項演示

1.技術選型


jpa:簡單方便,基於Spring構建,簡化了持久層開發,只要繼承Repository就有簡單的增刪改查功能了,且自帶分頁功能

hikari:最快的資料庫連線池,springboot2自帶,不需要用到監控頁面的話推薦使用hikari

oracle:生產環境最常用

2.程式碼例項


主要依賴pom.xml:

 1     <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <
artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-data-jpa</artifactId> 9 </dependency> 10 <
dependency> 11 <groupId>com.oracle</groupId> 12 <artifactId>ojdbc7</artifactId> 13 <version>12.1.0.1.0</version> 14 </dependency> 15 <!--很不錯的一個工具類庫-hutool,推薦使用--> 16 <dependency> 17 <
groupId>cn.hutool</groupId> 18 <artifactId>hutool-all</artifactId> 19 <version>5.4.2</version> 20 </dependency>

配置檔案application.yml:

 1 spring:
 2   datasource:
 3     ##使用hikari資料來源
 4     type: com.zaxxer.hikari.HikariDataSource
 5     ##使用oracle連線驅動
 6     driver-class-name: oracle.jdbc.driver.OracleDriver
 7     ##資料庫連線地址
 8     url: jdbc:oracle:thin:@000.000.000.000:1520:orcl
 9     ##資料庫連線使用者名稱
10     username: xxx
11     ##資料庫連線密碼
12     password: xxxxxx
13     hikari:
14       ##池中維護的最小空閒連線數
15       minimum-idle: 5
16       ##池中最大連線數,包括閒置和使用中的連線
17       maximum-pool-size: 15
18       ##自動提交從池中返回的連線
19       auto-commit: true
20       ##連線允許在池中閒置的最長時間
21       idle-timeout: 30000
22       ##連線池的使用者定義名稱,主要出現在日誌記錄和JMX管理控制檯中以識別池和池配置
23       pool-name: DatebookHikariCP
24       ##池中連線最長生命週期
25       max-lifetime: 18000000
26       ##等待來自池的連線的最大毫秒數
27       connection-timeout: 30000
28       ##驗證該連線是否是有效的查詢語句
29       connection-test-query: select 1 from dual

jpa自動生成實體類TestUserEntity:

 1 package com.project.demo.spring.boot.model;
 2 
 3 import javax.persistence.*;
 4 import java.util.Objects;
 5 
 6 /**
 7  * @author:wk
 8  * @date:2020/9/16
 9  */
10 @Entity
11 @Table(name = "TEST_USER", schema = "TW_E", catalog = "")
12 public class TestUserEntity {
13     private String name;
14     private String password;
15     private String phoneNo;
16     private String remark;
17     private String id;
18 
19     @Basic
20     @Column(name = "NAME")
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     @Basic
30     @Column(name = "PASSWORD")
31     public String getPassword() {
32         return password;
33     }
34 
35     public void setPassword(String password) {
36         this.password = password;
37     }
38 
39     @Basic
40     @Column(name = "PHONE_NO")
41     public String getPhoneNo() {
42         return phoneNo;
43     }
44 
45     public void setPhoneNo(String phoneNo) {
46         this.phoneNo = phoneNo;
47     }
48 
49     @Basic
50     @Column(name = "REMARK")
51     public String getRemark() {
52         return remark;
53     }
54 
55     public void setRemark(String remark) {
56         this.remark = remark;
57     }
58 
59     @Override
60     public boolean equals(Object o) {
61         if (this == o) return true;
62         if (o == null || getClass() != o.getClass()) return false;
63         TestUserEntity that = (TestUserEntity) o;
64         return Objects.equals(name, that.name) &&
65                 Objects.equals(password, that.password) &&
66                 Objects.equals(phoneNo, that.phoneNo) &&
67                 Objects.equals(remark, that.remark);
68     }
69 
70     @Override
71     public int hashCode() {
72         return Objects.hash(name, password, phoneNo, remark);
73     }
74 
75     @Id
76     @Column(name = "ID")
77     public String getId() {
78         return id;
79     }
80 
81     public void setId(String id) {
82         this.id = id;
83     }
84 }

建立TestUserRepository,繼承JpaRepository,可實現簡單的增刪改查,TestUserEntity和String引數為實體類和主鍵,加@Query註解實現自定義查詢語句:

 1 package com.project.demo.spring.boot.repository;
 2 
 3 import com.project.demo.spring.boot.model.TestUserEntity;
 4 import org.springframework.data.jpa.repository.JpaRepository;
 5 import org.springframework.data.jpa.repository.Query;
 6 import org.springframework.data.repository.query.Param;
 7 
 8 import java.util.List;
 9 
10 /**
11  * @author:wk
12  * @date:2020/9/16
13  */
14 public interface TestUserRepository extends JpaRepository<TestUserEntity, String> {
15 
16     /**
17      * 自定義查詢語句介面
18      *
19      * @param name
20      * @return
21      */
22     @Query(nativeQuery = true, value = "SELECT * FROM TEST_USER WHERE name = :name  ")
23     List<TestUserEntity> findUserByName(@Param("name") String name);
24 }

測試用的controller:

 1 package com.project.demo.spring.boot.controller;
 2 
 3 import cn.hutool.core.collection.CollectionUtil;
 4 import com.project.demo.spring.boot.model.TestUserEntity;
 5 import com.project.demo.spring.boot.repository.TestUserRepository;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.*;
 9 
10 import java.util.List;
11 
12 /**
13  * @author:wk
14  * @date:2020/9/11
15  */
16 @ResponseBody
17 @Controller
18 @RequestMapping("/demo")
19 public class TestUserController {
20     @Autowired
21     private TestUserRepository testUserRepository;
22 
23     @GetMapping("/getAllUser")
24     public List<TestUserEntity> getAllUser() {
25         List<TestUserEntity> allUser = testUserRepository.findAll();
26         return allUser;
27     }
28 
29     @PostMapping("/saveAllUser")
30     public List<TestUserEntity> saveAllUser(@RequestBody List<TestUserEntity> resource) {
31         List<TestUserEntity> allUser = CollectionUtil.newArrayList(resource);
32         List<TestUserEntity> successUser = testUserRepository.saveAll(allUser);
33         return successUser;
34     }
35 
36     @DeleteMapping("/deleteUserById")
37     public String deleteUserById(@RequestBody String ids) {
38         List<String> idList = CollectionUtil.toList(ids.split(","));
39         idList.parallelStream().forEach(id -> testUserRepository.deleteById(id));
40         return "success";
41     }
42 
43     @GetMapping("/findUserByName")
44     public List<TestUserEntity> findUserByName(@RequestBody String name) {
45         List<TestUserEntity> allUser = testUserRepository.findUserByName(name);
46         return allUser;
47     }
48 
49 }

3.演示


新增兩個使用者,新增成功:

獲取到所有的user:

呼叫自定義查詢介面,通過name查詢使用者:

刪除id為1和2的使用者

成功刪除後查詢為空: