Mybatis——动态sql

动态sql

1.同一个dao的方法,根据不同的条件表示不同的sql语句,主要是where部分有变化;

2.使用mybatis提供的标签,实现动态sql能力,主要使用如 if, where, foreach, sql;

3.使用动态sql的时候,dao方法的形参使用java对象;

4.多条件查询时可以使用动态sql;

一、if标签

<if test="boolean判断结果(条件)">
    sql代码
</if>
<!--if标签
        test:使用对象的属性值作为条件
        id=-1 能避免sql语句错误
    -->
    <select id="selectByIf" resultType="com.itjuzi.entity.Student">
        select * from student
        where id=-1
        <if test="name != null and name!='' ">
            or name like "%" #{name} "%"
        </if>
        <if test="age > 0">
            or age = #{age}
        </if>
    </select>

注意:

使用动态sql,出现 > >= < <= 符号时,最好将其转换为实体符号,否则xml可能解析会出现问题,特别是 < 符号。

1608804719929

二、where标签

使用if标签时容易引起sql语句语法错误,使用where标签可以解决这些问题。

使用where,里面是一个或多个if标签,当有一个if标签判断条件为true,where标签会转为WHERE关键字附加到sql语句后面;如果if没有一个条件为true,则忽略where和里面的if。

<where>
	<if test="条件1">sql语句1</if>
    <if test="条件2">sql语句2</if>
</where>
<!--where标签-->
    <select id="selectByWhere" resultType="com.itjuzi.entity.Student">
        select * from student
        <where>
            <if test="name != null and name!='' ">
                or name like "%" #{name} "%"
            </if>
            <if test="age > 0">
                or age &lt;= #{age}
            </if>
        </where>
    </select>

三、foreach标签

使用foreach可以循环数组,list集合,一般使用在in语句中。

<foreach collection="集合类型" open="开始的字符" close="结束的字符"
    	 item="集合中的成员" separator="集合成员之间的分割符">
    #{item的值}
</foreach>
标签属性:
collection:表示循环的对象是数组还是list集合。如果dao方法的形参是数组,collection="array";
			如果dao方法形参是list,collection="list";
open:循环开始的字符。sql.append("(");
close:循环结束的字符。sql.append(")");
item:集合成员,自定义的变量。Integer item = idList.get(i);
separator:集合成员之间的分隔符。sql.append(",");
#{item的值}:获取集合成员的值;
<!--foreach第一种方式,循环简单类型的List-->
    <select id="selectForeachOne" resultType="com.itjuzi.entity.Student">
        select * from student
        <if test="list != null and list.size>0">
            where id in
            <foreach collection="list" open="(" close=")" separator="," item="myId">
                #{myId}
            </foreach>
        </if>
    </select>
<!--foreach第二种方式,循环对象List<Student>-->
    <select id="selectForeachTwo" resultType="com.itjuzi.entity.Student">
        select * from student
        <if test="list != null and list.size>0">
            where id in
            <foreach collection="list" open="(" close=")" separator="," item="student">
                #{student.id}
            </foreach>
        </if>
    </select>

四、sql标签 代码片段

sql标签表示一段sql代码,可以是表名,几个字段,where条件都可以,可以在其它地方复用sql标签的内容。

1)在mapper文件中定义sql代码片段。<sql id="唯一字符串">部分sql语句</sql>
2)在其它位置,使用include标签引用某个代码片段。
<!--定义代码p片段-->
    <sql id="studentSelect">
        select * from student
    </sql>
    
 <select id="selectByIf" resultType="com.itjuzi.entity.Student">
 
        <include refid="studentSelect"/>
        
        where id=-1
        <if test="name != null and name!='' ">
            or name like "%" #{name} "%"
        </if>
        <if test="age > 0">
            or age >= #{age}
        </if>
    </select>
  • 19
    点赞
  • 119
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值