| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4196 人关注过本帖
标题:动态SQL
只看楼主 加入收藏
fyzn12
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2019-9-25
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:1 
动态SQL

动态SQL语句总结
主要内容

    if元素
    choose、when、otherwise元素
    trim、where、set元素
    foreach元素

<if>元素

下面先看一个例子

<select id="selectUserById" resultType="MyUser" parameterType="MyUser">
    select * from user where 1=1
    <if test ="name !=null and name !=''">
        and name like concat('%',#{name},'%')
    </if>
    <if test = "sex !=null and sex !=''">
        and sex = #{sex}
    </if>
</select>  

在上面的例子中,运用的是sql语句的拼接,在这里需要注意的是有如下几点:
1. resultType与resultMap不能同时使用
2. #{xxx}与${vlaue}的使用看上一篇博客
3. if的判断条件的test里面的参数目前我知道有三种声明方式如下:

    1)对象作为参数,则test里面的参数为对象的属性  
    2)@Param声明的参数  
    3)map方式键值对  

 
<choose>、<when> 、<otherwise>元素

有些时候不想用到所以的条件语句,而只想从中选择一二,针对这种情况,Mybatis提供了<choose>元素,如下例子。

<select id = "selectUserBychoose" resultType="MyUser" parameterType="MyUser">
    select * from user where 1=1
    <choose>
        <when test="name !=null and name !=''">
            and name like concat('%',#{name},'%')
        </when>
        <when test="sex != null and sex !='' "
            and sex=#{sex}
        </when>
        <otherwise>
            and id>10
        </otherwise>
    </choose>  

上面sql语句的含义是当满足test里面的其中之一时,执行when子句,两者都不满足时执行otherwise子句,这样做就可以实现if元素没法实现的一些选择功能。
<trim>、<where>、<set>元素

trim 的主要功能是可以在自己包含的内容上加上某些前缀,也可以在后面加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,及忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides。正因为<trim>元素有这样的功能,所以也可以非常简单利用<trim>来代替<where>元素功能。代码如下:

trim应用

<select id="selectByTrim" resultType="MyUser" parameType="MyUser">
    select * from user
    <trim prefix="where" prefixOverrides="and | or">
        <if test="name !=null and name !=''">
            and name like concat('%',#{name},'%')
        </if>
        <if test="sex !=null and sex !=''">
            and sex=#{sex}
        </if>
    </trim>
</select>   

where子句的应用

<select id="selectByWhere" resultType="MyUser" parameType="MyUser">
    select * from user
    <where>
        <if test="name !=null and name !=''">
            and name like concat('%',#{name},'%')
        </if>
        <if test="sex !=null and sex !=''">
            and sex=#{sex}
        </if>
    </where>
</select>  

set元素的应用:

<select id="selectBySet" resultType="MyUser" parameType="MyUser">
    update user
    <set>
        <if test="name !=null and name !=''"
            name like concat('%',#{name},'%')
        </if>
        <if test="sex !=null and sex !=''"
            sex like concat('%',#{sex},'%')
        </if>
    </set>
    where id=#{id}
</select>

set 子句会在update的sql语句中用到,对应的值千万别忘了where限定。

foreach元素的应用:
foreach主要用在构建in条件中,它可以在SQL语句中迭代一个集合,<foreach>元素的属性主要有item、index、collection、open、separator、close。item表示集合中每个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号进行分隔符,close表示以什么结束。在使用<foreach>元素时最关键,最容易出错的是collection属性,该属性是必选的,但在不同情况下值不同,有以下几种情况:

    如果传入的是单参数且参数类型是一个List,collection属性值为list
    如果传入的参数是单参数且参数类型是一个arry数组,collection值为arry
    如果传入的参数是多个,需要把她们封装成一个Map,当然单参数也可以封装成Map。map的key值参数名,collection属性值是传入的List或者arry对象在封装的Map中的key

具体例子如下:
 

<select id="selectByForeach" resultType="MyUser" parameType="List">
    select *from user
    <foreach item="item" index='index' collection='list' open="("separator='',"close=")">
        #{item}
    </foreach>
</select>

<bind>元素

在进行模糊查询时,如果使用”${}“拼接字符串,则无法防止SQL注入问题;bind使用可以一定程度上避免这个问题。代码演示如下:

<select id="selectByBind" resultType="MyUser" parameType="List">
    <bind name ="paran_uname" vlaue = "'%'+uname+'%' "/>
    select * from user
        where name like #{paran_uname}
</select>

mybatis
搜索更多相关主题的帖子: and name 元素 if select 
2019-10-09 20:34
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
你也知道是mybatis,为啥在java板块发?

剑栈风樯各苦辛,别时冰雪到时春
2019-10-10 08:55
快速回复:动态SQL
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015421 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved