Java框架spring Boot學習筆記(五):Spring Boot操作MySQL數據庫增、刪、改、查


在pom.xml添加一下代碼,添加操作MySQL的依賴jar包。

1 <dependency>
2   <groupId>org.springframework.boot</groupId>
3   <artifactId>spring-boot-starter-data-jpa</artifactId>
4 </dependency>
5 
6 <dependency>
7   <groupId>mysql</groupId>
8   <artifactId>mysql-connector-java</artifactId>
9 </dependency>

 

新建dbpeople的一個數據庫,用戶名root,密碼123456

 

編寫生成配置文件application-prod.yml

1 server:
2   port: 8082
3   context-path: /

 

修改application.yml文件

 1 spring:
 2   profiles:
 3     active: prod
 4   datasource:
 5     driver-class-name: com.mysql.jdbc.Driver
 6     url: jdbc:mysql://localhost:3306/dbpeople?useSSL=true
 7  username: root  8  password: 123456  9  jpa: 10  hibernate: 11  ddl-auto: create #update 12 show-sql: true

 

添加一個people類People.java

 1 package com.example.demo;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 
 7 //@Entity對應數據庫中的一個表
 8 @Entity
 9 public class People {
10 
11     //@Id配合@GeneratedValue,表示id自增長
12     @Id
13     @GeneratedValue
14     private Integer id;
15     private String name;
16     private Integer age;
17 
18     //必須要有一個無參的構造函數,不然數據庫會報錯
19     public People() {
20     }
21 
22     public Integer getId() {
23         return id;
24     }
25 
26     public void setId(Integer id) {
27         this.id = id;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public Integer getAge() {
39         return age;
40     }
41 
42     public void setAge(Integer age) {
43         this.age = age;
44     }
45 }

 

添加一個注入類PeopleProperties.java

 1 package com.example.demo;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 //獲取注入還需要加一個@Component注解,@ConfigurationProperties(prefix = "people")獲取前綴是people的配置
 7 @Component
 8 @ConfigurationProperties(prefix = "people")
 9 public class PeopleProperties {
10 
11     private String name;
12     private Integer age;
13 
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18     public void setAge(Integer age) {
19         this.age = age;
20     }
21 
22     public String getName() { return name; }
23     public Integer getAge() { return age; }
24 }

 

編寫一個數據庫操作自定義接口,定制操作接口,這里只演示定制查詢接口

PeopleRepository.java

 1 package com.example.demo;
 2 
 3 import org.springframework.data.jpa.repository.JpaRepository;
 4 
 5 import java.util.List;
 6 
 7 public interface PeopleRepository extends JpaRepository<People,Integer> {
 8     //通過年齡來查詢
 9     public List<People> findByAge(Integer age);
10 }

 

編寫數據庫操作類,可以進行增、刪、改、查等操作。

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.*;
 5 
 6 import java.util.List;
 7 
 8 @RestController
 9 public class PeopleController {
10     @Autowired
11     private PeopleRepository peopleRepository;
12 
13     /**
14      * 查詢數據庫中所有列表
15      * @return
16      */
17     @GetMapping(value = "/people")
18     public List<People> girlList(){
19         return peopleRepository.findAll();
20     }
21 
22     /**
23      * 增加一個人的數據
24      * @param name
25      * @param age
26      * @return
27      */
28     @PostMapping(value = "/people")
29     public People peopleAdd(@RequestParam("name") String name,
30                             @RequestParam("age") Integer age){
31         People people = new People();
32         people.setName(name);
33         people.setAge(age);
34 
35         return peopleRepository.save(people);
36     }
37 
38     /**
39      * 查詢一個人
40      * @param id
41      * @return
42      */
43     @GetMapping(value = "/people/{id}")
44     public People peopleFindOne(@PathVariable("id") Integer id){
45         return peopleRepository.findOne(id);
46     }
47 
48     /**
49      * 修改一個人的數據
50      * @param id
51      * @param name
52      * @param age
53      */
54     @PutMapping(value = "/people/{id}")
55     public People peopleUpdate(@PathVariable("id") Integer id,
56                              @RequestParam("name") String name,
57                              @RequestParam("age") Integer age){
58         People people = new People();
59         people.setId(id);
60         people.setName(name);
61         people.setAge(age);
62 
63         return peopleRepository.save(people);
64     }
65 
66     /**
67      * 刪除一個人的數據
68      * @param id
69      */
70     @DeleteMapping(value = "/people/{id}")
71     public void peopleDelete(@PathVariable("id") Integer id){
72         peopleRepository.delete(id);
73     }
74 
75     /**
76      * 通過年齡查詢列表
77      * @param age
78      * @return
79      */
80     @GetMapping(value = "/people/age/{age}")
81     public List<People> peopleListByAge(@PathVariable("age") Integer age){
82         return peopleRepository.findByAge(age);
83     }
84 }

 

運行,使用postman工具發包給運行起來的服務器

 

增加三條數據

 

 

 

打開數據庫可以看到增加了三條數據

 

通過id查詢數據,看到返回id=2的數據

 

查詢年齡age為22的數據列表

可以看到返回兩條age=22的數據

 

修改數據

 

可以看到id=2的數據修改成了

 

 

刪除id=2的數據

可以看到id=2的數據已經被刪掉了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM