MyBatis---sql片段
2021/12/5 2:18:25
本文主要是介绍MyBatis---sql片段,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Sql片段
1.目标
Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。我们先到 UserDao.xml 文件中使用标签,定义出公共部分.
2.讲解
- 使用sql标签抽取
<!-- 使用sql标签将重复的sql语句部分封装起来 在需要使用这个sql片段的地方,就用include标签引入就行了 --> <sql id="select_all"> select uid,username,sex,address,birthday from t_user </sql>
- 使用include标签引入使用
<select id="findUserListByAddress" parameterType="string" resultType="User"> <include refid="select_all"/> <!-- 加入一个判断,判断传入的address是否为空,使用if标签进行判断,该标签中的test属性就编写判断条件 --> <if test="address != null"> where address=#{address} </if> </select> <select id="findUserListByAddressAndSex" parameterType="User" resultType="User"> <include refid="select_all"/> <!-- where标签的作用: 1. 可以在条件之前添加where关键字 2. 可以去掉第一个条件前的and --> <where> <if test="address != null"> and address=#{address} </if> <if test="sex != null"> and sex=#{sex} </if> </where> </select>
3.小结
- sql标签可以把公共的sql语句进行抽取, 再使用include标签引入. 好处:好维护, 提示效率
这篇关于MyBatis---sql片段的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求