zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Mybatis where 1=1 和 标签详解数据库

数据库mybatis 详解 标签 where
2023-06-13 09:20:10 时间
 select id="selSampleListByIDX4" resultMap="BaseResultMap" parameterType="cn.com.git.cbs.datamodel.TBL_Sample" 

 select 

 include refid="Base_Column_List" / 

 from SAMPLE 

 where 1=1 

 if test="samplenumber != null" 

 AND SAMPLENUMBER = #{samplenumber,jdbcType=DECIMAL} 

 /if 

 /select 

Mybatis  之前拼条件的时候 写法  where 1=1,也可以使用 where 标签

 select id="findActiveBlogLike" 

 resultType="Blog" 

 SELECT * FROM BLOG 

 where 

 if test="state != null" 

 state = #{state} 

 /if 

 if test="title != null" 

 AND title like #{title} 

 /if 

 if test="author != null and author.name != null" 

 AND author_name like #{author.name} 

 /if 

 /where 

 /select 

where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。

如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:

 trim prefix="WHERE" prefixOverrides="AND |OR " ... /trim 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/4382.html