ReceiveAddressController.java
package com.example.controller;
import com.example.entity.ReceiveAddress;
import com.example.service.IReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.time.LocalDateTime;
import java.util.List;
@Controller
@RequestMapping("/receive-address")
public class ReceiveAddressController {
@Autowired
private IReceiveAddressService addressService;
@GetMapping("{addrId}")
@ResponseBody
public ReceiveAddress getById(@PathVariable("addrId") Integer arrId){
ReceiveAddress address = addressService.getById(arrId);
return address;
}
@GetMapping("")
@ResponseBody
public List<ReceiveAddress> getAll(){
int custId = 1;
List<ReceiveAddress> addressList = addressService.getAll(custId);
return addressList;
}
@PostMapping
@ResponseBody
public boolean save(ReceiveAddress receiveAddress){
int customerId = 1;
receiveAddress.setCustId(customerId);
receiveAddress.setStatus(1);
receiveAddress.setCreateTime(LocalDateTime.now());
return addressService.save(receiveAddress);
}
@DeleteMapping("/{addrId}")
@ResponseBody
public boolean remove(@PathVariable("addrId") Integer addressId){
return addressService.removeById(addressId);
}
@PutMapping
@ResponseBody
public boolean update(ReceiveAddress receiveAddress){
return addressService.updateById(receiveAddress);
}
}
ReceiveAddress.java
package com.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
@TableName("receive_address")
public class ReceiveAddress implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "addr_id", type = IdType.AUTO)
private Integer addrId;
private Long receiveUserTelno;
private String receiveUsername;
private Integer custId;
private String addrProvince;
private String addrCity;
private String addrArea;
private String addrStreet;
private String addrDetail;
private Integer status;
private Integer version;
private LocalDateTime createTime;
private LocalDateTime updateTime;
public Integer getAddrId() {
return addrId;
}
public void setAddrId(Integer addrId) {
this.addrId = addrId;
}
public Long getReceiveUserTelno() {
return receiveUserTelno;
}
public void setReceiveUserTelno(Long receiveUserTelno) {
this.receiveUserTelno = receiveUserTelno;
}
public String getReceiveUsername() {
return receiveUsername;
}
public void setReceiveUsername(String receiveUsername) {
this.receiveUsername = receiveUsername;
}
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getAddrProvince() {
return addrProvince;
}
public void setAddrProvince(String addrProvince) {
this.addrProvince = addrProvince;
}
public String getAddrCity() {
return addrCity;
}
public void setAddrCity(String addrCity) {
this.addrCity = addrCity;
}
public String getAddrArea() {
return addrArea;
}
public void setAddrArea(String addrArea) {
this.addrArea = addrArea;
}
public String getAddrStreet() {
return addrStreet;
}
public void setAddrStreet(String addrStreet) {
this.addrStreet = addrStreet;
}
public String getAddrDetail() {
return addrDetail;
}
public void setAddrDetail(String addrDetail) {
this.addrDetail = addrDetail;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ReceiveAddress{" +
"addrId=" + addrId +
", receiveUserTelno=" + receiveUserTelno +
", receiveUsername=" + receiveUsername +
", custId=" + custId +
", addrProvince=" + addrProvince +
", addrCity=" + addrCity +
", addrArea=" + addrArea +
", addrStreet=" + addrStreet +
", addrDetail=" + addrDetail +
", status=" + status +
", version=" + version +
", createTime=" + createTime +
", updateTime=" + updateTime +
"}";
}
}
ReceiveAddressMapper.java
package com.example.mapper;
import com.example.entity.ReceiveAddress;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface ReceiveAddressMapper extends BaseMapper<ReceiveAddress> {
}
IReceiveAddressService.java
package com.example.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.ReceiveAddress;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
public interface IReceiveAddressService extends IService<ReceiveAddress> {
public List<ReceiveAddress> getAll(Integer customerId);
public boolean removeById(Integer addressId);
public boolean updateById(ReceiveAddress address);
}
ReceiveAddressServiceImpl.java
package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.ReceiveAddress;
import com.example.mapper.ReceiveAddressMapper;
import com.example.service.IReceiveAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class ReceiveAddressServiceImpl extends ServiceImpl<ReceiveAddressMapper, ReceiveAddress> implements IReceiveAddressService {
@Autowired
private ReceiveAddressMapper receiveAddressMapper;
public List<ReceiveAddress> getAll(Integer customerId){
QueryWrapper<ReceiveAddress> wrapper = new QueryWrapper<>();
wrapper.eq("cust_id",customerId).eq("status",1);
List<ReceiveAddress> receiveAddressList = receiveAddressMapper.selectList(wrapper);
return receiveAddressList;
}
@Override
public boolean removeById(Integer addressId) {
ReceiveAddress address = receiveAddressMapper.selectById(addressId);
address.setStatus(0);
address.setVersion(address.getVersion() + 1);
int rows = receiveAddressMapper.updateById(address);
return rows > 0;
}
@Override
public boolean updateById(ReceiveAddress address) {
int oldVersion = receiveAddressMapper.selectById(address.getAddrId()).getVersion();
address.setUpdateTime(LocalDateTime.now());
address.setVersion(oldVersion+1);
int rows = receiveAddressMapper.updateById(address);
return rows > 0;
}
}
SpringbootDemoApplication.java
package com.example;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
receive_address.sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `receive_address`;
CREATE TABLE `receive_address` (
`addr_id` int(0) NOT NULL AUTO_INCREMENT,
`receive_user_telno` bigint(0) NULL DEFAULT NULL,
`receive_username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`cust_id` int(0) NULL DEFAULT NULL,
`addr_province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的省份',
`addr_city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的城市',
`addr_area` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的区域',
`addr_street` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的街道',
`addr_detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '详细地址',
`status` int(0) NULL DEFAULT NULL COMMENT '状态',
`version` int(0) NULL DEFAULT NULL COMMENT '版本号,用于做乐观锁',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '数据添加的时间',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '数据修改时间',
PRIMARY KEY (`addr_id`) USING BTREE,
INDEX `fk_address_customer`(`cust_id`) USING BTREE,
CONSTRAINT `fk_address_customer` FOREIGN KEY (`cust_id`) REFERENCES `customer` (`cust_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
INSERT INTO `receive_address` VALUES (1, NULL, NULL, 1, '江苏省', '苏州市', '园区', '若水路', '若水路', 1, 1, '2023-08-11 13:47:02', NULL);
INSERT INTO `receive_address` VALUES (2, NULL, NULL, 1, '黑龙江', '大庆市', '市区', '育才路', '育才路', 1, 1, '2023-07-31 13:47:52', NULL);
SET FOREIGN_KEY_CHECKS = 1;
ReceiveAddressMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ReceiveAddressMapper">
</mapper>
application.yaml
server:
servlet:
context-path: /app
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/dicts?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
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.7.6</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot_demo</name>
<description>springboot_demo</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-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>