MyBatis动态SQL

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。MyBatis动态SQL,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

什么是MyBatis

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射、SQL 是 MyBatis 的强大特性之一。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

动态SQL:

什么是动态SQL,使用传统的组合拼接SQL,稍微不注意就会多出空格,导致报错,Mybatis的动态SQL功能正是为了解决这种问题,通过if,choose,when, otherwise,trim,where, set,foreach标签组合SQL语句

标签

说明
if 条件判断,java相似
choose

条件判断,是一个组合使用标签,需要搭配when、otherwise标签使用,类似java中的switch语句

trim 动态添加前后缀,智能互联前后缀多余的关键字
where 简化SQL语句中where关键字,智能处理and|or等关键字
set 简化SQL语句中set关键字,智能处理尾部多余的逗号
foreach 以遍历方式处理集合类型参数

if标签

 if标签是MyBatis框架动态SQL常用且重要的标签之一,它的功能和java中的if判断相似

//if标签
<if test="判断条件,返回true或者false">
    SQL语句
</if>

choose标签

choose标签是一个组合标签,通常和when和otherwise一起使用,功能和java中的if…else语句类似

<select id="getlist" resultType="com.xinxi2.bean.TSysUser">
        SELECT * FROM `t_sys_user` 
        <where>
            <choose>
                <when test="realname!=null and realname!='' ">
                    and realname=#{realname}
                </when>
                <when test="password!=null and password!='' ">
                    and password=#{password}
                </when>
                <otherwise>
                    and acccunt is not null
                </otherwise>
            </choose>
        </where>
    </select>

trim标签

通过trim元素自定义where 元素的功能和set元素的功能,并可以智能地忽略标签前后多余的and、or或逗号等字符

prefix 给sql语句拼接的前缀
suffix 给sql语句拼接的后缀
prefixOverrides 去除sql语句前面的关键字,该关键字由prefixOverrides属性指定,假设该属性指定为“AND|OR”,当sql语句的开头为“AND或者OR”,trim标签会去除“AND或者OR”
suffixOverrides

去除sql语句后面的字符, 该字符由suffixOverrides属性指定

1.前缀和忽略前缀用于查询数据库代码

//prefix前缀  prefixOverrides忽略前缀  
<select id="getlist" resultType="com.xinxi2.bean.TSysUser">
        SELECT * FROM `t_sys_user`
        <trim prefix="where" prefixOverrides="and|or">
            <if test="realname!=null">
                and realname=#{realname}
            </if>
            <if test="password!=null">
                and password=#{password}
            </if>
            <if test="acccunt!=null">
                and acccunt=#{acccunt}
            </if>
        </trim>
    </select>

2.后缀和忽略后缀用于修改数据库代码 

//suffix后缀 suffixOverrides忽略后缀
<update id="update" parameterType="com.xinxi2.bean.TSysUser">
        update t_sys_user
            <trim suffix="set" suffixOverrides=",">
                <if test="realname!=null">
                    realname=#{realname},
                </if>
                <if test="password!=null">
                    password=#{password},
                </if>
                <if test="acccunt!=null">
                    acccunt=#{acccunt},
                </if>
            </trim>
        where id=#{id}
    </update>

where标签

where标签的主要作用是对SQL语句中的where关键字进行简化处理,并可以智能地处理其内部and、or等关键字

/*TSysUserMapper.xml*/
<select id="getlist" resultType="com.xinxi2.bean.TSysUser">
        SELECT * FROM `t_sys_user`
        <where>
            <if test="realname!=null">
               and realname=#{realname}
            </if>
            <if test="password!=null">
               and password=#{password}
            </if>
            <if test="acccunt!=null">
               and acccunt=#{acccunt}
            </if>
            <if test="id!=null">
                and id=#{id}
            </if>
        </where>
    </select>

 set标签

MyBatis 提供了 set 元素,set 元素主要用于修改操作,它可以在动态 SQL 语句前输出一个 SET 关键字,并将 SQL 语句中最后一个多余的逗号去掉,set 元素与 if 元素结合可以实现只修改需要更新的字段

/*TSysUserMapper.xml*/
<update id="update" parameterType="com.xinxi2.bean.TSysUser">
        update t_sys_user
        <set>
            <if test="realname!=null">
                realname=#{realname},
            </if>
            <if test="password!=null">
                password=#{password},
            </if>
            <if test="acccunt!=null">
                acccunt=#{acccunt},
            </if>
        </set>
        where id=#{id}
    </update>

foreach标签

循环标签 适用于批量添加、删除 和查询记录

collection 必填,指定参数入参类型 列表(list) 、数组(array)、HashMap(map)
item 起名字,给集合中单个元素起名称
open 开始字符串
close 结束字符串
separator 分割符
index 索引的属性名,在集合数组下值为当前的索引值

1.foreach 元素迭代数组

//单参数List的类型
/*TSysUserMapper.xml*/
<select id="getlist" resultType="com.xinxi2.bean.TSysUser">
        SELECT * FROM `t_sys_user`
        <where>
            id in 
            <foreach collection="list" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

2.foreach 元素迭代 List

//数组类型的参数
/*TSysUserMapper.xml*/
<select id="getlist" resultType="com.xinxi2.bean.TSysUser">
        SELECT * FROM `t_sys_user`
        <where>
            id in 
            <foreach collection="array" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

3.foreach 元素迭代 Map

//Map类型参数
/*TSysUserMapper.xml*/
<select id="getlist" resultType="com.xinxi2.bean.TSysUser">
        SELECT * FROM `t_sys_user`
        <where>
            id in 
            <foreach collection="array" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </select>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/161203.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!