qidao123.com技术社区-IT企服评测·应用市场
标题:
动态SQL
[打印本页]
作者:
大连全瓷种植牙齿制作中心
时间:
2024-12-18 14:46
标题:
动态SQL
目录
if
where
trim
set
choose-when-otherwise
foreach
批量删除
批量插入
sql标签与include标签
不能使用&&,只能使用 and
空值:name != null and name != ''
if
public List<Monster> findMonsterByAge(@Param(value = "agex") Integer age);
复制代码
<select id="findMonsterByAge" resultType="Monster" parameterType="Integer">
select * from `monster` where 1 = 1
<if test="agex >= 0">
and `age` > #{agex}
</if>
<if test="xxx">
and xxx
</if>
</select>
复制代码
select * from monster where
[1 = 1] [and]
age > #{agex} and xxx
假如第一个不成立,
要加 1 = 1
,where 1 = 1 and xxx
假如全成立,
要加and
,where 1 = 1 and age > #{agex} and xxx
where
所有条件为空时,where标签保证不会生成where子句
自动去除某些条件
前面
多余的and或or
<select id="findMonsterByIdAndName" parameterType="Monster" resultType="Monster">
select * from `monster`
<where>
<if test="id >= 0">
and `id` > #{id}
</if>
<if test="name != null and name != ''">
and `name` = #{name}
</if>
</where>
</select>
复制代码
trim
在trim标签前/后添加前/后缀
preifx:加前缀
suffix:加后缀
把trim标签内容的前/后缀去掉
prefixOverrides:删除前缀
suffixOverrides:删除后缀
set
重要用在update语句中,用来生成set关键字,同时
去掉末了多余的 ","
只更新提交的不为空的字段
,假如提交的数据是空大概“ ”,那么这个字段将不更新数据
正常的update语句:null也会传给数据库
<update id="setMonster" parameterType="map">
update `monster`
<set>
<if test="age != null and age != ''">
`age` = #{age},
</if>
<if test="email != null and age != ''">
`email` = #{email},
</if>
</set>
where id = #{id}
</update>
复制代码
choose-when-otherwise
相当于if elseif elseif else
先根据name查,name为空根据id查,否则根据salary查
假如都为空,会实行末了一条语句
<select id="findchoose" parameterType="map" resultType="Monster">
select * from `monster`
<choose>
<when test="name != null and name != ''">
where `name` = #{name}
</when>
<when test="id > 0">
where `id` > #{id}
</when>
<otherwise>
where `salary` > 100
</otherwise>
</choose>
</select>
复制代码
foreach
常用于对集合进行遍历(尤其是在构建IN条件语句的时候)
collection:指定命组或集合
item:代表数组或集合中的元素
separator:循环之间的分隔符
open:foreach循环拼接的所有sql语句的最前面以什么开始
close:foreach循环拼接的所有sql语句的末了面以什么开始
底层map存放的key是array或arg0,value是这个数组
map.put("array",数组)、map.put("arg0",数组)
不写注解:
collection= array 或 arg0
写注解@Param("name"):
collection = "name"
批量删除
int deleteByIds (@Param("ids") Long[] ids);
复制代码
<select id="deleteByIds">
delete from `monster`
<if test="ids != null and ids != ''">
<where>
id IN
<foreach collection="ids" item="idx" open="(" separator="," close=")">
#{idx}
</foreach>
</where>
</if>
</select>
复制代码
批量插入
List<Car> cars = Arrays.asList(car1, car2, car3);
复制代码
int insertBatch (@Param("cars") List<Car> cars);
复制代码
<insert id="insertBatchByForeach">
insert into t_car values
<foreach collection="cars" item="car" separator=",">
(null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
</foreach>
</insert>
复制代码
sql标签与include标签
sql标签用来声明sql片段
include标签用来将声明的sql片段包含到某个sql语句当中
作用:代码复用。易维护。
<sql id="carCols">
id,
car_num carNum,
brand,guide_price guidePrice,
produce_time produceTime,
car_type carType
</sql>
<select id="selectAllRetMap" resultType="map">
select
<include refid="carCols"/>
from t_car
</select>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/)
Powered by Discuz! X3.4