Mybatis, mybatis Official Website
Mybatis
Usermapper. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapper
PUBLIC "-// mybatis.org//DTD Mapper 3.0 // EN"
Http://mybatis.org/dtd/mybatis-3-mapper.dtd>
<Mapper namespace = "com. easytopit. demo. mybatis. mapper. UserMapper">
<Insert id = "insert" useGeneratedKeys = "true" keyProperty = "id">
Insert into et_user (username, truename, login_time) VALUES (# {username}, # {truename}, # {loginTime });
</Insert>
<Update id = "update">
UPDATE et_user SET username =#{ username}, truename =#{ truename}, login_time =#{ loginTime} WHERE id =#{ id}
</Update>
<Delete id = "delete" parameterType = "int">
Delete from et_user where id =#{ id}
</Delete>
<Select id = "queryAll" resultType = "com. easytopit. demo. mybatis. bean. User">
SELECT id, username, truename, login_time loginTime FROM et_user LIMIT 10
</Select>
<Select id = "queryAllByPage" parameterType = "java. util. HashMap" resultType = "com. easytopit. demo. mybatis. bean. User">
<! -- SELECT id, username, truename, login_time loginTime FROM et_user LIMIT # {param1}, # {param2} -->
SELECT id, username, truename, login_time loginTime FROM et_user LIMIT # {pageOffset}, # {pageSize}
</Select>
<Select id = "query" resultType = "com. easytopit. demo. mybatis. bean. User">
SELECT id, username, truename, login_time loginTime FROM et_user WHERE id =#{ id}
</Select>
</Mapper>
Namespace:
It is used for identifying SQL operations with the same name.
For example, if the access path is/add. do and the namespace is/user, the access path is/user/add. do.
Select id = "selectid"
Id is used to uniquely identify an SQL statement.
ResultType = java. util. map
Identifies the type of a row record
// Manual query (based on the namespace-The namespace is consistent with the Mapper Interface)-> com/easytopit/demo/mybatis/mapper/persistent/UserMapperNS. xml
Users = session. selectList ("NS. queryAll ");
Three methods of parameters
1: Use # {0} Index
2: start from 1 with parameter #{param1}
3: annotation @ param ("name ")
Jdk1.8 supports parameter annotations. Other unsupported parameter names are the same as those of param ("").
Notes
# {} Actually used jdbc? Placeholders are safe
11:41:56. 728 [http-bio-8080-exec-10] DEBUG c. e. c. m. AlbumMapper. deletelAlbum-=> Preparing: delete from AB _album where id =?
$ {} Is actually replaced by the underlying layer. It is unsafe to add single quotation marks.
11:47:29. 831 [http-bio-8080-exec-10] DEBUG c. e. c. m. AlbumMapper. deletelAlbum-=> Preparing: delete from AB _album where id = 67
Result set ing
Resulttype
Map a specific class
Resultmap
First define a resultmap and then map
Multi-to-one ing
It is equivalent to querying an attribute first, then automatically using the attribute as the parameter to call other methods, and then returning the result
Column ''deptno, property = 'dept', javaType = dept. class, one = @ one (select = "namespace. Method Name ")
One-to-multiple ing
Lazy Loading
Set
Load as needed
The cause is that there may be insufficient memory for one-to-multiple scenarios. Therefore, when necessary, re-initiate the SQL statement.
Insert selectkey label
First, use SQL syntax to query the maximum value + 1 of the current column, put it into map, and then insert into statement
<Insert id = "addRecord" parameterType = "map">
<! -- Get the primary key. The maximum value of the primary key is + 1, which is suitable for a single thread and is not recommended.
Then, set the primary key to key and put it in map.
-->
<SelectKey keyColumn = "mykey" keyProperty = "scoreNo" order = "BEFORE" resultType = "int">
Select max (s_id) + 1 as mykey from ex_score
</SelectKey>
Insert into ex_score (s_id, u_id, s_score, s_create_date, s_level) values (# {scoreNo}, # {userId}, # {userScore}, # {createTime }, # {level })
</Insert>
SQL and include labels
Encapsulate the same SQL statement into the SQL tag, and then use the include tag to call the SQL
Special symbols in SQL
Escape in xml
Lowercase & lt;
Less than or equal to & lt; =
Maven plug-in failure
C: \ Users \ Administrator \. m2 \ repository \ org \ apache \ maven \ plugins \ maven-resources-plugin
Delete
The plug-in is automatically updated in the case of the network. If the network fails in the middle, the problem will occur.
Dynamic SQL
Trim
Prefix = 'where' prefixoverrides = 'and'
If the prefix "and" exists, overwrite "and" into "where ".
Note that you must add and before each condition.
Trim to set
Trim prifix = 'set' prefixoverrides = ''suffix ='' suffixoverride = ','
It means to replace the leading space with set, and the trailing space with a space.
Set
Add set at the beginning and remove the comma at the end
Where
Purpose: if conditions are met, the where clause is automatically added and the and clause that meets the first condition is automatically removed.
Foreach
Most of the data used in loop in
Foreach collection = 'Property of the passed object 'close = ') item = ''variable 'open =' ('separator = 'separated by commas before each data'
We do not recommend that you use where1 = 1 because it will read all the tables and has performance problems.
Cache
Cache means that all the data that has been queried once is temporarily stored in the memory, and the second time is used to determine whether the memory exists. If the memory does not exist, the database will be read.
When the memory is insufficient, some data should be kicked out.
Level 1 Cache
It is a session-level cache. It must be the same as the session, and the retrieved cache data is the same.
Otherwise, you will go to the database every time you perform a query in the plug-in.
Effect: when data is operated in the same session, the second operation is directly cached, without the need to re-operate the database.
Check for incremental difference Modification
Level 2 Cache
Yessessionfactory level
Effect: use the same sesseionfactory. When the first sesseion data is saved to sesseion, the session is closed. If the second-level cache is enabled, the data is pushed to the second-level cache, next time you use the new sesseion, it will directly read from the second-level cache.
Enable Level 2 Cache
In mapper, use <cache> </cache>
Subtopic 4
Secondary cache is not recommended.
Equivalent to the previous global variable
Query Security
Algorithm
Fifo
Push the data at the rightmost end of the queue
Lfu: least frequently used
Play out with the least number of times
That is, the minimum number of times data is stored in the database and deleted.
LRU: least recently userd
The least recently used
The minimum number of times used within the specified time
Implement Cache
Each cached row of data must have a unique identifier (the primary key in the table), that is, map
If yes, It is retrieved from the map. If no, it is added.
Subtopic 1