我就废话不多说了,大家还是直接看代码吧~
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!-- 查询物品的id -->
<select id= "checkItemsId" parameterType= "pd" resultType= "java.lang.Integer" >
SELECT
i.itemsid
FROM pq_goods_items i
<where>
<!--方式一使用choose的方式查询-->
<!-- <choose>
<when test= "parentId !=0 " >parentTypeId=#{parentId}</when>
<when test= "parentId==0" >parentTypeId is null </when>
</choose> -->
<!--方式二使用 if 的方式查询-->
< if test= "color!=null" >
i.personone=#{personone}
AND i.persontwo=#{persontwo}
AND i.color=#{color}
</ if >
< if test= "color==null" >
i.personone=#{personone}
AND i.persontwo=#{persontwo}
AND i.color is null
</ if >
</where>
</select>
|
需要注意的是 使用了where标签以后,sql中不在使用where字段来限制条件
如果判断条件有多个 中间用 and 表示并列
?
1
|
< if test= "color!=null and personone!=null" >
|
补充:mybaits中if 多个test 和 if else 分支支持
mybaits中if 多个test
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<select id= "selectByDynamicallyWithPage" parameterType= "map" resultMap= "BaseResultMap" >
select
<include refid= "Base_Column_List" />
from gene_polymorphism
<where>
diag_id = #{conds.diagId,jdbcType=INTEGER}
< if test= "conds.chromesome!=null and conds.chromesome!=''" >
and chromesome = #{conds.chromesome,jdbcType=VARCHAR}
</ if >
< if test= "conds.startPos!=null" >
and start_pos >= #{conds.startPos,jdbcType=BIGINT}
</ if >
</where>
</select>
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<select id= "selectByDynamicallyWithPage" parameterType= "map" resultMap= "BaseResultMap" >
select
<include refid= "Base_Column_List" />
from gene_polymorphism
<where>
diag_id = #{conds.diagId,jdbcType=INTEGER}
< if test= "conds.chromesome!=null" >
and chromesome = #{conds.chromesome,jdbcType=VARCHAR}
</ if >
< if test= "conds.startPos!=null" >
and start_pos >= #{conds.startPos,jdbcType=BIGINT}
</ if >
< if test= "conds.endPos!=null" >
and end_pos <= #{conds.endPos,jdbcType=BIGINT}
</ if >
< if test= "conds.geneTypes!=null" >
<!--and gene_type in-->
<!--<foreach collection= "conds.geneTypes" open= "(" close= ")" item= "item" separator= "," >-->
<!--#{item,jdbcType=VARCHAR}-->
<!--</foreach>-->
and (
<foreach collection= "conds.geneTypes" item= "item" separator= "or" >
gene_type like CONCAT( '%' ,CONCAT(#{item,jdbcType=VARCHAR}, '%' ))
</foreach>
)
</ if >
< if test= "conds.geneChange!=null" >
and gene_change like CONCAT( '%' ,CONCAT(#{conds.geneChange,jdbcType=VARCHAR}, '%' ))
</ if >
</where>
order by
<trim suffixOverrides= "," >
<choose>
<when test= "conds.chromesomeSort!=null and conds.chromesomeSort=='asc'" >
chromesome asc ,
</when>
<when test= "conds.chromesomeSort!=null and conds.chromesomeSort=='desc'" >
chromesome desc ,
</when>
</choose>
<choose>
<when test= "conds.startPosSort!=null and conds.startPosSort=='asc'" >
start_pos asc ,
</when>
<when test= "conds.startPosSort!=null and conds.startPosSort=='desc'" >
start_pos desc ,
</when>
<otherwise>
id desc
</otherwise>
</choose>
</trim>
limit #{startRow,jdbcType=INTEGER} ,#{pageSize,jdbcType=INTEGER}
<!-- order by id desc limit #{startRow,jdbcType=INTEGER} ,#{pageSize,jdbcType=INTEGER} -->
</select>
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持快网idc。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/pqsas_com/article/details/75337608
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 39
-
2025-06-04 101
-
2025-05-24 49
-
2025-05-25 61
-
2025-05-27 75
热门评论