当前位置:网站首页>Ruiji takeout project (II)

Ruiji takeout project (II)

2022-06-25 09:42:00 Cold Snowflakes

front end

JSON object :
and JS Object difference :

JSON The property name of the object (key) Must be enclosed in double quotes .
 and JavaScript Object except for property names with spaces 、 There is a hyphen in the middle - The property name of must be inside or outside double quotes ,  Other random .
 Can't be in JSON Object ,  And in the JavaScript Objects can .

JSON character string :
To transfer data between the front end and the back end, you can use JSON, But what is actually being passed on is JSON character string , and JSON Objects cannot be passed directly .
JSON The string is in JSON On both sides of the object ' Formed string .

let JSONObject  = {
    "k1":"v1","k2":"v2"};			// JSON object  
let JSONString  = '{"k1":"v1","k2":"v2"}';			// JSON character string  

JSON.parse : take JSON character string Convert to JS object .
JSON.stringify : take JSON object Convert to JSON character string .
computed: Compute properties
this.$refs[formName].validate:
< template slot-scope=“scope” >

Respond to JSON Data custom serialization type

The entity class that encapsulates the response data

@Data
public class employee implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private Long id;
    private String username;
    private String name;
    private String password;
    private String phone;
    private String sex;
    private String id_number;
    private Integer status;

    private LocalDateTime create_time;
    private LocalDateTime update_time;

    private Long create_user;
    private Long update_user;
}

 Insert picture description here

/** *  Object Converter :  be based on jackson take Java Object to json,  Or will json To Java object  *  take JSON It can be interpreted as Java The process of an object is called  [ from JSON Deserialization Java object ] *  from Java Object to generate JSON The process is called  [ serialize Java Object to 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();

        // No exception is reported when an unknown attribute is received 
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
        // When deserializing ,  Property does not exist 
        this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);

        SimpleModule simpleModule = new SimpleModule()
        //  Deserialization  
                .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)))
        //  serialize   Respond to  JSON  Data time ,  Change the serialization type of the following types 
                .addSerializer(BigInteger.class, ToStringSerializer.instance)
                .addSerializer(Long.class, ToStringSerializer.instance) //  take  domain in Long Type field ,  Serialize to  JSON  when , Serialize to  String
                .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);
    }
}

stay WebMvcConfig Configuration class , make carbon copies extendMessageConverters Method , Extended message converter .

    /** *  Expand  mvc  frame   Message converter for  * @param converters */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    
        log.info(" Extended message converter ....");
        //  establish   Message converter object 
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

        //  Set up   Object Converter 
        messageConverter.setObjectMapper(new JackSonObjectMapper());

        //  Append to  mvc  In the converter collection of the framework ,  And put it in the first place 
        converters.add(0,messageConverter);
    }

 Insert picture description here

ThreadLocal

Use SpringBoot When the server is embedded , If after starting , The front page file has been modified , You need to restart the server at the back end , The front end clears the browser cache , Only in this way can it work .

ThreadLocal, yes Thread Local variables of .
When using ThreadLocal When maintaining variables ,ThreadLocal Provide a separate copy of the variable for each thread that uses the variable .
So each thread can independently change its own copy , It will not affect the copy corresponding to other threads .ThreadLocal Provide a separate storage space for each thread , It has the effect of thread isolation .

/** *  be based on  ThreadLocal  Wrapper utility class  */
public class BaseContext {
    

    // ThreadLocal  Is thread isolated 
    private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    //  Store value 
    public static void setCurrentId(Long id){
    
        threadLocal.set(id);
    }

    //  Value 
    public static Long getCurrentId(){
    
        return threadLocal.get();
    }
}
//  Use 
Long empId = (Long) request.getSession().getAttribute("employee");
BaseContext.setCurrentId(empId);

metaObject.setValue("updateUser",BaseContext.getCurrentId());

Data table primary key

Why is the primary key of a data table not set to auto increment , The back end is inserting , There is no auto fill , Set the default value , But you get a value .
because mybatis-plus The default policy for primary key generation is :ASSIGN_ID , This strategy will use snowflake algorithm to generate primary key automatically ID, The primary key type is Long or String.
Mybatis-plus Primary key generation policy

原网站

版权声明
本文为[Cold Snowflakes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250906143771.html