1.SQL map file (..... Mapper.xml)
Several top-level elements of the SQL mapping file (in order of definition)
mapper-only one property namespace (namespace)
Used to differentiate between different mapper, globally unique
Binding DAO interfaces, and interface-oriented programming
cache-Configuring the cache for a given namespace
cache-ref-applying cache configuration from other namespaces
Resultmap-used to describe the correspondence between a database result set and an object
Sql-can be reused for SQL blocks or referenced by other statements
insert-Mapping INSERT statement
update-Map UPDATE statement
delete-Map DELETE Statements
select-Mapping Query statements
mappings for the return result type of a select of a 2.SQL map file can use Resultmap and resulttype, but cannot be used at the same time.
Resulttype directly indicates that the return type includes the underlying data type and the complex data type.
Resultmap is a reference to an external RESULTMAP definition, an ID to an external resultmap that represents the Resultmap on which the returned results are mapped
Its application scenario is that database field information is inconsistent with object properties or complex joint queries are required to freely control the mapping results.
3. For MyBatis SQL statement parameter parameters, use @param annotations for parameter data of the underlying data type, and parameters for complex data types directly into the parameter.
The Association and collection of 4.RESULTMAP enable advanced result mapping.
The Association properties are:
- Javatype: Full Java class name or alias. If mapped to a javabean, the MyBatis usually detects its own type;
If you map to a hashmap, you should explicitly specify javatype to ensure the desired behavior.
- Property: The attribute of the entity object that maps the database column.
The child elements of the association are:
Property: The attribute of the entity object that maps the database column.
Column: Database columns name or alias
The properties of the collection are:
- OfType: The full Java class name or alias, and the type that the collection contains.
- Property: The attribute of the entity object that maps the database column.
5.MyBatis Cache
- First-level cache: is based on the Perpetualcache (mybatis) HashMap local cache, scoped to the session field, but after the session flush or close, all the caches in the session are emptied.
- Secondary cache: Is the global caching, it is outside the session range, can be shared by all sqlsession, open it only need to set in MyBatis core configuration file (Mybatis-config.xml) settings
- The first-level cache caches the SQL statements, and the two-level cache caches the result objects.
SQL mapping File