已解决
Java --- Mybatis的动态sql标签
来自网友在路上 146846提问 提问时间:2023-11-09 08:43:51阅读次数: 46
最佳答案 问答题库468位专家为你答疑解惑
一、if标签
<select id="queryEmpByCondition" resultType="User">select * from t_user where 1=1<if test="username != null and username != ''">and username = #{username}</if></select>
if:根据标签中的test属性内容条件决定是否拼接到sql中
@Testpublic void test3(){SqlSession sqlSession = SqlSessionUtils.sqlSession();DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);User admin = new User(null, "admin", null, null, null, null);List<User> users = mapper.queryEmpByCondition(admin);System.out.println(users);}
二、where标签
条件内容前的and或or
select id="queryEmpByCondition" resultType="User">select * from t_user<where><if test="username != null and username != ''">and username = #{username}</if></where></select>
条件内容后的and或or
<select id="queryEmpByCondition" resultType="User">select * from t_user<where><if test="username != null and username != ''">username = #{username} and</if></where></select>
where:当where标签中有内容会生成where关键字,没有内容将不会产生任何效果,并且会将条件内容前的and或or去掉,但条件内容后的依然会生效
三、trim标签
<select id="queryEmpByCondition" resultType="User">select * from t_user<trim prefix="where" suffixOverrides="and|or" ><if test="username != null and username != ''">username = #{username} and</if></trim></select>
trim:
标签中有内容
prefix="" | suffix="" :将trim标签中的内容前面或后面添加指定内容
prefixOverrides="" | suffixOverrides="" :将trim标签中内容前面或后面去掉指定内容
标签中没有内容则trim标签不生效
四、choose、when、otherwise标签
<select id="queryEmpByCondition" resultType="User">select * from t_user<trim prefix="where" suffixOverrides="and|or" ><choose><when test="username != null and username != ''">username = #{username}</when><when test="password != null and password != ''">password = #{password}</when><otherwise>id = 1</otherwise></choose></trim></select>
相当于if...else if...else
五、foreach标签
批量删除:
形式一
<mapper namespace="com.cjc.ssm.mapper.DynamicSQLMapper"><delete id="removeByUserArray">delete from t_user where id in<foreach collection="arr" item="id" separator="," open="(" close=")">#{id}</foreach></delete>
形式二
<delete id="removeByUserArray">delete from t_user where<foreach collection="arr" item="id" separator="or">id = #{id}</foreach></delete>
批量添加
<insert id="addUserList">insert into t_user values<foreach collection="list" item="user" separator=",">(null,#{user.username},#{user.password},#{user.age},#{user.sex},#{user.email})</foreach></insert>
foreach:
collection:设置需要循环的数组或集合
item:数组或集合中的每一个数据
separator:循环体之间的分割符
open:所有循环内容的开始符
close:所有循环内容的结束符
六、sql标签
查看全文
99%的人还看了
相似问题
- 【PyQt小知识 - 3】: QComboBox下拉框内容的设置和更新、默认值的设置、值和下标的获取
- VR全景打造亮眼吸睛创意内容:三维模型、实景建模
- 广州华锐互动VRAR | VR课件内容编辑器解决院校实践教学难题
- PyCharm:PyCharm新建.py文件时自动带出指定内容
- EI论文程序:Adaboost-BP神经网络的回归预测算法,可作为深度学习对比预测模型,丰富实验内容,自带数据集,直接运行!
- 信息中心网络提出的背景、研究现状及研究内容
- 【IDEA 使用easyAPI、easyYapi、Apifox helper等插件时,导出接口文档缺少代码字段注释的相关内容、校验规则的解决方法】
- 340条样本就能让GPT-4崩溃,输出有害内容高达95%?OpenAI的安全防护措施再次失效
- 控制您的音乐、视频等媒体内容
- el-table固定表头(设置height)出现内容过多时不能滚动问题
猜你感兴趣
版权申明
本文"Java --- Mybatis的动态sql标签":http://eshow365.cn/6-36046-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 第二十三节——路由懒加载+动态路由
- 下一篇: 现一个智能的SQL编辑器