Mybatis4 Mybatis动态sql的实现

感情迁移 提交于 2020-03-10 20:14:37

1.什么是动态SQL

SQL语句不固定, 会根据前台用户的操作而进行变化的SQL语句, 可以被称之为动态SQL. 在MyBatis中, 提供了一组标签, 用于方便的实现动态SQL, 不需要通过java代码拼接字符串了.
###2.动态sql中的标签

1. <if>

用于条件判断, test属性表示判断结果, 要求是一个boolean.

2.<where>

用于维护where子句, 通常配合一起使用. 如下功能:
a)当没有条件时, 不会创建WHERE关键字;
b)当有条件时, 会自动生成WHERE关键字;
c)会自动去掉第一个条件的and/or关键字.

3.<choose><when><otherwise>

功能类似于switch…case…default, 表示多分支判断, 只能成立一个条件

<mapper namespace="com.bjsxt.mapper.UserMapper">
    <select id="selByCondition" resultType="user">
        select * from tb_user
        <where>
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="username != null and username != ''">
                and username=#{username}
            </if>
            <if test="age != null">
                and age &lt;&gt; #{age}
            </if>
            <choose>
                <when test="birthday != null and birthday != ''">
                    and birthday = #{birthday}
                </when>
                <otherwise>
                    and birthday is null
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>
4.<bind>

对参数进行加工, 通常用于模糊查询给参数加通配符

<select id="sel2" resultType="user">
    <include refid="base_sql" />
    <where>
        <if test="realname != null and realname != ''">
            <bind name="realname" value="'%' + realname + '%'"/>
            and realname like #{realname}
        </if>
    </where>
</select>
5.<include>

配合使用, 用于提取通用sql语句片段, 用于引用SQL片段

<sql id="base_sql">
    select
        id, username, password, realname, age, birthday, reg_time regTime
    from tb_user
</sql>
<select id="sel2" resultType="user">
    <include refid="base_sql" />
    <where>
        <if test="realname != null and realname != ''">
            <bind name="realname" value="'%' + realname + '%'"/>
            and realname like #{realname}
        </if>
    </where>
</select>
6.<set>

用于维护update语句中的set子句, 特点是可以删除多余的逗号

<update id="upd">
    update
        tb_user
    <set>
        <if test="username != null and username != ''">
            username=#{username},
        </if>
        <if test="age != null">
            age=#{age}
        </if>
    </set>
    where
        id=#{id}
</update>
7.<foreach>

遍历集合(数组, List, Set, Map), 通常用于in操作或批量新增. 属性简介:

a)collection: 要遍历的集合

b)item: 迭代项

c)open: 以什么字符开头

d)close: 以什么字符结束

e)separator: 多个迭代项之间的分隔符

<delete id="delBatch">
    delete from tb_user
    <where>
        id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </where>
</delete>
8.<trim>

在语句的前后进行追加和去除指定的字符.

<insert id="insBatch">
    insert into tb_user values
    <foreach collection="users" item="user" separator=",">
        <trim prefix="(" prefixOverrides="," suffix=")" suffixOverrides=",">
            ,default, #{user.username}, #{user.password}, #{user.realname}, #{user.age}, #{user.birthday}, now(),
        </trim>
    </foreach>
</insert>

需要解决问题和获取学习资料可以加群 272112643
经常在群里面解决问题 分享学习视频和资料 共同成长!!。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!