mybatis foreach标签的解释
2015-06-03 13:39
267 查看
情景:查询数据库中文章的相关文章 文章为一个表 字段tags为相关文章字符串中间用','逗号进行啦分割
查询完一个文章后可以把tags字段构造为一个List<String> 然后利用这个集合作为条件来查询
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where
<foreach collection="listTag" index="index" item="tag" open="("
separator="," close=")">
#{tag} in n.tags
</foreach>
看。 foreach 生成的效果是集合 转化为下面的
select * from t_news n where ( ? in n.tags , ? in n.tags )
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名.
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.
open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.
close表示以什么结束.
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where
<foreach collection="listTag" index="index" item="tag" open=""
separator="or" close="">
#{tag} in n.tags
</foreach>
</select>
所以 去除左右括号 和 把,改成 or 进行 。 就可以转化为这种形式。
select * from t_news n where ? in n.tags or ? in n.tags 这样可以用List<String> 来查。
但是查不到数据
需要使用如下方式:
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where
<foreach collection="listTag" index="index" item="tag" open=""
separator="or" close="">
n.tags like '%'||#{tag}||'%'
</foreach>
<lect>
生成的SQL为
select * from t_news n where n.tags like ? or n.tags like ?
还可以使用如下方式
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where n.tags in
<foreach item="item" index="index" collection="listTag" open="(" separator="," close=")">
#{item}
</foreach>
</select>
生成的SQL为
select * from t_news n where n.tags in(?,?,?)
查询完一个文章后可以把tags字段构造为一个List<String> 然后利用这个集合作为条件来查询
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where
<foreach collection="listTag" index="index" item="tag" open="("
separator="," close=")">
#{tag} in n.tags
</foreach>
看。 foreach 生成的效果是集合 转化为下面的
select * from t_news n where ( ? in n.tags , ? in n.tags )
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名.
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.
open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.
close表示以什么结束.
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where
<foreach collection="listTag" index="index" item="tag" open=""
separator="or" close="">
#{tag} in n.tags
</foreach>
</select>
所以 去除左右括号 和 把,改成 or 进行 。 就可以转化为这种形式。
select * from t_news n where ? in n.tags or ? in n.tags 这样可以用List<String> 来查。
但是查不到数据
需要使用如下方式:
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where
<foreach collection="listTag" index="index" item="tag" open=""
separator="or" close="">
n.tags like '%'||#{tag}||'%'
</foreach>
<lect>
生成的SQL为
select * from t_news n where n.tags like ? or n.tags like ?
还可以使用如下方式
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
select * from t_news n where n.tags in
<foreach item="item" index="index" collection="listTag" open="(" separator="," close=")">
#{item}
</foreach>
</select>
生成的SQL为
select * from t_news n where n.tags in(?,?,?)
相关文章推荐
- XenDesktop 7.5VDI无法应用自动锁屏策略在虚拟桌面中不生效,RDP下OK
- 从中外用户习惯,看图片社交的痛点和商业化路径
- SAT阅读题型该如何备考
- [Lab 0]Cisco network Assistant 实验题
- keras学习(2)
- JVM的内存区域划分
- iOS App升级安装 - CoreData数据库升级
- jquery页面刷新
- Libpcap 总结
- css全局样式表
- Hibernat缓存的作用
- 如何通过svn,git进行优雅的代码集成
- iOS开发UI基础—25UITableview控件使用小结
- 1个税务问题,可能要搞死90%的创业团队!
- 每日一题C
- this关键字
- Tomcat默认打开项目设置
- 第7章
- iOS开发UI基础—24使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- 【数论】无平方因子的数