MyBatis do not write 1 = 1 for multiple query conditions

When we encounter multiple query conditions, using where 1=1 can be very convenient to solve the problem, but it may cause a very large performance loss, because the database system can not use indexes and other query optimization strategies after adding “where 1=1” filtering conditions. The database system will be forced to scan each row of data (i.e., full table scan) to compare whether the row meets the filtering conditions. The query speed will be very slow when the amount of data in the table is large. There is also the risk of SQL injection. If you have any questions about learning Java (learning methods, learning efficiency, how to obtain employment), you can come to consult me at any time, this is my Java exchange learning group: 934623944 we communicate a lot of problems, help each other, there are good learning tutorials and development tools in the group. Note: Nuggets counter example:

Select count() from t_rule_BookInfo t where 1=1 AND title = #{title} AND author = #{author} Select count() from t_rule_BookInfo t title = #{title} AND author = #{author} UPDATE Iterating through keySet() is correct when the loop only needs to retrieve the Map’s primary key; However, when the primary key and value are needed, iterating through entrySet() is more efficient than iterating through keySet() and then iterating through get.

Example:

Counter example: HashMap<String, String> Map = new HashMap<>(); for (String key : map.keySet()){ String value = map.get(key); } is:

Example: HashMap<String, String> Map = new HashMap<>(); for (Map.Entry<String,String> entry : map.entrySet()){ String key = entry.getKey(); String value = entry.getValue(); }

There is no logical problem with using collection.size () to detect empty, but using collection.isEmpty () makes the code more readable and provides better performance; In addition, any implementation of collection.isempty () has a time complexity of O(1) and does not require multiple loops, but some implementations with collection.size () may have a time complexity of O(n).

Example:

LinkedList collection = new LinkedList<>(); If (collection.size() == 0){system.out.println (” collection is empty. “); } is:

LinkedList collection = new LinkedList<>(); If (collection.isEmpty()){system.out.println (” collection is empty. “); }

Collectionutils.isempty () if (collectionUtils.isempty (collection)){system.out.println (” collection is Null. “);

}

4. When initializing the collection, specify the size of the collection as much as possible, which can effectively reduce the number of expansion of the collection, because the time complexity of each expansion of the collection is likely to be O(N), which consumes time and performance.

Example:

Int [] arr = new int[]{1,2,3,4}; List list = new ArrayList<>(); for (int i : arr){ list.add(i); } is:

Int [] arr = new int[]{1,2,3,4}; List list = new ArrayList<>(arr.length); for (int i : arr){ list.add(i); }

Common string concatenation is optimized by Java at compile time, but concatenation of strings in loops cannot be optimized at compile time, so StringBuilder is used instead.

Example:

// Concatenate String in loop as an example String STR = “”; for (int i = 0; i < 10; I ++){// String concatenation in a loop is not optimized by Java. } is:

String str1 = “Love”; String str2 = “Courage”; String strConcat = str1 + str2; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; I ++){// In the loop, the Java compiler cannot optimize, so use StringBuilder sb. Append (I) manually; }

In the Java Collection library, the general time complexity of the contains method of List is O(n). If the code needs to call the CONTAINS method frequently to find data, the set list will be converted into a HashSet implementation first, and the time complexity of O(n) will be O(1).

Example:

List List = new ArrayList<>(); for (int i = 0; i <= Integer.MAX_VALUE; I ++){// Time complexity is O(n) if (list. Contains (I)) system.out.println (“list contains “+ I); } is:

List List = new ArrayList<>(); Set set = new HashSet<>(); for (int i = 0; i <= Integer.MAX_VALUE; If (set.contains(I)){system.out.println (“list contains “+ I); }}

Static member variables of a collection type should be assigned using a static code block rather than a collection implementation.

Example:

Private static Map<String, Integer> Map = new HashMap<String, Integer>(){{map.put(” Leo “,1); The map. The put (” Family – loving “, 2); Put (” Cold on the out side passionate on the inside “,3); }}; Private static List List = new ArrayList<>(){{list.add(” Sagittarius “); List. The add (” Charming “); List. The add (” Perfectionist “); }}; Is:

Private static Map<String, Integer> Map = new HashMap<String, Integer>(); The static {map. The put (” Leo “, 1); The map. The put (” Family – loving “, 2); Put (” Cold on the out side passionate on the inside “,3); }

private static List list = new ArrayList<>(); The static {list. Add (” Sagittarius “); List. The add (” Charming “); List. The add (” Perfectionist “); }

Delete unused local variables, method parameters, private methods, fields, and extra parentheses.

If you have any questions about learning Java (learning methods, learning efficiency, how to obtain employment), you can come to consult me at any time, this is my Java exchange learning group: 934623944 we communicate a lot of problems, help each other, there are good learning tutorials and development tools in the group. The utility class is a collection of static fields and functions that should not be instantiated. However, Java adds an implicit public constructor for every class that does not explicitly define a constructor. To avoid unnecessary instantiation, you should explicitly define a private constructor to mask this implicit public constructor.

Example:

Public Class PasswordUtils {// Negative example of the utility constructor private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.

Public static final String DEFAULT_CRYPT_ALGO = “PBEWithMD5AndDES”;

public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); } is:

Public Class PasswordUtils {// Public static final Logger LOG = LoggerFactory.getLogger(Passwordutils.class);

// Define a private constructor to mask the implicit public constructor private PasswordUtils(){}

Public static final String DEFAULT_CRYPT_ALGO = “PBEWithMD5AndDES”;

public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }

After catching an exception, if nothing is done, the exception is simply thrown again, which is the same as if the exception was not caught. You can either delete the block of code or add some other handling.

Example:

Private static String fileReader(String fileName)throws IOException{private static String fileReader(String fileName)throws IOException{

try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) ! = null) { builder.append(line); } return builder.toString(); } catch (Exception e) {// throw e; }

Is:

Private static String fileReader(String fileName)throws IOException{

try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) ! = null) { builder.append(line); } return builder.toString(); // remove extra exceptions or add other handlers: /catch (Exception e) {return “fileReader Exception “; } /

}

Use String.valueof (value) instead of “” +value when converting other objects or types to strings, it is more efficient to use String.valueof (value) than” “+value.

Example:

// To convert other objects or types to strings: int num = 520; // “” + value String strLove =” “+ num; Is:

// Convert other objects or types to strings: int num = 520; String strLove = string.valueof (num); String strLove = string.valueof (num);

Avoid Using BigDecimal(Double) BigDecimal(Double) carries the risk of loss of accuracy, which can cause business logic exceptions in scenarios where exact calculations or value comparisons are performed.

Example:

// BigDecimal BigDecimal = new BigDecimal(0.11d); Is:

BigDecimal bigDecimal1 = Bigdecimal.valueof (0.11d); // BigDecimal bigDecimal1 = Bigdecimal.valueof (0.11d);

If the program returns NULL, the caller must enforce null detection. Otherwise, a null pointer exception will be thrown. Return an empty array or collection, effectively preventing the caller from throwing a null-pointer exception because null is not detected, and making the code cleaner by removing null-detecting statements.

Example:

Public static Result[] getResults() {return null; }

public static List getResultList() { return null; }

public static Map<String, Result> getResultMap() { return null; } is:

Public static Result[] getResults() {return new Result[0]; }

public static List getResultList() { return Collections.emptyList(); }

public static Map<String, Result> getResultMap() { return Collections.emptyMap(); }

Call equals using constants or determined values instead of null pointer exceptions.

Example:

Private static Boolean fileReader(String fileName)throws IOException{

Return filename.equals (” Charming “); } is:

Private static Boolean fileReader(String fileName)throws IOException{

Return “Charming”. Equals (fileName);

// Use the: java.util.objects.equals () method

Return Objects. Equals (” Charming “, the fileName); }

Enumerations are usually used as constants. If there are public property fields in an enumeration or a set field method, the properties of these enumerated constants can be easily modified. Ideally, property fields in enumerations are private and assigned in private constructors, with no corresponding Setter methods, preferably with a final modifier.

Example:

Public enum SwitchStatus {// Enumeration property field example DISABLED(0, Disable), ENABLED(1, Enable);

public int value; private String description;

private SwitchStatus(int value, String description) { this.value = value; this.description = description; }

public String getDescription() { return description; }

public void setDescription(String description) { this.description = description; }

Is:

Public enum SwitchStatus {// Enumeration property field examples DISABLED(0, Disable), ENABLED(1, Enable);

// final private final int value; private final String description;

private SwitchStatus(int value, String description) { this.value = value; this.description = description; }

Public int getValue() {return value; }

public String getDescription() { return description; }

16, tring. Split (String regex) need to translate some key words using a String String plit method, the incoming delimited String is a regular expression, are part of the keyword (such as. | etc) need to be escaped.

Example:

// string.split (String regex) example String[] split = “a.a.a.bc “. Split (“.”); System.out.println(Arrays.toString(split)); // Result is []

String [] split1 = “a | ab | ABC.” the split (” | “); System.out.println(Arrays.toString(split1)); / / the results for “a”, “|”, “a”, “b”, “|”, “a”, “b”, “c”] are:

// String. Split (String regex) String[] split2 = “A.A.B.A.B. “. Split (“.”); System.out.println(Arrays.toString(split2)); // Result is [” a “, “ab”, “ABC”]

/ / | need to translate the String [] split3 = “a | ab | ABC.” the split (” | “); System.out.println(Arrays.toString(split3)); // The result is [” A “, “ab”, “ABC”] if you have any questions about learning Java (learning method, learning efficiency, how to get employment), please feel free to consult me. This is my Java exchange learning group: 934623944 we exchange a lot of problems, help each other, there are good learning tutorials and development tools in the group. Notes: Nuggets