当前位置:网站首页>3.员工的增删改查
3.员工的增删改查
2022-06-24 09:43:00 【xjhqre】
员工的增删改查
1、新增员工
1.1、保存员工方法
创建save方法,路径:com/itheima/reggie/controller/EmployeeController.java
@PostMapping
public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
log.info("新增员工,员工信息:{}", employee.toString());
// 设置初始密码123456,需要进行md5加密处理
employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
// 获得当前登陆用户的id
Long empId = (Long) request.getSession().getAttribute("employee");
// 设置添加该员工信息的人的id
employee.setCreateUser(empId);
employee.setUpdateUser(empId);
// 将员工信息存入数据库
employeeService.save(employee);
return R.success("新增员工");
}
1.2、全局异常
创建全局异常捕获类GlobalExceptionHandler,对标注了指定注解的类的异常进行捕获
package com.itheima.reggie.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;
/** * @Author: xjhqre * @DateTime: 2022/6/17 14:17 */
// 对标上了RestController和Controller注解的类进行拦截
@Slf4j
@RestControllerAdvice(annotations = {
RestController.class, Controller.class})
public class GlobalExceptionHandler {
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex) {
log.error(ex.getMessage());
if (ex.getMessage().contains("Duplicate entry")) {
String[] split = ex.getMessage().split(" ");
String msg = split[2] + "已存在";
return R.error(msg);
}
return R.error("未知错误");
}
}
2、员工列表的分页查询
2.1、配置mybatis-plus的分页插件
在config目录下创建MybatisPlusConfig
package com.itheima.reggie.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * @Author: xjhqre * @DateTime: 2022/6/17 14:45 */
@Configuration
public class MybatisPlusConfig {
/** * 配置分页插件 */
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
2.2 查询方法编写
创建page方法,路径:com/itheima/reggie/controller/EmployeeController.java
// 分页查询员工信息
@GetMapping("/page")
public R<Page<Employee>> page(int page, int pageSize, String name) {
log.info("page = {}, pageSize = {}, name = {}", page, pageSize, name);
// 构造分页构造器
Page<Employee> pageInfo = new Page<>(page, pageSize);
// 构造条件构造器
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
// 添加过滤条件
queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
// 添加排序条件
queryWrapper.orderByDesc(Employee::getUpdateTime);
// 执行查询
employeeService.page(pageInfo, queryWrapper);
return R.success(pageInfo);
}
2.3 测试分页
如果想要测试分页按钮功能,可以添加多条数据或者修改每页的显示数量
若要修改每页显示数量可以修改前端list.html文件,路径:backend/page/member/list.html
修改以下两处数值:


3、启用/禁用员工账号
在查询员工列表时,后端会传给前端每个员工的信息,但由于员工的id属性是Long类型并且长度超过了js能处理的最大长度,导致精度丢失。
解决方法:
自定义消息转换器替换默认的消息转换器,在自定义转换器中把Long类型转成String类型后在发给前端。
3.1 编写JacksonObjectMapper类
在common目录下创建JacksonObjectMapper。
package com.itheima.reggie.common;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
/** * 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象 * 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象] * 从Java对象生成JSON的过程称为 [序列化Java对象到JSON] */
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注册功能模块 例如,可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}
3.2、扩展Spring MVC的消息转换器
在WebMvcConfig中重写extendMessageConverters方法
/** * 扩展mvc框架的消息转换器 * @param converters */
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
log.info("扩展消息转换器...");
// 创建消息转环器对象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
// 设置对象转环器,底层使用Jackson将java对象转换为json
messageConverter.setObjectMapper(new JacksonObjectMapper());
// 将上面的消息转换器对象追加到mvc框架的转换器集合中
converters.add(0, messageConverter);
}
3.3 编写更新方法
在EmployeeController里创建update方法
在代码里没有看到修改status,是因为该值是从前端传入的,已经封装在了形参employee里
/** * 更新员工信息 * @param request * @param employee * @return */
@PutMapping
public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
Long empId = (Long) request.getSession().getAttribute("employee");
employee.setUpdateUser(empId);
employee.setUpdateTime(LocalDateTime.now());
employeeService.updateById(employee);
return R.success("员工信息修改成功");
}
4、编辑员工信息
流程说明:
- 点击编辑按钮时,页面跳转到add.html,并在url中携带参数[员工id]
- 在add.html页面获取url中的参数
- 发送ajax请求,请求服务端,同时提交员工id参数
- 服务端接收请求,根据员工id查询员工信息,将员工信息以JSON形式响应给页面
- 页面接收服务端响应的JSON数据,通过vue的数据绑定进行员工信息回显
- 点击保存按钮,发送ajax请求,将页面中的员工信息以JSON方式提交给服务端,调用的是update方法
- 服务端接收员工信息,并进行处理,完成后给页面响应
- 页面接收到服务端响应信息后进行响应处理
add.html页面为公共页面,新增员工和编辑员工都是在此页面操作
4.1 创建回显数据的方法
在EmployeeController类中创建getById方法
/** * 根据id查询员工信息 * @param id * @return */
@GetMapping("/{id}")
public R<Employee> getById(@PathVariable(value = "id") Long id) {
log.info("根据id查询员工信息...");
Employee employee = employeeService.getById(id);
if (employee != null) {
return R.success(employee);
}
return R.error("没有查询到对应的员工信息");
}
4.2 测试修改员工信息
边栏推荐
- TP5 using post to receive array data times variable type error: solution to array error
- Indexeddb local storage, homepage optimization
- Distributed | how to make "secret calls" with dble
- el-table表格的拖拽 sortablejs
- linux下oracle服务器打开允许远程连接
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- js代理模式
- Cookie encryption 4 RPC method determines cookie encryption
- canvas无限扫描js特效代码
- How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
猜你喜欢

NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!

411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)

canvas 绘制图片

美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架

正规方程、、、

2021-08-17

Getting user information for applet learning (getuserprofile and getUserInfo)

SQL Sever中的窗口函数row_number()rank()dense_rank()

Binary tree part I

整理接口性能优化技巧,干掉慢代码
随机推荐
使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
416-二叉树(前中后序遍历—迭代法)
Cookie encryption 4 RPC method determines cookie encryption
Programming questions (continuously updated)
vim的使用
数组无缝滚动demo
p5.js千纸鹤动画背景js特效
How to manage massive network infrastructure?
linux下oracle服务器打开允许远程连接
canvas管道动画js特效
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
2021-08-17
机器学习——主成分分析(PCA)
Go language development environment setup +goland configuration under the latest Windows
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
2021-08-17
GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
413-二叉树基础