<!--1.导入data JPA-->
org.springframework.boot</groupId>
spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--2.mysql-->
mysql</groupId>
mysql-connector-java</artifactId>
</dependency>
server:
port: 8082
servlet:
context-path: /redpackage
limit:
minMoney: 0.01
maxMoney: 10000
description: 最少要发${limit.minMoney}元 , 最多不能超过${limit.maxMoney}元
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/redpackage?serverTimezone=UTC
username: root
password: admins
jpa:
hibernate:
ddl-auto: create
show-sql: true
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.20.jar:8.0.20]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.20.jar:8.0.20]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.20.jar:8.0.20]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.20.jar:8.0.20]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.20.jar:8.0.20]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) ~[mysql-connector-java-8.0.20.jar:8.0.20]
需要在yml的mysql server配置中填在serverTimezone=UTC
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/redpackage?serverTimezone=UTC
username: root
password: admins
jpa:
hibernate:
ddl-auto: create
show-sql: true
package com.cevent.springboot.redpackage;/**
* Created by Cevent on 2020/7/15.
*/
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.math.BigDecimal;
/**
* @author cevent
* @description 红包实体类
* @date 2020/7/15 13:18
* 持久化persistence固态层@Entity @Id @GeneratedValue
*/
@Entity
public class Redpackage {
//id需要引入persistence持久化,初始化@GeneratedValue
@Id
@GeneratedValue
private Integer id;
private BigDecimal money;
//发送方
private String producer;
//接收方
private String consumer;
public Redpackage() {
}
public Redpackage(Integer id, BigDecimal money, String producer, String consumer) {
this.id = id;
this.money = money;
this.producer = producer;
this.consumer = consumer;
}
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;
}
@Override
public String toString() {
return "Redpackage{" +
"id=" + id +
", money=" + money +
", producer='" + producer + '\'' +
", consumer='" + consumer + '\'' +
'}';
}
}
server:
port: 8082
servlet:
context-path: /redpackage
limit:
minMoney: 0.01
maxMoney: 10000
description: 最少要发${limit.minMoney}元 , 最多不能超过${limit.maxMoney}元
## hibernate自动创建表采用ddl-auto: create 如果不需要在启动项目时重建表,需要使用update
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/redpackage?serverTimezone=UTC
username: root
password: admins
jpa:
hibernate:
## ddl-auto: create
ddl-auto: update
show-sql: true
package com.cevent.springboot.redpackage;/**
* Created by Cevent on 2020/7/15.
*/
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;
/**
* @author cevent
* @description
* @date 2020/7/15 13:32
*/
@RestController
public class RedpackageController {
//获取DAO接口
@Autowired
private RedpackageRepository redpackageRepository;
//1. 获取红包列表
@GetMapping("/moneys")
public List<Redpackage> list(){
return redpackageRepository.findAll();
}
}
package com.cevent.springboot.redpackage;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Cevent on 2020/7/15.
* DAO:调用数据层
*/
public interface RedpackageRepository extends JpaRepository<Redpackage,Integer> {
}
//2. 新建(发)红包
@PostMapping("/addRP")
public Redpackage addRedPackage(@RequestParam("producer") String producer,
@RequestParam("money")BigDecimal money){
Redpackage redpackage=new Redpackage();
redpackage.setProducer(producer);
redpackage.setMoney(money);
return redpackageRepository.save(redpackage);
}
## hibernate自动创建表采用ddl-auto: create 如果不需要在启动项目时重建表,需要使用update
## 设置编码:?characterEncoding=UTF-8
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/redpackage?serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: admins
jpa:
hibernate:
## ddl-auto: create
ddl-auto: update
show-sql: true
//3. 通过id查询红包
@GetMapping("/getRPid/{id}")
public Redpackage getByid(@PathVariable("id") Integer id){
return redpackageRepository.findById(id).orElse(null);
}
http://localhost:8082/redpackage/getRPid/1
//4. 更新(领取)红包
@PutMapping("/updateRP/{id}")
public Redpackage updataRP(@PathVariable("id") Integer id,
@RequestParam("consumer") String consumer,
@RequestParam("money") BigDecimal money){
//先查询出当前id
Optional<Redpackage> optional=redpackageRepository.findById(id);
if(optional.isPresent()){
//带入属性值,从optional中获取
Redpackage redpackage=optional.get();
redpackage.setId(id);
redpackage.setConsumer(consumer);
redpackage.setMoney(money);
return redpackageRepository.save(redpackage);
}
return redpackageRepository.findById(2).orElse(null);
}
数据库事务:作为单个逻辑工作单元,执行的一系列命令操作,全部执行 OR 不执行(全部回滚)
新建服务层,同时创建2个redpackage
package com.cevent.springboot.redpackage;/**
* Created by Cevent on 2020/7/15.
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
/**
* @author cevent
* @description
* @date 2020/7/15 16:51
*/
@Service
public class RedpackageService {
@Autowired
private RedpackageRepository redpackageRepository;
public void createDoubleRP(){
Redpackage redpackage1=new Redpackage();
redpackage1.setProducer("刘一刀");
redpackage1.setMoney(new BigDecimal("520"));
redpackageRepository.save(redpackage1);
Redpackage redpackage2=new Redpackage();
redpackage2.setProducer("刘一刀");
redpackage2.setMoney(new BigDecimal("1314"));
redpackageRepository.save(redpackage2);
}
}
添加post请求
//5.两个内容对象同时发送
@PostMapping("/redpackage/double")
public void createdDouble(){
redpackageService.createDoubleRP();
}
http://localhost:8082/redpackage/redpackage/double
添加发送失败
CREATE TABLE `redpackage` (
`id` int(11) NOT NULL,
`consumer` varchar(255) DEFAULT NULL,
`money` decimal(7,2) DEFAULT NULL,
`producer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
开启MySQL事务,需要将MySQL的引擎类型更改为InnoDB,两个业务设置同一事务,错误则全部回滚
package com.cevent.springboot.redpackage;/**
* Created by Cevent on 2020/7/15.
*/
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 cevent
* @description
* @date 2020/7/15 13:32
*/
@RestController
public class RedpackageController {
//获取DAO接口
@Autowired
private RedpackageRepository redpackageRepository;
//调用Service获取2个包
@Autowired
private RedpackageService redpackageService;
//1. 获取红包列表
@GetMapping("/moneys")
public List<Redpackage> list(){
return redpackageRepository.findAll();
}
//2. 新建(发)红包
@PostMapping("/addRP")
public Redpackage addRedPackage(@RequestParam("producer") String producer,
@RequestParam("money")BigDecimal money){
Redpackage redpackage=new Redpackage();
redpackage.setProducer(producer);
redpackage.setMoney(money);
return redpackageRepository.save(redpackage);
}
//3. 通过id查询红包
@GetMapping("/getRPid/{id}")
public Redpackage getByid(@PathVariable("id") Integer id){
return redpackageRepository.findById(id).orElse(null);
}
//4. 更新(领取)红包
@PutMapping("/updateRP/{id}")
public Redpackage updataRP(@PathVariable("id") Integer id,
@RequestParam("consumer") String consumer,
@RequestParam("money") BigDecimal money){
//先查询出当前id
Optional<Redpackage> optional=redpackageRepository.findById(id);
if(optional.isPresent()){
//带入属性值,从optional中获取
Redpackage redpackage=optional.get();
redpackage.setId(id);
redpackage.setConsumer(consumer);
redpackage.setMoney(money);
return redpackageRepository.save(redpackage);
}
return redpackageRepository.findById(2).orElse(null);
}
//5.两个内容对象同时发送
@PostMapping("/redpackage/double")
public void createdDouble(){
redpackageService.createDoubleRP();
}
}