MyBatis 动态SQL技术

news/2024/7/7 20:34:14

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能。

sql 标签

<sql id="empColumns">
eid,ename,age,sex,did
</sql>
select <include refid="empColumns"></include> from t_emp

可以记录一段公共sql片段,在使用的地方通过include标签进行引入

if 标签

<select id="getEmpListByMoreTJ" resultType="Emp">
	select * from t_emp where 1=1
	<if test="ename != '' and ename != null">
	and ename = #{ename}
	</if>
	<if test="age != '' and age != null">
	and age = #{age}
	</if>
	<if test="sex != '' and sex != null">
	and sex = #{sex}
	</if>
</select>

if标签可以通过test属性的表达式进行判断,若表达式结果为true,则执行标签中的内容。

where 标签

<select id="getEmpListByMoreTJ2" resultType="Emp">
	select * from t_emp
	<where>
		<if test="ename != '' and ename != null">
		and ename = #{ename}
		</if>
		<if test="age != '' and age != null">
		and age = #{age}
		</if>
		<if test="sex != '' and sex != null">
		and sex = #{sex}
		</if>
	</where>
</select>

where 一般和 if 结合使用。where会去掉条件开头多余的and

trim 标签

<select id="getEmpListByMoreTJ" resultType="Emp">
	select * from t_emp
	<trim prefix="where" suffixOverrides="and">
		<if test="ename != '' and ename != null">
		ename = #{ename} and
		</if>
		<if test="age != '' and age != null">
		age = #{age} and
		</if>
		<if test="sex != '' and sex != null">
		sex = #{sex}
		</if>
	</trim>
</select>

trim用于去掉或添加标签中的内容

常用属性:
prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容

foreach 标签

foreach 用于批量操作数据库。foreach标签的内容存放用于循环的sql操作。
比如在批量删除和添加时,需要用到foreach。

  • 批量添加:
<!--void insertManyEmp(@Param("emps") List<Emp> emps);-->
<insert id="insertManyEmp">
    insert into t_emp values
    <foreach collection="emps" item="emp" separator=",">
        (null,#{emp.empName},#{emp.age},#{emp.gender},null)
    </foreach>
</insert>
  • 批量删除:
<!--void deleteManyEmp(@Param("empIds") Integer[] empIds);-->
<delete id="deleteManyEmp">
    delete from t_emp where emp_id in (
    <foreach collection="empIds" item="empId" separator=",">
        #{empId}
    </foreach>)
</delete>

http://lihuaxi.xjx100.cn/news/125956.html

相关文章

动手学深度学习(pytorch)2

动手学深度学习&#xff08;pytorch&#xff09;2概率库函数查阅线性神经网络线性回归概率 先抽样 import torch from torch.distributions import multinomial from d2l import torch as d2l fair_probs torch.ones([6]) / 6 tmultinomial.Multinomial(10, fair_probs).sam…

人工智能Linux基础命令

操作系统 定义&#xff1a;直接运行在计算机上的系统软件&#xff0c;它是与硬件打交道和控制软件运行的计算机程序。 主要作用&#xff1a;向下控制硬件&#xff0c;向上支持软件的运行 一切皆文件&#xff0c;只有根目录/ Linux内核是操作系统内部操作和控制硬件设备的核心…

【linux】线程条件变量 信号量

1.条件变量 条件变量本身不是锁&#xff01;但它也可以造成线程阻塞 通常与互斥锁配合使用。给多线程提供一个会合的场所。 1.主要函数应用 pthread_cond_init函数 pthread_cond_destroy函数 pthread_cond_wait函数 pthread_cond_timedwait函数 pthread_cond_signal函数 pthrea…

01.高等数学基础

高等数学基础1.数列2. 极限2.1 符号表示2.2 极限充要条件2.3 极限中的无穷小2.4 极限中的无穷大2.5 无穷小的比较3.函数的连续性3.1 函数连续性定义3.2 函数连续性满足的条件3.3 函数的间断点3.4 函数间断点分类4.导数4.1 导数定义4.2 常用的导数4.3导数基本运算5.偏导数5.1 偏…

处理多维特征的输入

一、多维特征输入 多维数据的输入可以看成矩阵进行运算&#xff0c;如下有8维的数据&#xff08;N*8&#xff09;&#xff0c;进变换后形成1维的数据&#xff08;N*1&#xff09; 二、糖尿病数据集为例 数据说明 pregnancies&#xff1a; 怀孕次数 glucose&#xff1a;口服葡…

【每天学习一点新知识】CC攻击和DDoS的区别

目录 CC攻击原理 CC攻击现象&#xff1a; 和DDoS的区别 CC攻击的常用防护方式 CC攻击原理 CC&#xff08;Challenge Collapsar&#xff09;&#xff0c;可以归为DDoS攻击的一种&#xff0c;对一些消耗资源较大的应用页面不断发起正常的请求&#xff0c;以达到消耗服务端资源…

两个List循环效率对比 List转Map 循环效率对比 Listmap 循环 效率对比

两个List循环效率对比 List转Map 循环效率对比 Listmap 循环 效率对比 一、情景描述 1、在微服务开发中&#xff0c;如&#xff1a; 查询用户列表 userList&#xff0c;需要关联查询 每个用户下面的文件信息&#xff0c;由于数据库层隔离&#xff0c;不能直接进行 left join &a…

Endnote X9文献管理器应用---使用总结

Endnote文献管理器应用---使用总结1. 文献分类和文献管理器Endnote2. Endnote使用&#xff08;1&#xff09;新建本地文献库&#xff08;2&#xff09;创建分组和文献导入&#xff08;3&#xff09;文献插入word文档&#xff08;4&#xff09;文献Style编辑&#xff0c;下载&am…