Mybatis 学习 之 XML 手册

目录

单次执行

单次新增

xml 复制代码
<insert id="insert" parameterType="java.util.List">  
    INSERT INTO USERS 
    (
    	NAME, 
    	EMAIL, 
    	AGE
    )  
    VALUES  
	(
		#{item.name}, 
		#{item.email},
		#{item.age}
	)  
</insert>

单次更新

xml 复制代码
<update id="update"  parameterType="java.util.List">  
	UPDATE USER
	<set>
	    NAME = #{item.name},
	    EMAIL = #{item.email},
	    AGE = #{item.age}
	</set>
	where ID = ${item.id}
</update>

单次删除

xml 复制代码
<delete id="delete" parameterType="java.util.List">  
    DELETE FROM 
    	USERS  
    WHERE 
    	ID = #{id}
</delete>

批量执行

MyBatis 的设计初衷是每个标签对应一个 SQL 语句,也就是不支持单个标签多条 SQL 语句的执行。

批量新增

xml 复制代码
<insert id="insertBatch" parameterType="java.util.List">  
    INSERT INTO USERS 
    (
    	NAME, 
    	EMAIL, 
    	AGE
    )  
    VALUES  
    <foreach collection="list" item="item" index="index" separator=",">  
	(
		#{item.name}, 
		#{item.email},
		#{item.age}
	)  
    </foreach>  
</insert>

批量更新

for 循环执行更新

for 循环生成多条 sql,一次执行

如需要在连接 MySQL 的 URL 上加 &allowMultiQueries=true

xml 复制代码
<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" separator=";">
        UPDATE USER
        <set>
            NAME = #{item.name},
            EMAIL = #{item.email},
            AGE = #{item.age}
        </set>
        where ID = ${item.id}
    </foreach>      
</update>

批量删除

xml 复制代码
<delete id="deleteBatch" parameterType="java.util.List">  
    DELETE FROM USERS  
    WHERE ID IN  
    <foreach collection="list" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</delete>

参数传递

  • XML 中逻辑判断 (<if> 等标签),不同的 Java 类型不同的判断规则
  • 入参类型应提前处理为与数据库中相匹配的类型,SQL 中处理类型问题不仅麻烦且可能存在版本问题

预处理方式 (OGNL表达式 #{})

  • 支持防 SQL 注入
  • 传入的参数自动数据类型转换
  • 有预处理,不能用于传递表名、列名 等

数据类型转换

Java 类型 XML 代码示例
Char <if test="req.parameter != null and req.parameter == 'A'">
Int <if test="req.parameter != null and req.parameter == 18">
Boolean <if test="req.parameter != null and req.parameter'">
String <if test="req.parameter != null and req.parameter != ''">
List <if test="req.parameter != null and req.parameter.size() > 0">
Array <if test="req.parameter != null and req.parameter.length > 0">
Map <if test="req.parameter != null and !req.parameter.isEmpty()">
Date <if test="req.parameter != null">
LocalDateTime <if test="req.parameter != null">

直接替换 (EL表达式 ${})

  • 有 SQL 注入风险
  • 传入的参数不支持自动数据类型转换
  • 可用于动态构建 SQL,用于传递表名、列名、排序字段名 等

安全

相关推荐
一隅论数智5 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
XiHongShi20165 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
爱吃苹果的日记本5 天前
离散数学第六课
学习·离散数学
AI职业加油站5 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
旖旎夜光5 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
奇思妙想聪明勤奋的小羊5 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
霍霍的袁5 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
彧azz5 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
我爱cope5 天前
【操作系统 | 计算机硬件:CPU、内存与 I/O 如何支撑操作系统?】
学习·操作系统