• Note is essentially a string in the field type Eagle-eyed colleagues should have found that we on the frame of essentially a JDBC connection string in the “sendStringParametersAsUnicode = false” parameter, unfortunately, so far, Only one student questioned me. Why use it? The answer is to minimize implicit conversions. The JDBC protocol defines two ways for Java to send strings to the database server: setString() and setNString(), which are used to send ASCII and unicode-encoded strings to SQLServer, respectively. We should all know that for Newegg, most string fields are CHAR or VARCHAR, not “N”. MyBatis and most data layer frameworks use setString() by default. But if he does not specify in its JDBC implementation of Microsoft “sendStringParametersAsUnicode = false” parameter, even with setString () method, the actual sent to the server is still a Unicode string, this just and runs counter to our field type, There is an implicit conversion problem that is difficult to troubleshoot (do not walk the index).
  • Proper Logging We generally use the log.debug/info/ WARN methods for logging, but these methods do not have lazy evaluation features. Therefore, when we use the log function, when the string needs a large amount of operations, we should first determine the current log status to avoid extra overhead. Take a closer look at the following code:

    LOGGER.debug("ItemBaseInfo Response: " + JSONUtils.obj2String(itemBaseInfoList)); // This method prints logs only at debug level
     
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("ItemBaseInfo Response: " + JSONUtils.obj2String(itemBaseInfoList));
    }
     
    // What's the difference with the if statement? Isn't it always the same?
    / / a
    Copy the code
  • ObjectMapper overhead ObjectMapper is a heavyweight object and is not allowed to be constructed at the method level.

  • For proper handling of database DATE (type DATE) fields as WHERE conditions, most of our tables index fields of type DATE. In order for us to use the index, we cannot use a function on this field.

    WHERE b.InDate > DATEADD(hh, -72, GETDATE())
    // The comparison is as follows
    WHERE DATEDIFF(hh, GETDATE(), b.InDate) < 72
     
    // The former can use the index
    Copy the code
  • Avoid using “double curly braces” to initialize objects. “Double curly braces” is actually a “speculative” notation that has never been officially or recommended by the Java community. The reason for this is that it generates an extra inner class with potentially unintended consequences and should be strictly avoided elsewhere except with the Jmockit framework. See hacpai.com/article/149…

  • Mapper. XML file of MyBatis is actually a kind of template engine, which supports many operations, including if, when, static method call, object comparison, expression operation, etc., these operations are performed by OGNL engine. It’s not nearly as efficient as Java code, so if possible, try to first compute the parameters in Java code and get them ready to pass to MyBatis.

  • The YML content writing problem raises many unrecognizable errors. Yml uses two Spaces instead of tabs to indent. To avoid errors, it is recommended to type space instead of Tab. For detailed introduction of YML, please refer to Ruan Yifeng’s article.

  • Avoid “script thinking” from Java code We were looking through code written by colleagues and found this way of returning a simple JSON object to the front end: Return New Object() {String name = “MKPL”}, this is not recommended, we should use concrete POJO Object instead, or use HashMap to return.

  • The alternative use of Java8 Stream gives us a huge efficiency boost (coding efficiency), but it does not apply to all scenarios. ForEach, for example, is not a substitute for a normal loop because it cannot use the break keyword. We recommend using Stream only for more complex operations.

  • Most of the time, we have to perform CAST or ISNULL operations on certain fields in the SELECT statement in order to fit the data type on the Java side. In some cases, these operations can be avoided. Depending on Mybatis compatibility for converting these types, we recommend not converting the types in SQL statements (for SQL efficiency), but in Java. As an example, MyBatis can convert INT and CHAR data to Boolean.