案例一:

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

 <insert id="insertBook" parameterType="modle.Book" keyProperty="id">
<selectKey keyProperty="id" resultType="String" order="BEFORE">
select nextval('book')
</selectKey>
insert into book
(bookname,author,isbn,price,typeid,publishDate)
values
(#{bookname},#{author},#{isbn},#{price},#{typeid},#{publishDate})
</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标签:

 <select id="limitBook" parameterType="Map" resultType="Book">
select * from book where
<if test="id !=null and id != '' "> id=#{id}</if>
<if test="bookname !=null and bookname !='' ">
and bookname like #{bookname}
</if>
</select>

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

很明显多了个and,

where - if解决上面的问题

 <select id="limitBook" parameterType="Map" resultType="Book">
select * from book
<where>
<if test="id !=null and id != '' "> id=#{id}</if>
<if test="bookname !=null and bookname !='' ">
and bookname like #{bookname}
</if>
</where>
</select>

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

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

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

     <!-- 4 if/set(判断参数) - 将实体类不为空的属性更新 -->
<update id="updateBook" parameterType="Book">
update book
<set>
<if test="bookname != null and bookname!= '' ">
bookname = #{bookname},
</if>
<if test="author != null and author != '' ">
author = #{author },
</if>
<if test="isbn!= null ">
isbn= #{isbn},
</if>
</set>
WHERE id= #{id};
</update>

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

if + trim代替where/set标签

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

trim代替where

 <select id="limitBook" parameterType="Map" resultType="Book">
select * from book
<trim prefix="where" prefixOverrides="AND|OR">
<if test="id !=null and id != '' "> id=#{id}</if>
<if test="bookname !=null and bookname !='' ">
and bookname like #{bookname}
</if>
</trim>
</select>

trim代替set

 <!-- 4 if/set(判断参数) - 将实体类不为空的属性更新 -->
<update id="updateBook" parameterType="Book">
update book
<trim prefix="SET" suffixOverrides=",">
<if test="bookname != null and bookname!= '' ">
bookname = #{bookname},
</if>
<if test="author != null and author != '' ">
author = #{author },
</if>
<if test="isbn!= null ">
isbn= #{isbn},
</if>
</trim>
WHERE id= #{id};
</update>

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

 <select id="limitBook" parameterType="Map" resultType="Book">
select * from book
<where>
<choose>
<when test="id !=null">
id=#{id}
</when>
<when test="bookname != null and bookname!='' ">
and bookname like #{bookname}
</when>
<otherwise> </otherwise>
</choose> </where>
</select>

foreach

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

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

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

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

array参数

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

list参数:

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

mybatis的动态sql的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  3. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  4. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  5. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  6. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  7. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  8. mybatis.5.动态SQL

    1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...

  9. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  10. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

随机推荐

  1. 虚拟化之kvm与xen对比

    xen XenServer is the leading open source virtualization platform, powered by the Xen Project hypervi ...

  2. CSS控制print打印样式

    来源:http://blog.csdn.net/pangni/article/details/6224533 一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示:   用于屏幕显 ...

  3. asp.net 程序,单击按钮时 同时实现打开页面并处理值

    来源:http://blog.csdn.net/nvhaixx/article/details/12430757 1)在网页中添加用于处理的客户端事件: <script language=&qu ...

  4. js给定时器调用传递参数

    给定时器调用传递参数 无论是window.setTimeout 还是window.setInterval,在使用函数名作为调用句柄时都不 能带参数,而在许多场合必需要带参数,这就需要想方法解决.例如对 ...

  5. IntelliJ IDEA常用快捷键

    双击shift 项目所有目录查找 ctrl+f 文件查找特定内容 ctrl+shift+f 项目查找包含特定内容的文件 ctrl+n 新建类 ctrl+shift+n 查找文件 ctrl+e  最近的 ...

  6. php 查询出来的字段名全是小写或者大写

    PHP PDO预定义常量 PDO::CASE_LOWER -- 强制列名是小写PDO::CASE_NATURAL -- 列名按照原始的方式PDO::CASE_UPPER -- 强制列名为大写 修改此参 ...

  7. Add 1G to a LVM on VMware

    1. update disk1 to 5G from 4G in vcenter2. echo 1 > /sys/block/sda/device/rescan3. fdisk /dev/sda ...

  8. Linux命令详解nice

    [命令]nice — 调整程序运行的优先级 [格式]nice [OPTION] [command [arguments...]] [说明] 在当前程序运行优先级基础之上调整指定值得到新的程序运行优先级 ...

  9. docker的例子

    定制镜像 做个测试服务器,testServer代码如下 package main import ( "net/http" ) func main() { http.Handle(& ...

  10. ASP.NET动态加载Js代码到Head标签中(三种方法)

    方法一代码如下: HtmlGenericControl Include2 = new HtmlGenericControl("script"); Include2.Attribut ...