mybatis的动态sql

案例一:

insert语句,然后获取这条语句的id值.

1 <insert id="insertBook" parameterType="modle.Book" keyProperty="id">  
2     <selectKey keyProperty="id" resultType="String" order="BEFORE">  
3         select nextval('book')  
4     </selectKey>  
5    insert into book
6 (bookname,author,isbn,price,typeid,publishDate)
7 values
8 (#{bookname},#{author},#{isbn},#{price},#{typeid},#{publishDate})
9 </insert>  

selectKey语句属性:

keyProperty selectKey 语句生成结果需要设置的属性。  
resultType 生成结果类型,MyBatis 允许使用基本的数据类型,包括String、int类型。  
order

1:BEFORE,会先选择主键,然后设置keyProperty,再执行insert语句;

2:AFTER,就先运行insert语句再运行selectKey 语句。

BEFORE

AFTER

statementType MyBatis 支持STATEMENT,PREPARED和CALLABLE的语句形式, 对应Statement,PreparedStatement 和CallableStatement响应  

STATEMENT

PREPARED

CALLABLE

if标签:

1 <select id="limitBook" parameterType="Map" resultType="Book">
2     select * from book where         
3     <if test="id !=null and id != ''  "> id=#{id}</if>
4     <if test="bookname !=null and bookname !='' ">
5         and bookname like #{bookname}
6     </if>            
7 </select>

当id不为空或''时, 就会出现这样的sql语句:select * from book where and bookname like #{bookname}

很明显多了个and,

where - if解决上面的问题

1 <select id="limitBook" parameterType="Map" resultType="Book">
2     select * from book 
3     <where>
4         <if test="id !=null and id != ''  "> id=#{id}</if>
5         <if test="bookname !=null and bookname !='' ">
6             and bookname like #{bookname}
7         </if>
8     </where>        
9 </select>

这样不论id判断如何,都不会出现多了一个and的情况

根据以上的情况引出  if-set

set标签可以将动态的配置SET关键字,和剔除追加到条件末尾的任何不相关的逗号

 1     <!-- 4 if/set(判断参数) - 将实体类不为空的属性更新 -->  
 2     <update id="updateBook" parameterType="Book">  
 3         update book  
 4         <set>  
 5             <if test="bookname != null and bookname!= '' ">  
 6                 bookname = #{bookname},  
 7             </if>  
 8             <if test="author != null and author != '' ">  
 9                 author  = #{author },  
10             </if>  
11             <if test="isbn!= null ">  
12                 isbn= #{isbn},  
13             </if>  
14         </set>  
15         WHERE id= #{id};      
16     </update>  

  使用if+set标签修改后,如果某项为null则不进行更新,而是保持数据库原值

if + trim代替where/set标签

trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

trim代替where

1 <select id="limitBook" parameterType="Map" resultType="Book">
2         select * from book 
3         <trim prefix="where" prefixOverrides="AND|OR">
4             <if test="id !=null and id != ''  "> id=#{id}</if>
5             <if test="bookname !=null and bookname !='' ">
6                 and bookname like #{bookname}
7             </if>
8         </trim>        
9     </select>

trim代替set

 1 <!-- 4 if/set(判断参数) - 将实体类不为空的属性更新 -->  
 2     <update id="updateBook" parameterType="Book">  
 3         update book  
 4         <trim prefix="SET" suffixOverrides=",">  
 5             <if test="bookname != null and bookname!= '' ">  
 6                 bookname = #{bookname},  
 7             </if>  
 8             <if test="author != null and author != '' ">  
 9                 author  = #{author },  
10             </if>  
11             <if test="isbn!= null ">  
12                 isbn= #{isbn},  
13             </if>  
14         </trim>  
15         WHERE id= #{id};      
16     </update>

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行otherwise中的sql。

 1 <select id="limitBook" parameterType="Map" resultType="Book">
 2         select * from book 
 3         <where>
 4             <choose>
 5                 <when test="id !=null">
 6                     id=#{id}
 7                 </when>
 8                 <when test="bookname != null and bookname!='' ">
 9                     and bookname like #{bookname}
10                 </when>
11                 <otherwise>  
12                 
13                 </otherwise> 
14             </choose>
15                         
16         </where>        
17     </select>

foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN条件。List 实例将使用“list”做为键,数组实例以“array”做为键。

foreach元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

注意:你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。

这个部分是对关于XML配置文件和XML映射文件的而讨论的。下一部分将详细讨论Java API,所以你可以得到你已经创建的最有效的映射。

array参数

1 <!— 7.1 foreach(循环array参数) - 作为where中in的条件 -->  
2 <select id="limitBook" resultMap="resultMap_studentEntity">  
3     select * from book       
4      where id in
5      <foreach collection="array" item="classIds"  open="(" separator="," close=")">  
6         #{classIds}  
7      </foreach>  
8 </select>

list参数:

1 <!-- 7.2 foreach(循环List<String>参数) - 作为where中in的条件 -->  
2  <select id="limitBook" resultMap="resultMap_studentEntity">  
3      select * from book       
4       where id in
5       <foreach collection="list" item="idList"  open="(" separator="," close=")">  
6          #{idList}  
7       </foreach>  
8  </select>