mybatissql中的条件语句
<if test="type!=null and type!=''">
AND type = #{type}
</if>
2.Mybatis中的like查询
今天要做⼀个模糊查询
⽤的Mybatis
开始写的是:
select id,bookName,author,publisher,donor,status,createDate,lastUpdate from book
<where>
<if test="bookName!=null">
bookName like '%#{bookName}%'
</if>
<if test="author!=null">
and author like '%#{author}%'
</if>
最后改为:
select id,bookName,author,publisher,donor,status,createDate,lastUpdate from book
<where>
<if test="bookName!=null">
bookName like CONCAT('%','${bookName}','%' )
</if>
<if test="author!=null">
and author like CONCAT('%','${author}','%' )
</if>
主要还是MyBatis传值的问题啊
如果不是字符串就没法替换了
---------------------------------------------------------------------------
yBatis 的动态SQL 是基于OGNL 表达式的,它可以帮助我们⽅便的在SQL 语句中实现某些逻辑。
MyBatis 中⽤于实现动态SQL 的元素主要有:
if
choose (when ,otherwise )
trim
where
set
foreach
if
if 就是简单的条件判断,利⽤if 语句我们可以实现某些简单的条件选择。先来看如下⼀个例⼦:
delete from user
<where>
<if test="id != null">
and id= #{id}
</if>
<if test="code!= null">
and code= #{code}
</if>
</where>
if语句可以⽅便条件不同的SQL重⽤,但当where后⾯的if全部不满⾜时,就会导致空条件⽽删除或更新整个表。可以在语句⾥⾯加上这样⼀个语句,避免全表更新或者删除。
<if test="id == null and code = null">
and exception
</if>
<if test="id != null">
and id= #{id}
</if>
<if test="code!= null">
and code= #{code}
</if>
</where>
choose
choose 元素的作⽤就相当于 JAVA 中的 switch 语句,基本上跟 JSTL 中的 choose 的作⽤和⽤法是⼀样的,通常都是与 when 和 otherwise 搭配的。看如下⼀个例⼦:
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
when 元素表⽰当 when 中的条件满⾜的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满⾜的时候,就会跳出 choose ,即所有的 when 和 otherwise 条件中,只有⼀个会输出,当所有的我很条件都不满⾜的时候就输出otherwise 中的内容。
where
where 语句的作⽤主要是简化 SQL 语句中 where 中的条件判断的,先看⼀个例⼦
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</where>
where 元素的作⽤是会在写⼊ where 元素的地⽅输出⼀个 where ,另外⼀个好处是你不需要考虑 whe
re 元素⾥⾯的条件输出是什么样⼦的, MyBatis 会智能的帮你处理,如果所有的条件都不满⾜那么 MyBatis 就会查出所有的记录,如果输出后是 and 开头的, MyBatis 会把第⼀个 and 忽略,当然如果是 or 开头的, MyBatis 也会把它忽略;此外,在 where 元素中你不需要考虑空格的问题, MyBatis 会智能的帮你加上。
注意,当 Where 元素后⾯跟着 <foreach open="AND"> ,并不能去除 foreach 元素中的 AND ,这时可以⽤trim来去除。
trim
trim 元素的主要功能是可以在⾃⼰包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是 prefix 和 suffix ;可以把包含内容的⾸部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是 prefixOverrides 和 suffixOverrides ;正因为 trim 有这样的功能,所以我们也可以⾮常简单的利⽤ trim 来代替 where 元素的功能
<trim prefix="where" prefixOverrides="and">
......
</trim>
trim元素后⾯跟着 <foreach open="AND"> ,可以去除 foreach 元素中的 AND 。
<update id="dynamicSetTest" parameterType="Blog">
update t_blog
<trim prefix="set" suffixOverrides=",">
<if test="title != null">
title = #{title},
<if test="content != null">
content = #{content},
</if>
</trim>
where id = #{id}
</update>
set
set 元素主要是⽤在更新操作的时候,它的主要功能和 where 元素其实是差不多的,主要是在包含的语句前输出⼀个 set ,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果 set 包含的内容为空的话则会出错。有了 set 元素我们就可以动态的更新那些修改了的字段。下⾯是⼀段⽰例代码:
<update id="dynamicSetTest" parameterType="Blog">
update t_blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="content != null">
content = #{content},
</if>
</set>
where id = #{id}
</update>
foreach
foreach 的主要⽤在构建in 条件中,它可以在SQL 语句中进⾏迭代⼀个集合。foreach 元素的属性主要有item ,index ,collection ,open ,separator ,close 。item 表⽰集合中每⼀个元素进⾏迭代时的别名,index 指定⼀个名字,⽤于表⽰在迭代过程中,每次迭代到的位置,open 表⽰该语句以什么开始,separator 表⽰在每次进⾏迭代之间以什么符号作为分隔符,close 表⽰以什么结束,在使⽤foreach 的时候最关键的也是最容易出错的就是collection 属性,该属性是必须指定的,但是在不同情况下,该属性的值是不⼀样的,主要有⼀下4 种情况:
1、如果传⼊的是单参数且参数类型是⼀个List 的时候,collection 属性值为list
<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
要注意的是,⽆论你JAVA代码传⼊的list名字是什么,在collection中名字必须为list。
2、如果传⼊的参数是多个的时候,我们就需要把它们封装成⼀个Map 了,当然单参数也可以封装成map ,实际上如果你在传⼊参数的时候,在MyBatis ⾥⾯也是会把它封装成⼀个Map 的,map 的key 就是参数名,所以这个时候collection 属性值就是传⼊的List 或array 对象在⾃⼰封装的map ⾥⾯的key
<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
select count(*) from TEST test
<WHERE>
<if test="code != null">
AND test.CODE = #{code}
</if>
<if test="idList !=null ">
and test.id in(
<foreach item="guard" index="index" collection="idList" separator=",">
#{guard}
</foreach>
)
</if>
</WHERE>
</select>
3、 JAVA对象中的list。和map写法⼀样,只是传⼊参数时不同
<select id="exists" parameterType="TestDto" resultType="java.lang.Integer">
select count(*) from TEST test
<if test="code != null">
AND test.CODE = #{code}
</if>
<if test="idList !=null ">
and test.id in(
<foreach item="guard" index="index" collection="idList" separator=",">
sql语句替换表中内容#{guard}
</foreach>
)
</if>
</WHERE>
</select>
注意这⾥的parameterType如果是JAVA对象的话,必须先注⼊进去,或者加上包的路径。注⼊时配置如下:
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage"
value="st.dto/>
<property name="configLocation" value="l"/>
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表⽰在mapper包所有⽬录中,以-l结尾所有⽂件 -->
<value>classpath:/mapper/*/*l</value>
</list>
</property>
</bean>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论