xml--sql语句

1.update/insert/select/delete

标签要和SQL语句要执行的类型对应好

2.#{}表示一个占位符(将参数内容加引号,以字符串形式拼接到SQL中),可以防止SQL注入;${}将内容直接拼接在SQL中,不能防止SQL注入。

eg:
参数形式为:1 union select user,password from users

select * from users WHERE user_id = #{id};=>select * from users WHERE user_id = '1 union select user,password from users';
将参数作为整个参数,没有满足要求则不会返回
select * from users WHERE user_id = ${id};=>select * from users WHERE user_id = 1 union select user,password from users
加上参数后实则为两个SQL语句,可以在参数中输入union关联想要查询的任何内容

3.where条件动态拼接(where子句能够自动消除第一个and)

<sql id="where1">
    <if	test="consno != null and consno != ''">
	    and cons_no = #{consno} 
	</if>
    <if	test="usertype != null and usertype !=''">
		and user_type=#{usertype}
	</if>
</sql>


<select id="consNo" resultType="User">
	select * from user
	<where>
		<include refid="where1"/>
	</where>
</select>

4.set标签

set标签中内容为空时,整个set标签去掉

<update id="update1">
    update user
    <set>
     <if test="name != null">
            NAME = #{name},
        </if>
    </set>
</update>

如果name=null,则语句为update user

5.foreach标签

在SQL中需要用到 in 时,在xml中可以用foreach循环来组合需要满足的 in 的数组

<foreach>标签中 collection :变量所在数组,index 位置,item 元素,open 在sql语句中变量开始位置符号,separator 在sql语句中变量间分隔的符号, close 在sql语句中变量结束位置符号

例子一:循环数组List

<!-- 数据库中 -->
select * from TB_SET
where plantid in ('300','301')
        
<!-- xml中 -->      
select * from TB_SET
where plantid in
<foreach collection="List" index="index" item="item" open="(" separator="," close=")">
     #{item}
</foreach>

例子二:循环map

<!-- 数据库中 -->
delete from table_name
where version = 'v0' and  (
        (pdate = '2022-01-02' and subject = '12312') or
        (pdate = '2022-01-03' and subject = '12352')
    )

<!-- xml中 -->  
delete from ${tableName}
where
version = 'v0'
<if test="map !=null">
    and
    <foreach collection="map" item="item" separator="or" open="(" close=")">
        (pdate = #{item.pdate} and subject = #{item.subjectNo})
    </foreach>
</if>

6.choose标签

<choose>
   <when test="name != null">
      order by ${name}
   </when>
   <otherwise>
      order by id desc
   </otherwise>
</choose>

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
xml文件中,可以使用多种标签和语法来编写SQL语句。其中常用的标签包括: 1. if标签:用于条件判断,根据条件动态生成SQL语句的一部分。 2. where标签:用于生成WHERE子句,多个条件可以使用AND或OR连接。 3. choose、when、otherwise标签:类似于if-else语句,根据条件选择不同的SQL语句块。 4. set标签:用于生成UPDATE语句的SET部分,根据条件动态设置更新的字段。 5. foreach标签:用于循环操作,可以遍历集合或数组,并生成相应的SQL语句。 通过以上标签的组合和嵌套,可以实现复杂的SQL语句拼接和动态生成。 以下是一个示例代码,展示了在xml文件中使用上述标签的情况: ```xml <select id="getStudentCount" parameterType="java.util.Map" resultType="java.util.Map"> select COUNT(*) count from student <where> <if test="record.ids != null"> and id in <foreach item="item" index="index" collection="record.ids" open="(" separator="," close=")"> #{item} </foreach> </if> </where> </select> ``` 在这个例子中,使用了if条件判断和foreach循环标签,根据条件动态生成了一个带有WHERE子句的SELECT语句。当record对象的ids属性不为空时,会生成形如"and id in (value1, value2, ...)"的SQL语句。这种灵活的动态SQL语法,使得在xml文件中编写复杂的SQL语句变得更加方便和可读性更高。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mybatis Plus中在XML文件中使用自定义的sql语句](https://blog.csdn.net/weixin_33074719/article/details/123480593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [myBatis xml文件SQL编写( if、where、if-else、set 、foreach )](https://blog.csdn.net/baidu_41847368/article/details/114593640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值