美文网首页
mybatis动态sql语句

mybatis动态sql语句

作者: 忒无聊了叭 | 来源:发表于2019-12-19 19:08 被阅读0次

最近在写ssm项目,感觉自己的动态sql部分学的不太好,便自己又复习了一遍~~~

根据条件查询:

接口中传入一个user,这个user设定了姓名、年龄、性别。如果此时我们只给该user一个属性,即姓名或年龄或性别。用动态sql语句怎么写?

if标签:

<!--根据条件查询-->
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
    select * from user where 1=1
    <if test = "userName != null">
        and username = #{userName}
    </if>
</select>
<!--根据条件查询-->
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
    select * from user where 1=1
    <if test = "userName != null">
        and username = #{userName}
    </if>
    <if test="userSex != null">
        and sex = #{userSex}
    </if>
</select>

where+if标签

注意:where标签会检索语句,将where中的第一个and 或者or 去掉。

<!--根据条件查询-->
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
    select * from user
    <where>
        <if test = "userName != null">
            and username = #{userName}
        </if>
        <if test="userSex != null">
            and sex = #{userSex}
        </if>
    </where>
</select>

foreach标签:

传入可以是一个数组、集合,在SQL语句中进行遍历。

<delete id="delGroup">
    delete from groupmsg
    <where>
        <foreach collection="array" item="groupNum" open="and g_id in(" close=")" separator=",">
            #{groupNum}
        </foreach>
    </where>
</delete>

当对一个集合。数组进行数据库的操作时,尽量使用foreach,因为你只用传入一个集合给dao层就可以,相当于只访问了一次数据库,而如果你在业务逻辑层将数据、集合进行遍历去操作数据库的话,就会多次访问数据库,如果数据量过大的话,可能给数据库带来一定压力。

when+choose标签:

<select id="selectGroupByName" resultMap="GroupMsg">
        select * from groupmsg
            <choose>
                <when test="g_name != null and g_name !='' ">
                   where g_name like '%' #{g_name} '%' limit #{page.pageIndexStart},#{page.pageSize}
                </when>
                <otherwise>
                limit #{page.pageIndexStart},#{page.pageSize}
                </otherwise>
            </choose>
    </select>

当有多个when标签时,只选择一个进行执行。

sql标签:

    <sql id="select">
        <where>
            <if test="person.communication_group!=null and person.communication_group!=''">
                and communication_group=#{person.communication_group}
            </if>
            <if test="person.sex!=null and person.sex!=''">
                and sex=#{person.sex}
            </if>
            <if test="person.name!=null and person.name!=''">
                and name=#{person.name}
            </if>
            <if test="person.department!=null and person.department!=''">
                and department=#{person.department}
            </if>
            <if test="person.email!=null and person.email!=''">
                and email=#{person.email}
            </if>
            <if test="person.address!=null and person.address!=''">
                and address=#{person.address}
            </if>
            <if test="person.company!=null and person.company!=''">
                and company=#{person.company}
            </if>
        </where>
    </sql>

该标签可以被其他语句进行调用。

相关文章

  • MyBatis 动态SQL(*.xml)

    原文参考MyBatis 动态SQL MyBatis的动态SQL大大减少了拼接SQL语句时候的各种格式问题,这里摘录...

  • 02.MyBatis映射文件深入

    1.1 动态sql语句 1. 动态sql语句概述 Mybatis 的映射文件中,前面我们的 SQL 都是比较简单的...

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • IT 每日一结

    mybatis动态sql 动态sql绝对是mybatis排列前几的闪光点之一。传统代码中的sql语句需要经过多个字...

  • Mybatis入门(三)之动态sql

    Mybatis入门之动态sql 动态拼接sql语句,在我的理解就是相当于Java中的逻辑控制语句(if,,swit...

  • Java数据持久化之mybatis(12)

    mybatis 动态SQL以及和spring的集成 3.6 动态SQL 有时候,静态的 SQL 语句并不能满足应用...

  • MyBatis动态sql

    动态sql就是可以对sql语句进行灵活的封装,拼接。通过mybatis语法的判断可以实现动态sql。 1 if标签...

  • mybatis动态sql

    一 动态sql 1.1 什么是动态sql? mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对...

  • Mybatis--动态SQL(if,where,trim)

    Mybatis框架的动态SQL技术是一种根据特定条件动态拼接SQL语句的功能,作用是为了解决拼接SQL语句字符串的...

网友评论

      本文标题:mybatis动态sql语句

      本文链接:https://www.haomeiwen.com/subject/ghdvnctx.html