Mybatis Note the use of (1)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq125281823/article/details/84935878

First, the difference in mybatis # and $ symbols

​ 1)#{}

Use # {} implies the use of pre-compiled statement, that preparedStatement in the use of jdbc, sql statement will be used if there is a parameter? As a placeholder, we know that the way to prevent sql injection, and the use of sql statement formed when # {}, has been enclosed in quotation marks .

Embodiment, select * from table1 where id = # {id} when calling this statement we can see through the background printed sql to: select * from table1 where id = '2' is added to the transmission 2. That is when the composition of the sql statement parameter defaults to the string.

​ 2)${}

Use Time of s q l Do not meeting when do word symbol s e l e c t f r o m t a b l e 1 w h e r e i d = {Sql **} when not treated as a string **, what it is, such as the top of the statement: select * from table1 where id = {ID} When calling the statement printed to the console: the parameter select * from table1 where id = 2assuming a value of 2 pass

It can be seen from the difference between the two ways described upper side, we preferably use # {} then use it because it is possible to prevent sql injection, and pre-compiled.

Use $ {} is output only when required, such as:

select * from $ {tableName} order by $ {id} It should pass the table name and column sorting in accordance with which, added to the incoming table1, id, the statement is: select * from table1 order by id

If you are using # {} it becomes a select * from 'table1' order by 'id' We know this is not right.

Also, when using the following configurations must be used # {}

<select id="selectMessageByIdI" parameterType="int" resultType="Message">
    select * from message where id=#{id};
</select>
<!--在parameterType是int时,sql语句中必须是#{}。-->

Second, the difference mybatis with jdbc

##### 1)定义

​ jdbc:

java database connection, the API is a set of interface specification java provided for connection to the database, the database is the bottom of the operation, wherein the predetermined type and database type java conversion

Since this is a set of standardized interfaces and classes, so each database vendor can have its own implementation.

​ mybatis:

Support is a common sql queries, stored procedures and advanced mapping into a lightweight persistence framework

Jdbc is encapsulation of the underlying or jdbc

2) Comparison

mybatis virtually eliminates all manual settings jdbc complex code and parameters and the manual packaging of the result set.

Xml configuration may interface with the database and record each pojo mapping

Usually accesses to access the database through a connection pool, abstract datasorce to decouple a particular connection pool implementations

Simple result set mapping

Sql simple configuration and dynamic complex logic

3) Persistence Layer

Persistence: saving data to a storage device power-down

Persistence Layer: or to store data from the one or more acquired data in memory or a class components

This layer is often used to package objects into a database table columns, or domain model, it is a quick and simple solution to the storage device from the frame memory object mapping transformation is a very good frame, the persistence framework are It must include conversion operations

Data storage and conversion of objects in memory involves a problem: The object becomes the data memory from memory, the process is irreversible, and how to restore the data memory to the object in memory, you can not convert well, so there was the issue of sequence

Guess you like

Origin blog.csdn.net/qq125281823/article/details/84935878