MyBatis needs some attention to the problem

Questions 1. Use mybatis secondary cache when the need to pay attention

The amount of data stored in the secondary cache is not too large. Secondary cache is based on namespace namespace when simultaneously operating a plurality of the same table, the secondary cache is not recommended. Because when a namespace for the data in the table were update, insert or delete will not refresh the cache of another namespace. At this time select another name with a space or queries to cache data.

The difference 2.resultType and resultMap

In MyBatis reference in the document (version 3.5.1) is described

Personal understanding and summary

resultType

When we query the database from a result that can be matched with objects in Java when using resultType. For example, the amount of data queries at this time resultType = "Integer", check out a person's name, then resultType = "String"

resultMap

When we check out the results could not be found corresponding to the type of match, we can use the results of the query resultMap packaging. Example: a person's name and salary are stored in two tables, name, salary we can customize resultMap

<resultMap id="自定义" type="查询时关联的主表">
   <result .... />
   ...
</resultMap>

The difference between the association and 3.collection

collection

Many, such as the relationship between teachers and students is one to many. When we define MyBatis from the resulting set of database query, we can use the corresponding collection resultMap package label makes it possible to be able to correspond with each other and java object.

For example-many relationship between teachers and students can use to traverse the collection, packaging the corresponding resultMap.

In a batch delete, update, insert, when you can also use the label collection to traverse.

association

Similarly, when we met one on one situation you can use the association tag wraps the result set

PreparedStatement and the difference 4.Statement

Statement object for sending SQL statements to be executed in the database, the database each time the compiler execute SQL statements.
PreparedStatement will be executed SQL statements precompiled, better safety and efficiency.

5. The MyBatis Mapper.xml profile difference {#} ¥ ​​{} and the?

# {} Will parse the incoming data into a string of data plus a double quotation mark, # {} manner can be prevented to a large extent implantation sql

$ {} Then the incoming data is displayed directly sql generated in {} $ sql injection can not be prevented, need to be cautious

6.

Published 14 original articles · won praise 2 · Views 1029

Guess you like

Origin blog.csdn.net/GoNewWay/article/details/103994251