MyBatis03 - Mapper XML mapping file

Preface:

In the last article we introduced the MyBatis XML mapping configuration file, configuration.xml. In fact, Mybatis true power lies in its mapped statement, but also its magic lies. Mybatis is aimed to do better than the conventional method of constructing SQL, xml file mapping is also significant is relatively simple.


SQL mapping files There are several top-level elements:

  • cache - given namespace cache configuration.
  • cache-ref - references to other namespaces cache configuration.
  • resultMap - is the most complex and powerful element that describes how to load from the database result object.
  • sql - reusable statement block can be referenced by other statements.
  • insert - Mapping insert statements
  • update - mapped UPDATE statement.
  • delete - Mapping delete statement
  • select - map query

The most common, understandable, that is, insert, delete, update, select, respectively corresponding to add, delete, change, thing.


select:

MyBatis statement is one of the most commonly used elements, most applications, queries to cumbersome than the modification for each insert, update, or delete operation, generally correspond to multiple queries. This is one of the basic principles of MyBatis, and is the focus and effort into the cause of the query and result mapping.

1, a simple query:

<select id="selectBlogById" parameterType="int" resultType="Blog">
    <![CDATA[
        select * from blog where blogId = #{blogId}
    ]]>
</select>

This statement is called selectBlogById, a receiving int (or Integer) type parameter and returns the object type of a Blog, where the key is the column name, the corresponding value is the value of the result row.

Use parameters when using the "#", there is another symbol "$" can also refer to the parameters, use the "#" The most important role is to prevent SQL injection.

2, select element has more attributes that allow you to configure, to determine the details of how each statement.

<select
  id="selectPerson"
  parameterType="int"
  parameterMap="deprecated"
  resultType="hashmap"
  resultMap="personResultMap"
  flushCache="false"
  useCache="true"
  timeout="10000"
  fetchSize="256"
  statementType="PREPARED"
  resultSetType="FORWARD_ONLY">

Below explain the role of these attributes:

  • In id-- namespace unique identifier that can be used to reference this statement.
  • parameterType-- parameter type, if the dynamic SQL statement parameter is only one, this property dispensable
  • resultType-- result type, note that if the result is a collection of type collection should be included, rather than the collection itself. Use resultType or resultMap, but not both.
  • resultMap-- external resultMap named reference. MyBatis mapping result set is the most powerful feature, because they have a good understanding of it, in many cases complex mappings can be solved. Use resultMap or resultType, but not both.
  • flushCache-- it is set to true, any time as long as the statement is called, will result in the local cache and secondary cache will be cleared, the default value: false.
  • useCache-- it is set to true, the result will lead to this statement to be secondary cache, default: to select the element to true.
  • This setting is timeout-- before throwing an exception, the database returns the number of seconds the driver will wait for results of the request. The default is unset (dependent on drive).
  • fetchSize-- This is an attempt to influence the results of each batch driver returned an equal number of rows and the set value. The default is unset (dependent on drive).
  • statementType - STATEMENT, PREPARED or a CALLABLE of. This will make MyBatis were used Statement, PreparedStatement or CallableStatement, default value: PREPARED.
  • resultSetType - a FORWARD_ONLY, SCROLL_SENSITIVE SCROLL_INSENSITIVE or the default value is unset (dependent on drive).

insert:

<insert id="insertAuthor">
  insert into Author (id,username,password,email,bio)
  values (#{id},#{username},#{password},#{email},#{bio})
</insert>

Insert statements, related to the issue of a primary key:

If your database supports auto-generated primary key fields (such as MySQL and SQL Server), then you can set useGeneratedKeys = "true", then put keyProperty set to the target property on OK. For example, if the Author table column type have been used for the automatic generation of the id, the sentence may be modified as follows:

<insert id="insertAuthor" useGeneratedKeys="true"
    keyProperty="id">
  insert into Author (username,password,email,bio)
  values (#{username},#{password},#{email},#{bio})
</insert>

update:

<update id="updateAuthor">
  update Author set
    username = #{username},
    password = #{password},
    email = #{email},
    bio = #{bio}
  where id = #{id}
</update>

delete:

<delete id="deleteAuthor">
  delete from Author where id = #{id}
</delete>

SQL:

This element can be used to define a reusable SQL code segment may be included in other statements. It may be statically (loading parameter) parameterization. Varied by different attribute values ​​included in the examples ..

<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password
</sql>

This sql above fragments may be contained in other statements,

<select id="selectUsers" resultType="map">
  select
    <include refid="userColumns"><property name="alias" value="t1"/></include>,
    <include refid="userColumns"><property name="alias" value="t2"/></include>
  from some_table t1
    cross join some_table t2
</select>

ResultMap:

MyBatis resultMap element is the most important and powerful elements. It is to keep you away from 90% of the need to remove that thing JDBC code data from the result set, and allows you to do things that JDBC does not support in some cases. In fact, the preparation is similar to the joint statement of the complex mapping of these equivalent code, maybe thousands of lines of code that can be crossed. ResultMap design is simple and does not require a clear statement of the results of the mapping, and more complex statements really need to describe their relationship.

supplement:

difference resultType and the resultMap:
MyBatis in the query select mapping, return type can be used resultType, can also be used resultMap, resultType is a direct representation of the return type, and resultMap is a reference to an external ResultMap, but resultType not with resultMap simultaneously exist. When a query is mapped in MyBatis, in fact, check out each attribute is placed inside a corresponding Map, where the key is the attribute name, the value is the corresponding value.

  • When the type attribute is provided to return resultType, MyBatis Map will be taken inside the key-value pair is assigned to the specified target resultType corresponding attribute. So in fact MyBatis query mapping for each type of return is ResultMap, only when the type attribute is provided resultType return time, automatic MyBatis to assign the value corresponding to the specified attribute object resultType.
  • When the return type is provided resultMap time, because the Map is not well represent the domain model, we need to put yourself further into its corresponding object, which is often useful role in complex queries.

(ResultMap being the first not described in detail, it will be showing its powerful features in future articles by specific cases.)

发布了128 篇原创文章 · 获赞 239 · 访问量 33万+

Guess you like

Origin blog.csdn.net/HLK_1135/article/details/61932618
Recommended