一、简述

1️⃣config 文件常用标签
properties 标签:引入外部 properties 文件资源。
settings 标签:设置 mybatis 全局行为。
typeAlias 标签:减少 mapper 文件配置,给模型类起别名。
transactionManager 标签:配置 mybatis 的事务行为(JDBC|MANAGED)
dataSource 标签:配置 mybatis 数据源(POOLED|UNPOOLED|JNDI)
mappers 标签:引入Mapper文件或接口(url|resources|class|package)
2️⃣Mapper 文件常用标签
a. resultMap 标签:对查询的结果进行封装处理
简单 pojo 类型处理,只需处理表中字段和对像中属性映射处理。
包装 pojo 类型处理,使用 Collection 处理包装集合类型的对像,association 处理包含单个 pojo 对像。
Collection 处理的两种方式:
1.连接查询 join
2.通过多条 select 查询的方式。
association 处理两种方式:
1.连接查询 join
2.通过多条 select 查询的方式。

discriminor 鉴别器:根据查询结果中某个字段作为标识符,根据其查询的值,进行分类封装处理。

b.主键映射策略:如果主键由数据库生成,而不是手动指定,怎么获取该主键的问题。
一般使用 selectKey 标签处理。
3️⃣自定义封装结果集(了解):
按照自已定义方式把数据库返回结果封装成自己想要的类型。
一般自己建立结果的处理类,实现 ResultHandler 接口,重写里面的方法。
一般在select(“statementId”,params,ResultHandler实现类对像)
4️⃣解决什么样问题
用来解决 sql 语句 where 后条件的拼接问题,
使用流程控制标签来完成条件的拼接:if 标签,where 标签,foreach 标签。。。。
5️⃣常用动态 sql 标签
1.if 标签
2.choose when otherwise 标签
3.trim 标签
4.foreache 标签
5.where 标签
6.set 标签

二、if 标签:类似于 Java 中的 if 语句

<select id="selectByItem" parameterType="User" resultType="User">
    select * from USER
    where 1=1
    <!--mybatis如果是低版本时,如果是整形值,不要用‘’来判断;3.4.6版本没有问题-->
    <if test="id != null and id !=''">
        and id = #{id}
    </if>
    <if test="username !=null and username !=''">
        and username=#{username}
    </if>
</select>

三、where标签

根据查询条件是否存在,来决定是否生成 where 字符串。可以去除 where 后面紧跟的 sql 关键字, 如 or 或 and。

<!-- where标签 -->
<select id="selectByItem2" parameterType="User" resultType="User">
    select * from USER
    <where>
        <if test="id !='' and id != null">
            and id = #{id}
        </if>
        <if test="username !=null and username !=''">
            and username=#{username}
        </if>
    </where>
</select>

四、 choose when otherwise

和 Java 中 switch case 作用类似。不管有多少条件满足,只拼接其中一个条件。

<!-- choose when otherwise -->
<select id="selectByItem3" parameterType="User" resultType="User">
    select * from USER
    <where>
        <choose>
            <when test="id !='' and id != null">
                and id = #{id}
            </when>
            <when test="username !=null and username !=''">
                and username=#{username}
            </when>
            <otherwise>
                and 1=1
            </otherwise>
        </choose>
    </where>
</select>

五、Set 标签

set 用法和上面 where 用法一致。可以生成 update 语句中的 set 关键字,也可去除 sql 关键字,如逗号(set 标签中 sql 字符串最后的逗号)

<update id="update" parameterType="User">
    update USER
    <set>
        <if test="username !=null and username !=''">
            username = #{username},
        </if>
        <if test="password !=null and password !=''">
            password=#{password},
        </if>
    </set>
    where id = #{id}
</update>

六、foreach 标签

类似于 Java 中的 foreach 迭代。把传入 sql 语句中的数组类型或集合类型数据进行遍历操作。

<!-- 接口中参数为数组时,collection值必须为array -->
<select id="selectByIds" resultType="User">
    select * from USER
    <foreach collection="array" item="id" open="where id in(" separator="," close=")">
        #{id}
    </foreach>
</select><!-- 接口中参数为pojo,collection值必须为该数组类型属性名 -->
<select id="selectByIds2" resultType="User" parameterType="User">
    select * from USER
    <foreach collection="ids" item="id" open="where id in(" separator="," close=")">
        #{id}
    </foreach>
</select>

七、trim 标签:可以删除 sql 语句指定的字符串。

<!--
 prefix="前缀":在后面sql字符串前面拼接指定的前缀。
 prefixOverrides="要去除的字符":把紧跟着前缀后面的字符去掉。
 suffix="后缀":在后面sql字符串后面拼接指定的后缀。
 suffixOverrides="要去除的字符":把紧跟着后缀前面的字符去掉。
--><update id="update2" parameterType="User">
    update USER
    <trim prefix="set" prefixOverrides=",">
        <if test="username !=null and username !=''">
            ,username = #{username}
        </if>
        <if test="password !=null and password !=''">
            ,password=#{password}
        </if>
        <if test="address !=null and address !=''">
            ,address=#{address}
        </if>
    </trim>
    where id = #{id}
</update>

<update id="update3" parameterType="User">
    update USER
    <trim prefix="set" suffixOverrides=",">
        <if test="username !=null and username !=''">
            username = #{username},
        </if>
        <if test="password !=null and password !=''">
            password=#{password},
        </if>
        <if test="address !=null and address !=''">
            address=#{address},
        </if>
    </trim>
    where id = #{id}
</update>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐