Mybatis基础之sql动态语句

目录

1、if语句

2、choose

3、trim标签

4、set元素

5、include和sql标签

6、 foreach元素的属性


测试的表结构

DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `money` double DEFAULT NULL,
  `isdeleted` tinyint(4) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)

1、if语句

<!--符合test中的条件就会把if语句内的内容添加到sql中 1=1就是为了解决字符串拼接的问题 -->
<select id="selByName" resultType="Account">
       select id,name,created,updated from account where 1=1
       <if test="name !=null and name !=''">
           and name like concat('%',#{name},'%')
       </if>
        
       <if test="isdeleted!=null and isdeleted!=''">
            and isdeleted like concat('%',#{isdeleted},'%')    
       </if>
</select>

<!--上个用的1=1 解决where字符串拼接问题 下面是用where标签解决问题
    where用来包含多个if的,当多个if有一个成立的时候where会自动增加一个where关键字,
并去掉if中多余的and,or等-->

<select id="selByName" resultType="Account">
       select id,name,created,updated from account 
       <where>
           <if test="name !=null and name !=''">
                 and name like concat('%',#{name},'%')
           </if>
        
           <if test="isdeleted!=null and isdeleted!=''">
                and isdeleted like concat('%',#{isdeleted},'%')    
           </if>
       <where>
</select>

2、choose

        choose就相当于多分支条件语句 类似于java中的switch...case...default

<!--when中有一个条件成立就不会再往下执行,当所有条件都不成立时,就会执行otherwise中的语句-->
<select id="selByChoose" resultType="Account">
        select id,name,created,updated,money from account where 1=1
        <choose>
            <when test="name !=null and name !=''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="money !=null and money !=''">
                and money =#{money}
            </when>
            <otherwise>
                and isdeleted=1
            </otherwise>
        </choose>
    </select>

3、trim标签

trim元素可以帮助我们去掉一下and、or等,prefix代表语句前缀, prefixOverrides代表要去掉的字符串

<select id="selByChoose" resultType="Account">
        select id,name,created,updated,money from account
        <trim prefix="where" prefixOverrides="and">
        <choose>
            <when test="name !=null and name !=''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="money !=null and money !=''">
                and money =#{money}
            </when>
            <otherwise>
                and isdeleted=1
            </otherwise>
        </choose>
        </trim>
    </select>

4、set元素

set元素,它可以在遇到逗号的时候,把对应的逗号去掉

<update id="updateAccout" parameterType="Account">
        update account
        <set>
            <if test="name !=null and name !=''">
               name=#{name},
            </if>
            <if test="money!=null and money!=''">
               money=#{money}
            </if>
        </set>
        where id=#{id}
    </update>

5、include和sql标签

sql标签
位置:在映射文件中
作用:可以存放多条SQL语句中重复的部分(一般用于存放重复的多个列名)
include标签
位置:在映射文件中的任意标签中
作用:引用SQL标签中的内容

 <sql id="refField">
        id,uname,upassword
    </sql>
    <select id="getAllUser" resultMap="getAllMap">
        select <include refid="refField"/> from user
    </select>
    <resultMap id="getAllMap" type="User">
        <id property="id" column="id"/>
        <result property="name" column="uname"/>
        <result property="password" column="upassword"/>
    </resultMap>

6、 foreach元素的属性

  • collection: foreach的对象,作为入参,对象为list、array时,collection属性值分别默认用"list"、"array"代替,Map对象没有默认的属性值。但是,在作为入参时可以使用@Param(“name”)注解来设置自定义collection属性值,设置name后,list、array会失效;
  • item: 集合元素迭代时的别名称,该参数为必选项;
  • index: 在list、array中,index为元素的序号索引。但是在Map中,index为遍历元素的key值,该参数为可选项;
  • open: 遍历集合时的开始符号,通常与close=")"搭配使用。使用场景IN(),values()时,该参数为可选项;
  • separator: 元素之间的分隔符,类比在IN()的时候,separator=",",最终所有遍历的元素将会以设定的(,)逗号符号隔开,该参数为可选项;

  • close: 遍历集合时的结束符号,通常与open="("搭配使用,该参数为可选项;
<select id="selIn" resultType="Account">
        select id,name,created,updated from account where name in
        <foreach collection="nameList" index="index" item="name" open="(" separator=","             close=")">
        #{name}
       </foreach>
 </select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值