MyBatis really powerful in mapping statements, focus on SQL, powerful, SQL map configuration is quite simple
So let's take a look at the specific structure of the mapping file
First, the XML node structure
Mapper as root node-namespace namespace
Cache-Configure caching for a given namespace
cache-ref– referencing cache configuration from other namespaces
Resultmap– used to describe the correspondence between a database result set and an object
sql– SQL blocks that can be reused, or can be referenced by other statements
insert– Mapping INSERT statement
update– Map UPDATE statement
delete– Map DELETE Statements
select– Mapping Query statements
Second, the role of each node
MyBatis Learning Archive (1)--the reverse-generated usersmapper.xml and Usersmapper.java in the introductory case
<?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 "><Mappernamespace= "Com.mapper.UsersMapper" > <ResultmapID= "Baseresultmap"type= "Com.pojo.Users" > <IDcolumn= "id" Property= "id"Jdbctype= "INTEGER" /> <resultcolumn= "Name" Property= "Name"Jdbctype= "VARCHAR" /> <resultcolumn= "Password" Property= "Password"Jdbctype= "VARCHAR" /> </Resultmap> <SQLID= "Base_column_list" >ID, name, password</SQL> <SelectID= "Selectbyprimarykey"Resultmap= "Baseresultmap"ParameterType= "Java.lang.Integer" >Select<includerefID= "Base_column_list" />From users where id = #{id,jdbctype=integer}</Select> <DeleteID= "Deletebyprimarykey"ParameterType= "Java.lang.Integer" >Delete from users where id = #{id,jdbctype=integer}</Delete> <InsertID= "Insert"ParameterType= "Com.pojo.Users" >INSERT into users (ID, name, password) VALUES (#{id,jdbctype=integer}, #{name,jdbctype=varchar}, #{passwo Rd,jdbctype=varchar})</Insert> <InsertID= "Insertselective"ParameterType= "Com.pojo.Users" >INSERT INTO Users<Trimprefix="("suffix=")"Suffixoverrides="," > <ifTest= "id! = NULL" >ID,</if> <ifTest= "name = NULL" >name,</if> <ifTest= "Password! = null" >Password,</if> </Trim> <Trimprefix= "Values" ("suffix=")"Suffixoverrides="," > <ifTest= "id! = NULL" >#{id,jdbctype=integer},</if> <ifTest= "name = NULL" >#{name,jdbctype=varchar},</if> <ifTest= "Password! = null" >#{password,jdbctype=varchar},</if> </Trim> </Insert> <UpdateID= "Updatebyprimarykeyselective"ParameterType= "Com.pojo.Users" >Update Users<Set> <ifTest= "name = NULL" >name = #{name,jdbctype=varchar},</if> <ifTest= "Password! = null" >password = #{password,jdbctype=varchar},</if> </Set>WHERE id = #{id,jdbctype=integer}</Update> <UpdateID= "Updatebyprimarykey"ParameterType= "Com.pojo.Users" >Update users set name = #{name,jdbctype=varchar}, password = #{password,jdbctype=varchar} where id = #{ Id,jdbctype=integer}</Update></Mapper>
Usersmapper.xml
Package Com.mapper; Import com.pojo.Users; Public Interface usersmapper { int deletebyprimarykey (Integer ID); int Insert (Users record); int insertselective (Users record); Users Selectbyprimarykey (Integer ID); int updatebyprimarykeyselective (Users record); int Updatebyprimarykey (Users record);}
Usersmapper.java
2.1 Mapper Node
As the root node of XML, it has a very important property--namespace (namespace)
The value of namespace should be consistent with the mapping interface to which it is bound, to ensure its uniqueness, typically "package name + class name", such as Com.mapper.UsersMapper, which is bound to an interface named Usersmapper under the Com.mapper package
The method in the interface usersmapper with which it is bound should correspond to the SQL statement ID one by one in the mapping file (the order does not need to be consistent)
2.2 Cache, Cache-ref node
It is not recommended to use MyBatis's own cache, so there is no detail here
2.3 Resultmap Node
Can be referenced in SQL mapping statements, typically used to represent mappings between database tables and entity classes
The Resultmap node has 2 properties:
ID: Identity function, should be unique
Type: The corresponding entity class, and if an alias is set in Mybatis-config.xml, the alias of the setting can also be used
ID node: The primary key in the corresponding database
Result node: Other fields of the non-primary key
Column: The corresponding field name in the database table
Property: The corresponding attribute in the entity class
Jdbctype: Types of fields changed in the database
2.4 SQL Node
A very simple node, used for the reuse of SQL statements, whose ID attributes serve as identifiers and should be unique
2.5 Select Node
Select is one of the most commonly used elements in MyBatis, corresponding to the SELECT statement
ID: A unique identifier in the namespace that corresponds to the method in the interface to the SQL statement ID one by one in the mapping file
ParameterType: The parameter type of the incoming SQL statement is divided into 2 cases:
1. Underlying data types
int, String, date, and so on
You can only pass in one, by #{parameter name} to get the value passed in
2. Complex data types
Java entity classes, maps, etc.
Incoming value can be obtained by #{property name} or #{map KeyName}
Resulttype:sql statement returns the full class name or alias of a value type
Resultmap: Referencing the configured Resultmap
2.6 Insert, Delete, update node
Add, delete, change statements, respectively, properties are basically the same as SELECT, except that you don't need to configure Resulttype or Resultmap, which by default returns the number of rows affected
Therefore, for additions and deletions to this kind of update operation, the return value of the interface method is proposed as an int type, the number of rows to execute SQL effect, preferably do not write the Boolean type
Three, multi-parameter incoming
As stated in 2.5, MyBatis only supports the passing of a single parameter, so how do I pass multiple arguments?
There are 2 types of methods:
1. Encapsulate multiple parameters into a map, but the method is opaque and cannot directly see what the required parameters are.
or pass directly to an entity class object
2. Use annotation @param to pass in multiple parameters
Note the incoming parameter in the interface @param ("value"), use ${value} in the mapping file to get the parameter
Recommendation: It is usually best to encapsulate an object instead of passing in a parameter that is more than 3.
MyBatis Learning Archive (3)--mapper.xml mapping file