背景:为了更好的测试,你就得更好地了解开发,为了更好的了解开发,你就知道开发常用框架,那就来吧,第一个springboot
目的:增/删/改/查接口对MySQL进行操作
组网图:不涉及
工具:java version “1.8.0_65” ;Apache Maven 3.6.3;IDEA版本 2018.3 (准备步骤见本人其它博文)
本文会用到postman,下载地址见网盘,链接:https://pan.baidu.com/s/1giLFsjQXh3ZxacT1bH0ESA 提取码:qbw5
简介:Springboot使用Jpa对MySQL进行增删改查,详细的pom.xml和目录结构见本文末尾
第一步:在pom.xml增加如下两个依赖:
第二步:在数据库建库,详见本人博客:win7下搭建mysql数据库并使用Navicat进行连接 CREATE DATABASE girl;
在application-dev.yml,中增加数据库连接配置,如下:
server:
port: 8080
servlet:
context-path: /v1
woman:
age: 25
girl: 那个我喜欢的人
attribute: handsome
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/girl?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: 123456
jpa:
hibernate:
ddl-auto: create
show-sql: true
第三步,新建Girls类
代码如下:
package com.hua.myfirst;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.math.BigDecimal;
//@Entity说明这个class是实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名
@Entity
public class Girls {
// @Id是主键,@GeneratedValue是自增
@Id
@GeneratedValue
private Integer id;
private BigDecimal money;
// 发送方
private String producer;
// 接收方
private String consumer;
public Girls() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
public String getProducer() {
return producer;
}
public void setProducer(String producer) {
this.producer = producer;
}
public String getConsumer() {
return consumer;
}
public void setConsumer(String consumer) {
this.consumer = consumer;
}
}
启动项目:
可以在控制台中看见创建过程:
我们去数据库看,就可以看到新创建的这个表:
在数据库手动增加一条数据:
再次重启项目,发现表中的数据被情况。可以看控制台的逻辑,他每次都会删除表在创建表:
此时我们再加一条数据进数据库,同时将ddl-auto改为update
再次启动项目,发现数据还在:
第四步:新建接口GirlsRepository,也就是我们说的DAO层,去连接数据库
package com.hua.myfirst;
import org.springframework.data.jpa.repository.JpaRepository;
//JpaRepository<Girls,Integer>Girls数据库的实体类,以及Id的类型
public interface GirlsRepository extends JpaRepository<Girls,Integer> {
}
第五步:新建GET请求,获取girls列表,新建类GirlsController:
package com.hua.myfirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class GirlsController {
@Autowired
private GirlsRepository repository;
//获取女孩列表
@GetMapping("/girls")
public List<Girls> list() {
return repository.findAll();
}
}
启动springboot
打开链接:http://127.0.0.1:8080/v1/girls
第六步:新建POST请求,增加girls,还是在GirlsController类里面:
package com.hua.myfirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;
@RestController
public class GirlsController {
@Autowired
private GirlsRepository repository;
//获取女孩列表
@GetMapping("/girls")
public List<Girls> list() {
return repository.findAll();
}
//增加一个女孩
@PostMapping("/girls")
public Girls creat(@RequestParam("producer") String producer,
@RequestParam("money") BigDecimal money) {
Girls girls = new Girls();
girls.setProducer(producer);
girls.setMoney(money);
return repository.save(girls);
}
}
使用postman进行测试。如下:
再去数据库查看数据是否添加成功:
第七步:新建get请求,通过ID查找girls,还是在GirlsController类里面:
package com.hua.myfirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
@RestController
public class GirlsController {
@Autowired
private GirlsRepository repository;
//获取女孩列表
@GetMapping("/girls")
public List<Girls> list() {
return repository.findAll();
}
//增加一个女孩
@PostMapping("/girls")
public Girls creat(@RequestParam("producer") String producer,
@RequestParam("money") BigDecimal money) {
Girls girls = new Girls();
girls.setProducer(producer);
girls.setMoney(money);
return repository.save(girls);
}
//通过id查询红包
@GetMapping("/girls/{id}")
public Girls findById(@PathVariable("id") Integer id) {
return repository.findById(id).orElse(null);
}
}
使用postman进行测试。如下:
第八步:新建put请求,通过ID更改consumer,还是在GirlsController类里面:
package com.hua.myfirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
@RestController
public class GirlsController {
@Autowired
private GirlsRepository repository;
//获取女孩列表
@GetMapping("/girls")
public List<Girls> list() {
return repository.findAll();
}
//增加一个女孩
@PostMapping("/girls")
public Girls creat(@RequestParam("producer") String producer,
@RequestParam("money") BigDecimal money) {
Girls girls = new Girls();
girls.setProducer(producer);
girls.setMoney(money);
return repository.save(girls);
}
//通过id查询红包
@GetMapping("/girls/{id}")
public Girls findById(@PathVariable("id") Integer id) {
return repository.findById(id).orElse(null);
}
//更新girls
@PutMapping("/girls/{id}")
public Girls update(@PathVariable("id") Integer id,
@RequestParam("consumer") String consumer) {
Optional<Girls> optional = repository.findById(id);
if (optional.isPresent()) {
Girls girls = optional.get();
girls.setConsumer(consumer);
return repository.save(girls);
}
return null;
}
}
使用postman进行测试。如下:
再去数据库查看数据是否更改成功:
第九步:新建delete请求,通过ID删除girls,还是在GirlsController类里面:
package com.hua.myfirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
@RestController
public class GirlsController {
@Autowired
private GirlsRepository repository;
//获取女孩列表
@GetMapping("/girls")
public List<Girls> list() {
return repository.findAll();
}
//增加一个女孩
@PostMapping("/girls")
public Girls creat(@RequestParam("producer") String producer,
@RequestParam("money") BigDecimal money) {
Girls girls = new Girls();
girls.setProducer(producer);
girls.setMoney(money);
return repository.save(girls);
}
//通过id查询红包
@GetMapping("/girls/{id}")
public Girls findById(@PathVariable("id") Integer id) {
return repository.findById(id).orElse(null);
}
//更新girls
@PutMapping("/girls/{id}")
public Girls update(@PathVariable("id") Integer id,
@RequestParam("consumer") String consumer) {
Optional<Girls> optional = repository.findById(id);
if (optional.isPresent()) {
Girls girls = optional.get();
girls.setConsumer(consumer);
return repository.save(girls);
}
return null;
}
//删除girls
@DeleteMapping("/girls/{id}")
public String delete(@PathVariable("id") Integer id) {
Optional<Girls> optional = repository.findById(id);
if (optional.isPresent()) {
repository.deleteById(id);
return "delete success";
}
return "delete fail";
}
}
表中原有数据:
删除ID=7的数据:
再次产看表中数据:
至此,使用jpa进行增删改查演示完毕。
备注,此项目的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hua</groupId>
<artifactId>myfirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myfirst</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
此项目的函数入口和目录结构:
package com.hua.myfirst;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyfirstApplication {
public static void main(String[] args) {
SpringApplication.run(MyfirstApplication.class, args);
}
}