Some summary of MyBatis

1. The difference between ${} and #{}

${} is a variable placeholder in the properties file, which can be used for label attribute values ​​and inside SQL, and belongs to static text replacement. For example, ${drive} will be statically replaced with com.mysql.jdbc.Drive

#{} is the parameter placeholder of sql, mybatis will replace # in sql with ? , when the sql is executed, the parameter setting method of the paeparedStatement will be used, and the sql will be given in order? The placeholder sets the parameter value.

#{} can prevent SQL injection to the greatest extent. ${} does not prevent SQL injection.

mybatis sort order by with ${}. It is generally used to pass in database objects, such as table names and field names.

#{} Treat the incoming parameters as strings, and add "".

${} will directly display and generate the incoming parameters into SQL.

example:

select * from table_name order by id.

If you use #{}, the SQL becomes select * from table_name order by "id";

If you use ${}, the SQL is select * from table_name order by id.


2. What are the common tags in the xml mapping file?

<mapper>: The root tag of each map file, focusing on the namespace attribute of <mapper>

Tags for writing CRUD statements: <select><insert><update><delete>

动态sql标签:trim|where|set|foreach|if|choose|when|otherwise

sql tag: <sql>  reusable SQL statement that can be referenced by other statements

<resultMap> is used to solve the situation where the returned result is multiple tables, define a map

Attributes:

id: represents the unique identifier of the tag

parameterType: input parameter type

resultType: return result type, usually an entity class

resultMap: The return result type is a map, used with <resultMap>

3. In the mybatis xml mapping file, can the ids of different xml mapping files be repeated?

A: If the namespace is configured, the id can be repeated. If the namespace is not configured, the id cannot be repeated. namespace+id is used as a map key.

4. Mybatis first level cache and second level cache

 Answer: (1) Level 1 cache

1. The first-level cache of mybatis refers to sqlsession, the scope is sqlsession, and mybatis opens the first-level cache by default.

2. In the same sqlsession, execute the same query SQL, the first time it will query the database, and put the result in the cache, the second time it will be taken directly from the cache, if there are additions, deletions and changes between the two SQL operations , the sqlsession cache is cleared.

(2) Level 2 cache

1. The secondary cache of mybatis refers to the mapper mapping file. The scope of the secondary cache is the content in the mapper mapping file under the same namespace. It is shared by multiple SQL sessions, and the secondary cache needs to be manually opened.

2. In the mapper file under the same namespace, execute the same SQL, go back to query the database for the first time, and write it into the cache, and directly fetch it from the cache the second time, if there are additions, deletions, and changes between the two SQL operations , the L2 cache is cleared.


5. How does mybatis perform paging? What is the principle?

   Answer: mybatis uses the RowBounds object for paging, which is memory paging performed for the ResultSet result set, not physical paging. If you want physical paging, you can directly write parameters with physical paging in SQL to complete physical paging, or you can use a paging plug-in to complete physical paging.

      The principle of the paging plug-in is to use the plug-in interface provided by mybatis to implement a custom plug-in, intercept the SQL to be executed in the plug-in interception method, then rewrite the SQL, and add the corresponding physical paging statement and physical paging parameters according to the dialect.

6. Does mybatis support lazy loading? What is the principle?

    Answer: mybatis only supports one-to-one and one-to-many queries to support lazy loading. In the configuration file, you can configure whether to enable lazy loading, lazyLoadingEnabled=true|false.

           Its principle is to use CGLIB to create a proxy object of the target object. When calling a method, enter the interceptor method, such as a.getB().getName(). The interceptor method finds that a.getB() is null, and then queries first The query associated with the B object, query B, the B object has a value, and then complete a.getB().getName(), which is the principle of lazy loading.

7. The problem of mybatis judging whether it is equal

When MyBatis judges that the condition is equal, the constant needs  to be converted by adding .toString() . This method is stable and recommended, such as:

  1. <!-- correct, stable, recommended -->  
  2. <iftest="newsImage != null and newsImage == '1'.toString()">   
  3.     <![CDATA[ and len(newsImage) > 0 ]]>  
  4. </if> 




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819956&siteId=291194637