网站首页 > 技术文章 正文
标签的认识:
resultType : 表的字段名称和类的属性名称一致的情况下,完成对象的封装
resultMap:表的字段名和类的属性名称不一致的情况下,完成对象的封装
<where> 标签 :
1.代替where关键字
2.智能把where后面的第一个条件的and关键字去掉
3.如果where后面没有条件,智能把where去掉
<select id = “findAll” resultType=“实体类全名”>
select * from user
<where>
<if test="username!=null and username!=‘’">
and username like #{username }
</if>
<if test="start!=null">
and birthday >= #{start}
</if>
<if test="end!=null">
and birthday <= #{end}
</if>
</where>
</select>
set 标签 :
1.代替set关键字
2.智能把最后一个set条件的逗号去掉
<update id="update”>
update user
<set>
<if test="username!=null and username!=''">
username = #{username},
</if>
<if test="birthday!=null">
birthday = #{birthday},
</if>
<if test="sex!=null and sex!=''">
sex = #{sex},
</if>
<if test="address!=null and address!=''">
address = #{address},
</if>
</set>
where id = #{id}
</update>
MySQL批量insert/update/select写法:
<insert id="saveSheeet" parameterType="java.util.List">
insert into ts_visit_tracks (id,created_date,business_id, store_id, product_id,
member_id, area_id)
values
<foreach collection="list" item="item" separator=",">
(#{item.id},#{item.created_date}, #{item.business_id}, #{item.store_id},
#{item.product_id}, #{item.member_id}, #{item.area_id})
</foreach>
</insert>
//缺点:这种方式需要设置jdbc连接 allowMultiQueries=true
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update course
<set>
name=${item.name}
</set>
where id = ${item.id}
</foreach>
</update>
<update id="updateBatch" parameterType="java.util.List">
update mydata_table
set status =
<foreach collection="list" item="item" index="index"
separator=" " open="case ID" close="end">
when #{item.id} then #{item.status}
</foreach>
where id in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.id}
</foreach>
</update>
Oracle批量insert/update/select写法:
//useGeneratedKeys="false" 要加上否则报错
<insert id="addBatch" useGeneratedKeys="false" parameterType="java.util.List">
insert into user(
id,
name,
sex)
//注意这里没有values
<foreach collection="list" item="item" index="index" separator="union all" >
//多了select from dual separator="union all"
( select
#{item.id}, #{item.name}, #{item.sex}
from dual )
</foreach>
</insert>
<update id="addBatch" parameterType="java.util.List">
begin
<foreach collection="list" item="item" separator=";" >
update user
<set>
<if test="item.name != null and item.name != ''">
name = #{item.name}
</if>
<if test="item.sex != null and item.sex != ''">
sex = #{item.sex}
</if>
</set>
where id = #{item.id}
</foreach>
;end;
</update>
- 上一篇: mybatis调用流程
- 下一篇: 一文带你入门 MyBatis
猜你喜欢
- 2024-11-21 MyBatis详解(二)
- 2024-11-21 想要开发中灵活的使用Mybatis?精通结果映射,你准了吗?
- 2024-11-21 小学妹问:Mybatis常见注解有哪些?
- 2024-11-21 重学Mybatis(二)-------主键自增 (含面试题)
- 2024-11-21 Mybatis入门
- 2024-11-21 重学Mybatis(五)-------分页 (含面试题)
- 2024-11-21 一、手写mybatis框架
- 2024-11-21 MyBatis中的翻页
- 2024-11-21 Mybatis的基础和高级查询应用实践
- 2024-11-21 看完这一篇学会MyBatis就够了
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 510℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 486℃MySQL service启动脚本浅析(r12笔记第59天)
- 466℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 464℃启用MySQL查询缓存(mysql8.0查询缓存)
- 444℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 423℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 420℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)