存销系统的售后模块---维修点管理
1.模块简介
1 | 返厂入库管理 |
2 | 返厂入库单审核 |
3 | 返厂出库管理 |
4 | 返厂单审核出库 |
5 | 退换货网点管理 |
博主先做的退换货网点管理,因为此模块相对独立,涉及到退换货网点的增删改查,
此图是在网络上搜的截图,效果大致是一样的
2.Sql表
见到原型图,就能知道数据库的字段了
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for service_point_address
-- ----------------------------
DROP TABLE IF EXISTS `service_point_address`;
CREATE TABLE `service_point_address` (
`service_point_id` int(0) NOT NULL AUTO_INCREMENT,
`service_point_province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '维修点省',
`service_point_city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '维修点市/区',
`service_point_company` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司名称',
`detail_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '详细地址(具体到号)',
`phone_num` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电话号码',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '维修点创建时间',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '维修点修改时间',
PRIMARY KEY (`service_point_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
3.项目结构
4.代码书写总结
entity实体类
实体类我用的easycode自动生成,这个应该有教程,很简单
lombok注解
@Data 自动生成Java类的一些常用方法,如getter、setter、toString、equals和hashCode等
@NoArgsConstructor @AllArgsConstructor 无参构造和有参构造
注意:有参构造生成的是全参构造,不能选中其中某一个或某几个生成构造
mapper类(Dao层)
@Repository
Spring框架的注解,用于将DAO(数据访问对象)或持久化层的类标识为存储库组件
BaseMapper<ServicePointAddress>接口
BaseMapper接口是一个泛型接口,用于定义通用的数据库操作方法,例如插入、更新、删除和查询等。
BaseMapper源码
XML文件(easycode自动生成)
service层
这层会有service接口,impl实现类(为了代码的可维护性,以接口之名暴露实现)
service接口(没啥说的,就是kuku写方法接口)
service实现类
@Servie
Spring框架中的注解之一,用于将一个类标识为服务层组件。
使用@Service
注解标记的类将被Spring容器自动扫描并实例化,通过依赖注(如@Autowired @Resource 二者实现方式不同,但作用相同)可以在其他组件中使用该类的实例。
controller层
注解讲解
这一层主要是将接口暴露给前端,以便前端调用(注解里带"/" URL路径)
@RestController
Spring框架中的注解之一,用于将一个类标识为RESTful风格的控制器组件。
在Spring MVC中,@RestController
注解通常与控制器类一起使用。控制器负责处理来 自客户端的HTTP请求,并返回相应的HTTP响应
@RequestMapping
Spring框架中的注解之一,用于将类或方法映射到指定的URL路径上
应用于类级别时,表示该类中的所有方法都映射到指定的URL路径上
注解应用于方法级别时,表示该方法映射到指定的URL路径上
@Resource
前文讲过。将其他类注入
@GetMapping
获取特定资源或一组资源的表示。通常用于读操作(浏览器就可操作)
GET请求的参数通常通过查询字符串(query parameters)传递,并将结果以响应体返回
@PostMapping
用于向服务器提交数据,创建新资源或执行非幂等的操作(可以理解为,传递实体类这种 操作就要用post,因为数据可能较大,做项目时,不仅仅是服务器传输,可能还包含网 络传输,这也是为什么实体类要进行序列化)
@PutMapping
用于向服务器更新或替换特定资源,可以多次执行具有相同结果
@DeleteMapping
用于删除特定资源
@PatchMapping(本项目没用到)
用于对资源进行部分更新。与PUT方法不同,PATCH仅更新部分资源,而不是整个资源
注意点:图片中有{id}格式的数据,此数据表示在浏览器或者postman软件中要加上/id
GET和PUT请求通常有返回值,而POST和DELETE请求的返回值取决于具体的业务需求 和实现方式。具体的返回值类型和格式可以根据开发框架、业务需求和约定进行定义。
get请求通常以HTML、JSON、XML等格式呈现
post请求自定义的JSON响应、HTTP状态码
此代码中是自带的响应码
@PostMapping("/inserteAddress")
public ResponseEntity<String> insertAddress(@RequestBody ServicePointAddress servicePointAddress){
SPAService.insertAddress(servicePointAddress);
return ResponseEntity.ok("新增数据成功");
}
@DeleteMapping("/deleteAddress")
public ResponseEntity<String> deleteAddress(@RequestBody ServicePointAddress servicePointAddress){
SPAService.deleteAddress(servicePointAddress);
return ResponseEntity.ok("删除成功");
}
@PutMapping("/updateAddress/{id}")
public ResponseEntity<String> updateAddress(@PathVariable("id") Integer id, @RequestBody ServicePointAddress servicePointAddress){
SPAService.updateAddress(id, servicePointAddress);
return ResponseEntity.ok("修改成功");
}
@RequestParam(value = " " ,required = false) Xxx xxx
Spring 框架中用于从请求参数中获取值的注解,它可以用来绑定 HTTP 请求中的特定参 数值到方法的参数上
value = " "
Spring 框架中用于从请求参数中获取值的注解,它可以用来绑定 HTTP 请求中的特 定参数值到方法的参数上
required = false
指定该请求参数为非必须。
如果没有提供名为 "id " 的参数值,方法的参数id将会是null值。
如果将 required设置为 true,表示该参数是必须提供的,如果请求中缺少该参数,将 会产生错误响应
controller层不写什么复杂的逻辑,主要是返回数据给前端,暴露接口给前端
代码细节总结
1.条件构建器
普通条件构建器
QueryWrapper<ServicePointAddress> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("service_point_province", servicePointAddress.getServicePointProvince())
.eq("service_point_city", servicePointAddress.getServicePointCity())
.eq("service_point_company",servicePointAddress.getServicePointCompany())
.eq("detail_address", servicePointAddress.getDetailAddress())
.eq("phone_num", servicePointAddress.getPhoneNum());
ServicePointAddress existingAddress = servicePointAddressMapper.selectOne(queryWrapper);
Lambda条件构建器
.eq(!StringUtils.isEmpty(servicePointProvince), ServicePointAddress::getServicePointProvince, servicePointProvince)
.eq(condition, property, value)
:这是一个封装的条件操作
condition
:第一个参数是一个条件表达式,这里是判断servicePointProvince
是否为空的条件。property
:第二个参数是一个方法引用,指定要应用条件的对象属性,这里是ServicePointAddress
类中的getServicePointProvince()
方法。value
:第三个参数是根据servicePointProvince
的值来确定的。
LambdaQueryWrapper<ServicePointAddress> servicePointAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
servicePointAddressLambdaQueryWrapper
.eq(!StringUtils.isEmpty(servicePointProvince), ServicePointAddress::getServicePointProvince, servicePointProvince)
.eq(!StringUtils.isEmpty(servicePointCity), ServicePointAddress::getServicePointCity, servicePointCity)
.eq(!StringUtils.isEmpty(servicePointCompany), ServicePointAddress::getServicePointCompany, servicePointCompany)
.eq(!StringUtils.isEmpty(detailAddress), ServicePointAddress::getDetailAddress, detailAddress)
.eq(!StringUtils.isEmpty(phoneNum), ServicePointAddress::getPhoneNum, phoneNum);
List<ServicePointAddress> servicePointAddresses = servicePointAddressMapper.selectList(servicePointAddressLambdaQueryWrapper);
2.时间类型
Sql里有每个维修点的创建时间和修改时间,意思就是创建的时候会将当前时间节点写入数据库,维修时间同解。
这个主要是Sql里的时间类型是DateTime,Java可以写成String 或 Date类型
不同的写法有不同的转换方法
String类型时间转换
/**
* 增加
* @param servicePointAddress
*/
@Override
public void insertAddress(ServicePointAddress servicePointAddress) {
QueryWrapper<ServicePointAddress> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("service_point_province", servicePointAddress.getServicePointProvince())
.eq("service_point_city", servicePointAddress.getServicePointCity())
.eq("service_point_company",servicePointAddress.getServicePointCompany())
.eq("detail_address", servicePointAddress.getDetailAddress())
.eq("phone_num", servicePointAddress.getPhoneNum());
ServicePointAddress existingAddress = servicePointAddressMapper.selectOne(queryWrapper);
//非空判断,如果数据存在,就提示修改,不存在就新增
if (existingAddress == null){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"));
String currentTime = simpleDateFormat.format(new Date());
servicePointAddress.setCreateTime(currentTime);
servicePointAddressMapper.insert(servicePointAddress);
}else {
throw new RuntimeException("数据已存在,若需要修改,请移步至修改操作");
}
}
Date时间类型转换
/**
* 修改
* @param servicePointAddress
*/
@Override
public void updateAddress(Integer id,ServicePointAddress servicePointAddress) {
//如果实体类的时间为Date类型 item.setCreateTime(new Timestamp(System.currentTimeMillis()));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = simpleDateFormat.format(new Date());
servicePointAddress.setUpdateTime(currentTime);
servicePointAddressMapper.updateById(servicePointAddress);
}