Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of Java employee Information Management

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to realize the information management function of Java employees". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

one。 Employee information paging query 1. Demand analysis

When more and more users in the system show incomplete pages, we need to display employees' information by paging:

two。 Code development

Before you develop the code, you need to understand the execution process and business logic of the program:

The page sends an Ajax request and submits the paging query parameter (page,pagesize,name) to the server

Controller receives the data submitted by the page and invokes the queried data

Service calls Mapper to operate the database and query paging data

Controller responds the paged data to the page.

The page receives the paged data and displays it on the page through the Table component of ElementUI

In fact, the paging parameters of the page are passed to the back end of the value through the JSON format, but why is it spliced in this way in the figure? the reason is that the front end intercepts the request and then re-splices the result (the front end code is no longer described).

Configure the paging plug-in

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;/** * configure the Mybatis-plus paging plugin * @ author jektong * @ date 0:08 on May 01, 2022 * / @ Configurationpublic class MybatisPlusConfig {@ Bean public MybatisPlusInterceptor mybatisPlusInterceptor () {MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor () MybatisPlusInterceptor.addInnerInterceptor (new PaginationInnerInterceptor ()); return mybatisPlusInterceptor;}

Controller layer

/ * * employee information paging query * * @ param page current page * @ param pageSize page * @ param name keyword query * @ return * / @ GetMapping ("/ page") public R page (int page, int pageSize, String name) {log.info ("page= {}, pageSize= {}, name= {}", page, pageSize, name) / / construct paging constructor Page pageInfo = new Page (page, pageSize); / / Construction condition LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper (); queryWrapper.like (StringUtils.isNotEmpty (name), Employee::getName, name). Or () .like (StringUtils.isNotEmpty (name), Employee::getUsername,name); / / add sorting queryWrapper.orderByDesc (Employee::getUpdateTime) / / execute query employeeService.page (pageInfo, queryWrapper); return R.success (pageInfo);} II. Enable or disable employee status 1 requirements analysis

The employee management list page, which can be enabled or disabled for an employee status. Account disabled and employees can not log in to the system, after enabled, you can log in normally. This operation only allows the administrator to do so.

2 code development front-end core code

How to make sure that only the administrator admin can see the disable button on the page. In fact, you only need to get the login account at the front end, and then make a user name judgment:

Get the login account when the page is initialized:

Created () {this.init () this.user = JSON.parse (localStorage.getItem ('userInfo')) .username}

The column showing the status of the account:

{{String (scope.row.status) ='0'? 'disabled': 'normal'}}

To pass the JSON data to the backend, you will need to disable the ID of the employee's account and the status value backend, and the main code of the frontend:

/ / status modification statusHandle (row) {this.id = row.id this.status = row.status this.$confirm ('confirm to adjust the status of this account?', 'prompt', {'confirmButtonText':' OK', 'cancelButtonText':' cancel' '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 ('account status changed successfully!') This.handleQuery ()}) .catch (err = > {this.$message.error ('request error:' + err)} The backend core code / * modifies the user status according to the user ID * @ param request * @ param employee * @ return * / @ PostMapping public R update (HttpServletRequest request, @ RequestBody Employee employee) {/ / get the employee ID Long empId = (Long) request.getSession () .getAttribute ("employee") Employee.setUpdateTime (LocalDateTime.now ()); employee.setUpdateUser (empId); employeeService.updateById (employee); return R.success ("employee information modified successfully");}

In fact, the test found that this code will not be modified successfully, because when it comes to the accuracy of a JS, JS identifies the Long type only to 16 bits, while ID has 19 bits of ID generated by the snowflake algorithm, resulting in the loss of ID accuracy.

Code repair

How to solve the above problem, change the Long type of the page to a string. Specific steps:

Using JacksonObjectMapper to transform JSON data

Extend the SringMVC message converter in the WebConfig configuration class to mirror the conversion of Java objects to JSON data

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 / * object mapper: converts Java objects to json based on jackson, or json to Java objects * the process of parsing JSON into Java objects is called [deserializing Java objects from JSON] * the process of generating JSON from Java objects is called [serializing Java objects to 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 (); / / No exception this.configure (FAIL_ON_UNKNOWN_PROPERTIES, false) is reported when an unknown attribute is received / / when deserialization, the compatibility processing this.getDeserializationConfig (). WithoutFeatures (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) where the property does not exist SimpleModule simpleModule = new SimpleModule () .addpromoializer (LocalDateTime.class, new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern (DEFAULT_DATE_TIME_FORMAT) .adddiagnoializer (LocalDate.class, new LocalDateDeserializer (DateTimeFormatter.ofPattern (DEFAULT_DATE_FORMAT) .adddiagnoializer (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) / / Registration function module for example, you can add custom serializers and deserializers this.registerModule (simpleModule);}}

WebMVCConfig:

/ * extended MVC message converter * @ param converters * / @ Override protected void extendMessageConverters (List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report