动态SQL标签大全

动态SQL

  • 根据不同的条件执行不同的SQL命令,称为动态SQL
  • 在Mybatis中的Mapper.xml中添加逻辑判断

符号

在Mybatis中,运算符号会被转义成字节码,所以要用代码符号

<<=>>=&'"
<<=>>=&'"

if标签(逻辑判断)

  • 成立则执行,不成立则不执行
<select id="selByAccinAccout" resultType="log">
	select * from log where 1=1
	<!-- OGL表达式,直接写key或对象的属性,不需要添加任何特殊字符号 -->
	<if test="accin!=null and accin!=''">
		and accin = #{accin}
	</if>
	<if test="accout!=null and accout!=''">
		and accout = #{accout}
	</if>
</select>

where标签(SQL判断)

  • 当编写where标签时,如果内容中第一个是and去掉第一个and
  • 如果 <where>中有内容会生成where关键字,如果没有内容不生成where关键字
<select id="selByAccinAccout" resultType="log">
	select * from log
	<where>
		<if test="accin!=null and accin!=''">
			and accin = #{accin}
		</if>
		<if test="accout!=null and accout!=''">
			and accout = #{accout}
		</if>
	</where>
</select>

choose,when,otherwise(Java中的switch)

  • 只要有一个成立,其他都不执行
  • 如果title和content都不为null或都不为""
    • 生成的sql中只有where title=?
  • 如果title和content都为null或都为""
    • 生成的sql中只有where owner = “owner1”
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
	select * from t_blog where 1=1
	<choose>
		<when test="title != null">
			and title = #{title}
		</when>
		<when test="content != null">
			and content = #{content}
		</when>
        <!-- <otherwise>可以不写 -->
		<otherwise>
			and owner = "owner1"
		</otherwise>
	</choose>
</select>

set (sql修改)

  • 作用:去掉最后一个逗号
  • 作用:如果<set>里面有内容就会生成set关键字,没有就不生成
<update id="upd" parameterType="log">
	update log 
	<set>
		<!-- 防止不生成set关键字造成语法错误 -->
		id=#{id},
		<if test="accIn!=null and accIn!=''">
			accIn = #{accIn},
		</if>
		<if test="accOut!=null and accOut!=''">
			accOut = #{accOut},
		</if>
	</set>
	where id=#{id}
</update>

trim(截断 添加)

  • prefix 在前面添加内容
  • suffix 在后面添加内容
  • prefixOverrides 去掉前面内容
  • suffixOverrides 去掉后面内容
<update id="upd" parameterType="log">
	update log
	<!-- 去掉了后面的内容 -->
	<!-- 覆盖了标签后的逗号 -->
	<!-- 适用于存在符号和关键字的参数(例如金钱符号$) -->
	<trim prefix="set" suffixOverrides>
		a=a,
	</trim>
	where id=100
</update>

bind(模糊查询)

  • 作用:给参数重新赋值
  • 场景:模糊查询 | 在原内容前或后添加内容
<select id="selByLog" parameterType="log" resultType="log">
	select * from log
    <where>
        <!-- 常用语模糊查询(添加%) -->
        <if test="title!=null and title!=''">
            <bind name="title" value="'$'+title+'$'"/>
        	and title like #{title}
        </if>
        <!-- bind:给参数附加字符串 -->
        <if test="money!=null and money!=''">
            <bind name="money" value="'$'+money"/>
			and money = #{money}
        </if>
    </where>
</select>

foreach(循环)

  • 循环参数内容,还具备在内容的前后添加内容,还具备添加分割符功能
  • 适用场景:in查询 | 批量新增(mybatis中foreach效率大幅度降低)
    • 如果希望批量新增,SQL命令
      • openSession()必须指定
        • //底层的JBDC的PrepareStatement.addBatch()
        • factpry.openSession(ExecuteorType.BATCH);
  • 属性
    • collection:添加要遍历的集合
    • item:迭代变量,循环内使用#{迭代变量名}来获取内容
    • open:循环后左侧添加的内容
    • close:循环后右侧添加的内容
    • separator:添加每次遍历尾部追加的分割符
<select id="selIn" parameterType="list" resultType="log">
	select * from log where id in
	<foreach collection="list" item="a" open="(" close=")" separator=",">
		#{a}
	</foreach>
</select>

sql (复用)

  • 某些SQL片段如果需要复用,可以使用<sql>这个标签
<sql id="mysql">
	id,accin,accout,money
</sql>
  • 在<select>或<upfate>或<insert>中使用<include>标签进行复用引用
<select id="">
	select <include refid="mysql"></include> from log
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值