ibatis 3 学习笔记 5
原创 ibatis 3 学习笔记 5 收藏
动态sql语句
可以在xml文件中添加条件配置来动态拼接,调用sql语句
ibatis使用的ONGL表达式有四种元素
if
choose
trim
foreach
if
<select id=”findActiveBlogLike”parameterType=”Blog” resultType=”Blog”>SELECT * FROM BLOG WHERE state = ‘ACTIVE’<if test=”title != null”>AND title like #{title}if><if test=”author != null && author.name != null”>AND title like #{author.name}if>select><select id=”findActiveBlogLike”parameterType=”Blog” resultType=”Blog”> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test=”title != null”> AND title like #{title} when> <when test=”author != null && author.name != null”> AND title like #{author.name} when> <otherwise> AND featured = 1 otherwise> choose>select><select id=”findActiveBlogLike”parameterType=”Blog” 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 && author.name != null”> AND title like #{author.name} if> where>select><trim prefix="WHERE" prefixOverrides="AND |OR ">…trim>
<update id="updateAuthorIfNecessary"parameterType="domain.blog.Author">update Author<set><if test="username != null">username=#{username},if><if test="password != null">password=#{password},if><if test="email != null">email=#{email},if><if test="bio != null">bio=#{bio}if>set>where id=#{id}update><trim prefix="SET" suffixOverrides=",">…trim>
<select id="selectPostIn" resultType="domain.blog.Post">SELECT *FROM POST PWHERE ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}foreach>select>