在pom.xml添加一下代碼,添加操做MySQL的依賴jar包。java
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,密碼123456mysql
編寫生成配置文件application-prod.ymlweb
1 server: 2 port: 8082 3 context-path: /
修改application.yml文件spring
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.javasql
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.javaapp
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工具發包給運行起來的服務器spring-boot
增長三條數據
打開數據庫能夠看到增長了三條數據
經過id查詢數據,看到返回id=2的數據
查詢年齡age爲22的數據列表
能夠看到返回兩條age=22的數據
修改數據
能夠看到id=2的數據修改爲了
刪除id=2的數據
能夠看到id=2的數據已經被刪掉了。