一. 员工信息分页查询1. 需求分析当系统中的用户越来越多页面展示不完整,我们需要通过实现分页的方式去展示员工的信息:
2. 代码开发在开发代码之前,需要理清楚程序的执行过程与业务逻辑:
页面发送ajax请求,将分页查询参数(page,pagesize,name)提交到服务端服务端
controller接收页面提交的数据并调用查询的数据
service调用mapper操作数据库,查询分页数据
controller将查询到的分页数据响应到页面
页面接收到分页的数据并通过elementui的table组件展示到页面上
其实页面的分页参数是通过json的格式传值后端,但是为何是图中是以这种问号的方式拼接的呢,原因是前端将请求进行一个拦截后重新拼接后的结果(前端代码不再叙述)。
配置分页插件
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;/** * 配置mybatis-plus分页插件 * @author jektong * @date 2022年05月01日 0:08 */@configurationpublic class mybatisplusconfig { @bean public mybatisplusinterceptor mybatisplusinterceptor(){ mybatisplusinterceptor mybatisplusinterceptor = new mybatisplusinterceptor(); mybatisplusinterceptor.addinnerinterceptor(new paginationinnerinterceptor()); return mybatisplusinterceptor; }}
controller层
/** * 员工信息分页查询 * * @param page 当前页 * @param pagesize 页码 * @param name 关键字查询 * @return */ @getmapping("/page") public r<page> page(int page, int pagesize, string name) { log.info("page={},pagesize={},name={}", page, pagesize, name); // 构造分页构造器 page pageinfo = new page(page, pagesize); // 构造条件 lambdaquerywrapper<employee> querywrapper = new lambdaquerywrapper(); querywrapper.like(stringutils.isnotempty(name), employee::getname, name).or() .like(stringutils.isnotempty(name),employee::getusername,name); // 添加排序 querywrapper.orderbydesc(employee::getupdatetime); // 执行查询 employeeservice.page(pageinfo, querywrapper); return r.success(pageinfo); }
二. 启用或禁用员工状态1 需求分析员工管理列表页,可以对某个员工状态进行启用或者禁用的操作。账号禁用的与员工不可登录系统,启用过后可以正常登录。这一操作只允许管理员进行操作。
2 代码开发前端核心代码页面中是如何做到只有管理员admin可以看到禁用按钮的,其实在前端只需获取到登录的账号,然后进行一个用户名判断即可:
页面初始化的时候就获取登录账号:
created() { this.init() this.user = json.parse(localstorage.getitem('userinfo')).username },
显示账号状态的那一列:
<el-table-column label="账号状态"> <template slot-scope="scope"> {{ string(scope.row.status) === '0' ? '已禁用' : '正常' }} </template></el-table-column>
向后端传递json的数据,将需要禁用员工的账号的id与状态传值后端,前端主要代码:
//状态修改statushandle (row) { this.id = row.id this.status = row.status this.$confirm('确认调整该账号的状态?', '提示', { 'confirmbuttontext': '确定', 'cancelbuttontext': '取消', 'type': 'warning' }).then(() => { enableordisableemployee({ 'id': this.id, 'status': !this.status ? 1 : 0 }).then(res => { console.log('enableordisableemployee',res) if (string(res.code) === '1') { this.$message.success('账号状态更改成功!') this.handlequery() } }).catch(err => { this.$message.error('请求出错了:' + err) }) })},
后端核心代码/** * 根据用户id去修改用户状态 * @param request * @param employee * @return */@postmapping public r<string> update(httpservletrequest request, @requestbody employee employee){ // 获取员工id long empid = (long) request.getsession().getattribute("employee"); employee.setupdatetime(localdatetime.now()); employee.setupdateuser(empid); employeeservice.updatebyid(employee); return r.success("员工信息修改成功");}
其实测试发现这段代码是不会被修改成功的,因为涉及一个js的精度问题,js识别long类型只精确到16位,而id是雪花算法生成的id有19位,导致id精度丢失。
代码修复如何解决上述问题,将页面的long类型转为字符串。具体步骤:
使用jacksonobjectmapper对json数据进行转换
在webconfig配置类中扩展sringmvc的消息转换器,镜像java对象到json数据的转换
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 org.springframework.stereotype.component;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] */@componentpublic 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); }}
webmvcconfig:
/** * 扩展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); }
修复之后员工状态可以正常修改,id也改变为字符串格式了:
以上就是如何使用java实现员工信息管理系统功能?的详细内容。