1, #{} and ${} are different:
#{} represents a placeholder symbol, #{} receives input parameters, type can be simple type, Pojo, HashMap.
If you receive a simple type, you can write value or other name in #{}.
#{} receives the Pojo object value, through the OGNL to read the property values in the object, through the properties. Properties ... Gets the object property value in the same way.
${} represents a concatenation symbol that references SQL injection, so it is not recommended to use ${}.
${} receives input parameters, type can be simple type, Pojo, HashMap.
If you receive a simple type, you can only write value in ${}.
${} receives the Pojo object value, through the OGNL to read the property values in the object, through the properties. Properties ... Gets the object property value in the same way.
2. The difference between SelectOne and selectlist:
SelectOne indicates that a record is queried for mapping. If using SelectOne can be implemented using SelectList can also be implemented (only one object in list).
SelectList indicates that a list (multiple records) is queried for mapping. If you use SelectList to query multiple records, you cannot use SelectOne.
If you use SelectOne error:
Org.apache.ibatis.exceptions.TooManyResultsException:
Expected one result (or null) to being returned by SelectOne (), but Found:4
3. Specify the type of input parameters in the mapping file by ParameterType.
4. Specify the type of output result by Resulttype in the mapping file.
5, Resulttype:
Using the Resulttype implementation is simple, if the Pojo does not include the queried column names,
You need to add a property for the column name to complete the mapping.
It is recommended to use Resulttype if there is no special requirement for query results.
6, Resultmap:
Resultmap need to be defined separately, the implementation is a bit cumbersome, if the query results have special requirements,
You can use Resultmap to complete the properties of Pojo the associated query map.
Resultmap can implement lazy loading, resulttype cannot implement lazy loading.
6.1, Resultmap:
Use Association and collection to complete a one-to-many advanced mapping (special mapping requirements for results).
Association:
Function: Maps The associated query information to a Pojo object.
Occasions: In order to facilitate query of association information, you can use association to map the associated order information to the Pojo attribute of the user object.
For example: query orders and associated user information.
The query results cannot be mapped to the Pojo property of the Pojo object using Resulttype, depending on whether you want to use Resulttype or resultmap for the result set query traversal
Collection
Function: Maps The associated query information to a list collection.
Occasions: To facilitate query traversal of association information, you can use collection to map association information to the list collection.
For example: Query the User Rights Range module and the menu under the module,
You can use collection to map a module to a module list and map a menu list to a menu of Module objects in the List property.
The purpose of this is also to facilitate the query result set traversal query. If you use Resulttype, the query results cannot be mapped to the list collection.
7, Friendship Links
MyBatis Introductory Example
MyBatis Official website
MyBatis's Mapper.xml file