美文网首页
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语句

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