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,一次执行

如需要在连接 MySQLURL 上加 &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,用于传递表名、列名、排序字段名 等

安全

相关推荐
l1t1 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
Hello_Embed1 天前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中1 天前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Magnetic_h1 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
研梦非凡1 天前
ICCV 2025|从粗到细:用于高效3D高斯溅射的可学习离散小波变换
人工智能·深度学习·学习·3d
limengshi1383921 天前
机器学习面试:请介绍几种常用的学习率衰减方式
人工智能·学习·机器学习
知识分享小能手1 天前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
周周记笔记1 天前
学习笔记:第一个Python程序
笔记·学习
优雅鹅1 天前
ARM、AArch64、amd64、x86_64、x86有什么区别?
arm开发·学习
..过云雨1 天前
05.【Linux系统编程】进程(冯诺依曼体系结构、进程概念、进程状态(注意僵尸和孤儿)、进程优先级、进程切换和调度)
linux·笔记·学习