Background:

In project management, we usually do for each parameter validation, calibration, front-end back-end check, in order to guarantee the consistency check, here we need to check we write a bit more efficient, not on paper are verified parameter is null, in order to be a mature engineer, we need to start working, dry goods


aboutParameter validationRelated issues of

1. Non-empty and repeated verification operations in project development:

Answer:

When a parameter is verified to be non-null, it is usually necessary to determine whether the parameter is an attribute of an object or the form of a set. We use the idea of classification discussion to start the solution:

1.1 Non-null verification of object Parameters:

①(Spring Valid)

    @notnull (message = "Data entry method cannot be empty, please check and fill in again ")
    private Integer dataMode;
    

    @notblank (message = "Data name cannot be blank ")
    @size (Max = 20, message = "data name, up to 20 Chinese characters ")
    @ the Pattern (regexp = "^ [\ \ u4E00 - \ \ u9FA5A - Za - z0-9 - '] {1, 20} $", message =" data name must be in Chinese and English letters, Numbers, ")
    private String dataName;

/* If an attribute is of String type, the @notblank annotation is valid for non-null validation. If it is of other types, the @notnull annotation is usually used for non-null validation, but there are also annotations for regular validation and data length */

Copy the code

For information on the use of Spring Valid annotations, check out this quick start blog

Blog.csdn.net/u012240455/…

② Verify the parameters using hutool library;

package cn.hutool.core.util;
// We use the hutool class, a Java code library

     public static boolean isNotBlank(CharSequence str) {
        return! isBlank(str); }public static boolean isBlankIfStr(Object obj) {
        if (null == obj) {
            return true;
        } else {
  return obj instanceof CharSequence ? isBlank((CharSequence)obj) : false; }}// Verify that String is null
 public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

/* CharSequence-- the parent of the String class, a sequence of strings, showing basic information about the String, such as int Length (); instanceof: Reserved word in Java, often used in ternary operations, to compare whether an object on the left is an instanceof an object on the right, for example, object instanceof CharSequence (implementation classes have string and stringbuffer, StringBuilder) * /
Copy the code

Take notes of previous interview questions:

StringBuilder andStringBuffer The difference between

StringBuilder and StringBuffer are mutable sequences of characters. Both inherit from AbstractStringBuilder and implement the CharSequence interface. However, StringBuilder is non-thread-safe, whereas StringBuffer is thread-safe.

1.2 Non-null Verification of Object Set Parameters (Set)

Answer: For sets, the most direct way to verify whether the set is empty is to find the set size()

① The size() method of the set itself, if greater than 0, proves to be non-empty

ArrayList.size(); //

HashMap.size();


Copy the code

(2) collecitonUtils is a tool class provided by Spring

package org.springframework.util;

// The spring framework's own utility class applies to List,MaP
CollectionUtils.isEmpty(List)
    
/ / a HashMAP
 Map has=new HashMap();
 CollectionUtils.isEmpty(has);


Copy the code

③ According to the hutool built-in Java class library,Collutil

// The spring framework's own utility class applies to List,MaP
Collutil.isEmpty(List)
    
Copy the code

Here’s a quick way to create a UUID

UUID.randomUUID().toString().replaceAll(\ \ "-"."")
Copy the code

1.3 For validation, the current collectionIs there duplicationData, how to find specific duplicate data

If there is duplicate data in the validation set, I will look to facilitate the duplication of validation for each element

// Determine that a collection cannot have duplicate data, collection utility class

 for (String value : dataList) {
int frequency = Collections.frequency(dataList, value);

 if (frequency > 1) {
     System.out.println("Parameters in the set"+value+It already exists.)
         // You can delete the current element directly
         dataList.remove();
   }
     
Copy the code

2. How to convert Json during data interaction;

In the process of interface development, we operate on the data. The queried data structure is returned to the front-end VO object after our back-end data verification and filtering operation. In fact, we have a unified object for docking query data, which is Result.

1. Develop a standard data receiving object in the system

Result

/* The standard data return object, which is commonly used as a docking object,lombok */
@Data
public class Result implements Serializable{
    
    // Return the code
    private Integer code;
      
    // The information returned
    private String  message;
    
    // The returned data
    private T data;
    
    // Request number
    private String requestId;
       
}
// Data contains large JSON data, which is usually encapsulated as a Map object

Copy the code

Json, as a universal data transmission format for unified data, has always been the main front and back end.

And then we convert that data object into JSON;

2. How to disassemble the object Result and get the Data they need

/ / hutool class library

jsonutil.toJsonStr(object o); // The object is converted to JSON

Map  dataMap=new HashMap();
dataMap.putAll(JSONUtil.toBean(JSONUtil.toJsonStr(result.getData()), Map.class));// Convert the object data to a map


// Then we need which specific data: for example, data is in result
object o =dataMap.get(result);  // Get all valid data

// Select * from Student; // Select * from Student
Student request = JSONUtil.toBean(JSONUtil.toJsonStr(o), Student.class);
// Get the current student data
Copy the code

For parameter validation, we take annotations, and the corresponding attribute constraints,

For direct display of code blocks in Typora:

Option + Command +c enters a code block

3. What fields a standard entity development class has

import java.util.*
import lombok.Data;
  
  /* Student table */
  
  @Data
  public class Student implements Serializable{
    
    private static final longSerialVersionUID u393l = - 65652384849505;// Add the id of the primary key
    @TableId(type=IdType.INPUT)
    private String oid;
      
    / / student ID
    private String studentId; 
    / / name
    private String name;
    / / score
    private double score;
    // Delete 1- delete 2- Not delete
    private Integer deteleteType;
    // Gender 1- male 2- female 3- Not selected
    private Integer genderType;
    / / founder
    private String  createorName;
    // Create time
    private Timestamp createTime;
    / / the modifier
    private String  modifyName;
    // Change the time
    private Timestamp modifyTime;

  }
Copy the code

3. Recommended backend learning enhancement framework:

About the backend in the table from id: recommend mybatisPlus official website, and the following a blog, interested to have a look;

Mybatisplus’s website

Newbie creation ->ID increment process

How to implement 🆔 increment process in the project

I’m Luca, and I’m going to stop sharing parameters here. If this article is helpful to you, please click “like” and go. Thank you

I hope we can make progress together 🤣