Springboot---REST风格、JPA实现GET\POST\PUT
标签:
SpringBoot
pom.xml
- 仅写需要的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
LuckmoneyController.java
- 使用postman类似软件发出URL请求
- 控制器接受URL请求
- 根据发送URL方式实现不同的操作请求
package ink.xlr.controller;
import ink.xlr.domain.Luckymoney;
import ink.xlr.repository.LuckmoneyRepository;
import ink.xlr.service.LuckmoneyService;
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;
/**
* @author 小龙人
*/
@RestController
public class LuckymoneyController {
@Autowired
private LuckmoneyRepository repository;
/**
* 获取红包列表
* @return
*/
@GetMapping("/luckymoneys")
public List<Luckymoney> list() {
return repository.findAll();
}
/**
* 创建红包(发红包)
*/
@PostMapping("/luckymoneys")
public Luckymoney create(@RequestParam("producer")String producer,
@RequestParam("money")BigDecimal money) {
Luckymoney luckymoney = new Luckymoney();
luckymoney.setProducer(producer);
luckymoney.setMoney(money);
return repository.save(luckymoney);
}
/**
* 通过id查询红包(不存返回null)
* @param id
* @return
*/
@GetMapping("/luckymoneys/{id}")
public Luckymoney findById(@PathVariable("id")Integer id) {
return repository.findById(id).orElse(null);
}
/**
* 更新红包
*/
@PutMapping("/luckymoneys/{id}")
public Luckymoney update(@PathVariable("id")Integer id,
@RequestParam("consumer")String consumer) {
Optional<Luckymoney> optional = repository.findById(id);
if (optional.isPresent()){
Luckymoney luckymoney = optional.get();
luckymoney.setId(id);
luckymoney.setConsumer(consumer);
return repository.save(luckymoney);
}
return null;
}
}
- @RestController = @ResponseBody(返回结果解析为JSON) + @Controller(声明为控制器)
- @Autowired依赖注入(只根据类型),@Resource(默认根据名称)
- @GetMapping("/luckymoneys")接受Get方式发送的URL请求,作为REST中查询方式
- @PostMapping("/luckymoneys")接受Post方式发送的URL请求,作为REST中的创建方式
- @PutMapping("/luckymoneys/{id}")接受Put方式发送的URL请求,作为REST中的部分更新方式
- @RequestParam获取请求体中的参数
- @PathVariable获取请求路径中的参数
Luckmoney.java
package ink.xlr.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.math.BigDecimal;
/**
* @author 小龙人
*/
@Entity
public class Luckymoney {
@Id
@GeneratedValue
private Integer id;
private BigDecimal money;
/**
* 发送方
*/
private String producer;
/**
* 接收方
*/
private String consumer;
public Luckymoney() {
}
public Integer getId() {
return id;
}
// getter、setter、toString
}
- @Entity表明该类(Luckmoney)为实体类,对应数据库表名为luckmoney
- @Id表明属性为主键
- @GeneratedValue提供主键生成策略
application.yml或application.properties
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/luckymoney?serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456789
jpa:
hibernate:
ddl-auto: update
show-sql: true
- spring.jpa.hibernate.ddl-auto:update ->每次运行程序,没有表格会新建表格,表内若有数据,只更新
- spring.jpa.show-sql->显示每次执行的sql语句(便于出现错误查看SQL语句)
LuckmoneyRepository.java
package ink.xlr.repository;
import ink.xlr.domain.Luckymoney;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author 小龙人
*/
@Repository
public interface LuckmoneyRepository extends JpaRepository<Luckymoney,Integer> {
}
- JpaRepository基于JPA的Respository接口,极大减少JPA作为数据访问的代码
- @Repository声明该类为数据访问组件
DemoApplication.java
package ink.xlr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦