参数为String,if test读取该参数代码
				?
			
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
						 | <selectid="getMaxDepartId"parameterType="java.lang.String"resultType="java.lang.String">SELECTMAX(DEPART_ID) FROMT_P_DEPART <where><if test="_parameter!=null and _parameter!=''"> ANDDEPART_PID = #{departId,jdbcType=VARCHAR} </if><if test="_parameter==null or _parameter==''"> ANDDEPART_PID ISNULL</if></where></select> | 
参数为pojo , if test读取该参数代码
				?
			
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
						 | <selectid="findShopByName"parameterType="ShopVo"resultType="ShopCustomer">select* fromshop <where><if test="shopCustomer.shopname!=null and shopCustomer.shopname!=''">shop.shopname like'%${shopCustomer.shopname}%'</if><if test="shopCustomer.shopname==null or shopCustomer.shopname==''"> ANDshop.shopname isnull</if></where></select> | 
1.mybatis 中 的 if test写法
1.1官方文档上对于if是这么写的
				?
			
| 
								1
 
								2
 
								3
						 | <if test="title != null">ANDtitle like#{title}</if> | 
参考官方文档:
实际项目中会有这种情况: 页面上title字段输入某个值进行查询,手动将输入框中的值删除,然后再次查询,发现结果不正确,究其原因是应为title传入了空串" " 这样在mybatis配置文件中就会用空串进行查询,导致出现错误结果
1.2建议写法
				?
			
                	
    
	
	
		
		
	
 
	
		
			
	
	 
     
	
			
                 
			
		
		
			
			
			
    
        
        
	
			
						
			
            			
    		
    		
		
	    
    	
    	
        
    	
    
| 
								1
 
								2
 
								3
						 | <if test="title != null and title != ''">ANDtitle like#{title}</if> | 
2.使用mybatis 做修改时将字段置空
解决办法:
				?
			
	
						
						
						
						
						
						
						
																		
    
        
    
        
                        
                
                    
                
                
                
                    
                
                
                
                    
                
                
                
                    
                
                        
    
 																		
						
																		
    
        
 												
						
																		
	
	
		
				
			
																		
						
						
					
				
				                | 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
						 | <updateid="updateObject"parameterType="*.*.Object">updatetable<set><if test="Object.fullName == null or Object.fullName ==''">full_name = null,</if><if test="Object.fullName != null and Object.fullName !=''">full_name = #{companyOrg.fullName},</if><if test="Object.level == null or Object.level ==''">level= null,</if><if test="Object.level == 0 ">level= null,</if><if test="Object.level != null and Object.level !='' and Object.level != 0 ">level= #{companyOrg.level},</if></set>where1=1 andid =#{companyOrg.id}</update> | 
以上为个人经验,希望能给大家一个参考,也希望大家多多支持快网idc。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/a241903820/article/details/51607131
相关文章
             猜你喜欢
        
        - 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 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-25 22
- 
            2025-06-04 30
- 
            2025-05-27 37
- 
            2025-05-29 61
- 
            2025-06-04 69
		热门评论
	
	 
        
 
    		 
            	 
															 
         
        
 
                        