在pom.xml添加一下代码,添加操作MySQL的依赖jar包。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
新建dbpeople的一个数据库,用户名root,密码123456
编写生成配置文件application-prod.yml
server:
port: 8082
context-path: /
修改application.yml文件
spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbpeople?useSSL=true
username: root password: 123456 jpa: hibernate: ddl-auto: create #update show-sql: true
添加一个people类People.java
package com.example.demo; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; //@Entity对应数据库中的一个表
@Entity
public class People { //@Id配合@GeneratedValue,表示id自增长
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age; //必须要有一个无参的构造函数,不然数据库会报错
public People() {
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} 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;
}
}
添加一个注入类PeopleProperties.java
package com.example.demo; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; //获取注入还需要加一个@Component注解,@ConfigurationProperties(prefix = "people")获取前缀是people的配置
@Component
@ConfigurationProperties(prefix = "people")
public class PeopleProperties { private String name;
private Integer age; public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
} public String getName() { return name; }
public Integer getAge() { return age; }
}
编写一个数据库操作自定义接口,定制操作接口,这里只演示定制查询接口
PeopleRepository.java
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface PeopleRepository extends JpaRepository<People,Integer> {
//通过年龄来查询
public List<People> findByAge(Integer age);
}
编写数据库操作类,可以进行增、删、改、查等操作。
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class PeopleController {
@Autowired
private PeopleRepository peopleRepository; /**
* 查询数据库中所有列表
* @return
*/
@GetMapping(value = "/people")
public List<People> girlList(){
return peopleRepository.findAll();
} /**
* 增加一个人的数据
* @param name
* @param age
* @return
*/
@PostMapping(value = "/people")
public People peopleAdd(@RequestParam("name") String name,
@RequestParam("age") Integer age){
People people = new People();
people.setName(name);
people.setAge(age); return peopleRepository.save(people);
} /**
* 查询一个人
* @param id
* @return
*/
@GetMapping(value = "/people/{id}")
public People peopleFindOne(@PathVariable("id") Integer id){
return peopleRepository.findOne(id);
} /**
* 修改一个人的数据
* @param id
* @param name
* @param age
*/
@PutMapping(value = "/people/{id}")
public People peopleUpdate(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age){
People people = new People();
people.setId(id);
people.setName(name);
people.setAge(age); return peopleRepository.save(people);
} /**
* 删除一个人的数据
* @param id
*/
@DeleteMapping(value = "/people/{id}")
public void peopleDelete(@PathVariable("id") Integer id){
peopleRepository.delete(id);
} /**
* 通过年龄查询列表
* @param age
* @return
*/
@GetMapping(value = "/people/age/{age}")
public List<People> peopleListByAge(@PathVariable("age") Integer age){
return peopleRepository.findByAge(age);
}
}
运行,使用postman工具发包给运行起来的服务器
增加三条数据
打开数据库可以看到增加了三条数据
通过id查询数据,看到返回id=2的数据
查询年龄age为22的数据列表
可以看到返回两条age=22的数据
修改数据
可以看到id=2的数据修改成了
删除id=2的数据
可以看到id=2的数据已经被删掉了。