Detailed mapping file label Mybatis

1. The input mapping
ParameterType: Specifies the type of input parameters java, you can use the alias or the fully qualified name of the class. It may receive a simple type, POJO, HashMap.
2. output mapping
** resultType: ** When using resultType mapping results, the column name and attribute name POJO mapping query exactly the same, the column can be mapped to succeed.
If the query column names and attribute names pojo map of all inconsistent, it does not create a POJO objects.
If the query column names and attribute names POJO mapping of a consistent, POJO objects will be created.
When the object and outputs a single pojo pojo list (pojo accommodating an object), mapper mapping file is the same type resultType, mapper interface method return values are different.
When the object and outputs a single pojo pojo list (bloom pojo object), mapper maps the same file, and returns a single object list of objects, when an interface mapper to generate dynamic proxy, according to the type of the return value, the method determines or call selectOne selectList method.

3. resultMap output mapping
when resultMap output mapping column names the query and the corresponding POJO attributes need not be the same, only need to define a resultMap in mapper mapping file, the column name and attribute name to do a mapping relationship can be, but this is the column name and the property name must be correct.
Defined resultMap:

<!--
    [id]:定义resultMap的唯一标识

    [type]:定义该resultMap最终映射的pojo对象

    [id标签]:映射结果集的唯一标识列,如果是多个字段联合唯一,则定义多个id标签

    [result标签]:映射结果集的普通列

    [column]:SQL查询的列名,如果列有别名,则该处填写别名

    [property]:pojo对象的属性名
-->
<resultMap type="user"id="userResultMap">

    <id column="id_" property="id"/>

    <result column="username_" property="username"/>

    <result column="sex_" property="sex"/>

</resultMap>

4. SQL fragments
Mybatis provides the functionality of SQL fragments, can improve the reusability of SQL.

SQL fragments defined
using SQL sql tags to define a segment

<!-- 定义SQL片段 -->
<!--
    [sql标签]:定义一个SQL片段
    [id]:SQL片段的唯一标识
    建议:
       1、SQL片段中的内容最好是以单表来定义
       2、如果是查询字段,则不要写上SELECT
       3、如果是条件语句,则不要写上WHERE
 -->
<sql id="selectUserSql">
    id,username,sex,birthday,address
</sql>

SQL quoted fragment
use to refer to the SQL fragments:

<!-- 根据用户id来查询用户信息(使用SQL片段) -->
<!--
    [include标签]:引用已经定义好的SQL片段
    [refid]:引用的SQL片段id
-->
<select id="findUserByIdSql"parameterType="int" resultType="user">
    SELECT <include refid="selectUserSql"/> FROM USER WHEREid=#{id}
</select>

5. Dynamic SQL
Dynamic labels: if, where, foreach, set the like.

If the tag parameter used as the judgment, if eligible, if the label put on the SQL splice body.
Note: judge whether the air, not only to judge null, but also determines the empty string '' is used if;
example:

<!--
    [if标签]:进行动态判断,如果成功则把if标签内的内容拼接到原有SQL上
    [test]:填写判断表达式
-->
<select id="findUser4If"parameterType="user" resultType="user">
    SELECT * FROM USER WHERE sex=#{sex}
    <if test="username != null and username != ''">
       AND username LIKE '%${username}%'
    </if>
</select>

WHERE
WHERE tag can automatically process the first and behind it

<!-- 根据查询条件来查询用户信息,查询条件可能会有性别或者用户名称,有可能什么没有。(学习where标签) -->
<!--
    通过动态SQL可以看出,为了保证SQL语句的正确性,需要在where 后面添加没有任何意义的1=1
    [where标签]:使用它可以去掉它后面的第一个and,这样就可以保证SQL的正确性了
-->
<select id="findUser4Where"parameterType="user" resultType="user">
    SELECT * FROM USER
    <where>
       <if test="sex != nulland sex != ''">
            AND sex=#{sex}
       </if>
       <if test="username !=null and username != ''">
           AND username LIKE '%${username}%'
       </if>
    </where>     
</select>

Foreach
when passing an array or List to sql, mybatis using foreach array of analytical parameters and spliced to SQL.

<!--
    [foreach标签]:表示一个foreach循环
    [collection]:集合参数的名称,如果是直接传入集合参数,则该处只能填写[list]。
    [item]:定义遍历集合之后的参数名称
    [open]:开始遍历之前需要拼接的SQL串
    [close]:结束遍历之后需要拼接的SQL串
    [separator]:遍历出的每个对象之间需要拼接的字符
 -->
<select id="findUsersByIdListForEach"parameterType="list" resultType="user">
    SELECT * FROM USER
    <where>
       <if test="list != nulland list.size > 0">
           <foreach collection="list"item="id" open="AND id IN(" close=")"separator=",">
              #{id}
           </foreach>
       </if>
    </where>
</select>

Set
the label where similar use, it will remove the tag SET after the first comma,

Guess you like

Origin blog.csdn.net/qq_43378945/article/details/93716911