<1>
<select id="selectUserByConditions" parameterType="user" resultType="user">
SELECT * FROM USER WHERE =
<if test="id != null and id!=''">
and id =#{id}
</if>
<if test="id != null and id!=''">
id =#{id}
</if>
<if test="email != null and email !=''">
and email =#{email}
</if>
<if test="lastName != null and lastName !=''">
and last_name =#{lastName}
</if>
<!-- ognl会进行数字和字符串之间的转换判断""== -->
<if test="gender == 1 or gender == 0">
and gender =#{gender}
</if>
</select>

<2>

<select id="selectUserByConditions" parameterType="user" resultType="user">
SELECT * FROM USER
<!-- where只会去掉第一个and ,如果and在后面的话就会出问题如下     
<if test="id != null and id!=''">
id =#{id} and
</if>
<if test="email != null and email !=''">
email =#{email} and
</if>
<if test="lastName != null and lastName !=''">
last_name =#{lastName} and
</if>
<!-- ognl会进行数字和字符串之间的转换判断""== -->
<if test="gender == 1 or gender == 0">
gender =#{gender}
</if>
    -->
<where>
<if test="id != null and id!=''">
id =#{id}
</if>
<if test="email != null and email !=''">
and email =#{email}
</if>
<if test="lastName != null and lastName !=''">
and last_name =#{lastName}
</if>
<!-- ognl会进行数字和字符串之间的转换判断""== -->
<if test="gender == 1 or gender == 0">
and gender =#{gender}
</if>
</where>
</select>

<3>前后缀解决

后缀解决
<select id="selectUserByConditions" parameterType="user"
resultType="user">
SELECT * FROM USER
<!-- 后面多出的and或者or where 不能解决
prefix 前缀
prefixOverrides:前缀覆盖,去掉字符串前面多余的字符
suffix:后缀 给拼装的字符串添加一个后缀
suffixOverrides:后缀覆盖
-->
<trim prefix="where" suffixOverrides="and">
<if test="id != null and id!=''">
id =#{id} and
</if>
<if test="email != null and email !=''">
email =#{email} and
</if>
<if test="lastName != null and lastName !=''">
last_name =#{lastName} and
</if>
<!-- ognl会进行数字和字符串之间的转换判断""== -->
<if test="gender == 1 or gender == 0">
gender =#{gender}
</if>
</trim>
</select>
  前缀解决
<select id="selectUserByCons" parameterType="user"
resultType="user">
SELECT * FROM USER
<!-- where只会去掉第一个and -->
<trim prefix="where" prefixOverrides="and">
<if test="id != null and id!=''">
and id =#{id}
</if>
<if test="email != null and email !=''">
and email =#{email}
</if>
<if test="lastName != null and lastName !=''">
and last_name =#{lastName}
</if>
<!-- ognl会进行数字和字符串之间的转换判断""== -->
<if test="gender == 1 or gender == 0">
and gender =#{gender}
</if>
</trim>
</select>

<4>

<select id="selectUserByCons" parameterType="user"
resultType="user">
SELECT * FROM USER
<where>
<!-- 只选择其中的一个条件查 -->
<choose>
<when test="id != null and id!=''">
id =#{id}
</when>
<when test="email != null and email !=''">
and email =#{email}
</when>
<when test="lastName != null and lastName !=''">
and last_name =#{lastName}
</when>
<otherwise>
=
</otherwise>
</choose>
</where>
</select>

<5>更新

<update id="updateUserById" parameterType="model.User">
UPDATE user
<set>
<if test="email != null and email !=''">
email =#{email},
</if>
<if test="lastName != null and lastName !=''">
last_name =#{lastName},
</if>
<!-- ognl会进行数字和字符串之间的转换判断""== -->
<if test="gender == 1 or gender == 0">
gender =#{gender}
</if>
</set>
WHERE id=#{id}
</update>

<6>批量查询

<select id="selectUserByIds" resultType="user">
SELECT * FROM USER where id
in
<!-- foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置, 遍历list的时候index就是list的索引,item就是当前值
遍历map时,index就是key,item当前值
open表示该语句以什么开始, separator表示在每次进行迭代之间以什么符号作为分隔 符,
close表示以什么结束。
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,
在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,
所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key. -->
       public List<User> selectUserByIds(@Param("ids")List ids);对应collection="ids"
<foreach collection="list" item="item_id" open="(" separator=","
close=")">
#{item_id}
</foreach>
</select>

<7>批量保存

mysql下

<!--  public void addDepts(@Param("emps") List<Dept> emps); -->

<!-- MySQL下批量保存,可以foreach遍历 mysql支持values(),(),()语法 --> //推荐使用
<insert id="addEmpsBatch">
INSERT INTO emp(ename,gender,email,did)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>
<!-- 这种方式需要数据库连接属性allowMutiQueries=true的支持 -->  //在jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
<!-- <insert id="addEmpsBatch"> 后加上allowMultiQueries=true
<foreach collection="emps" item="emp" separator=";"> 表示可以多次执行insert into语句,中间;不会错
INSERT INTO emp(ename,gender,email,did)
VALUES(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert> -->

批量batch保存mybatis ExecutorType.BATCH

Mybatis内置的ExecutorType有3种,默认为simple,该模式下它为每个语句的执行创建一个新的预处理语句,单条提交sql;而batch模式重复使用已经预处理的语句,并且批量执行所有更新语句,

显然batch性能将更优; 但batch模式也有自己的问题,比如在Insert操作时,在事务没有提交之前,是没有办法获取到自增的id,这在某型情形下是不符合业务要求的

Oracle下批量保存

  方法1,将多个insert放在begin和end之间执行 
  begin
  insert into user(userno,username,email) values (seq_user.nextval,'','aaaaa');
  insert into user(userno,username,email) values (seq_user.nextval,'','bbbbbb');
end;
 方法2利用中间表
  insert into user(userno,username,email) 
        select seq_user.nextval,username,email from(
            select '' username ,'cccccc' email from dual
            union
            select '' username,'ddddddddddd' email from dual
            union
            select '' username,'eeeeeee' email from dual
         )
-->  <!--示例-->
<!--  public void addUsers(@Param("users") List<User> users);-->   
  <insert id = "addUsers"databaseId="oracle"  parameterType="user">   
  <!--Oracle:批量保存方法1 -->      
  <foreach collection = "users" item="user" open="begin" close="end;">          
insert into user(userno,username,email) 
        values (seq_user.nextval,#{user.username},#{user.email});
</foreach> <!--
  <!-- Oracle:批量保存方法2  -->
insert into user(userno,username,email) 
     select seq_user.nextval,username,email from(
          <foreach collection="users" item="user" separator="union" >
                   select #{user.username} username ,#{user.email} email from dual
            </foreach>
        )
  -->
</insert>

<8>两个内置参数  

    <!-- mybatis的 两个内置参数,_parameter 代表整个参数 单个参数,_parameter 就是这个参数 多个参数 会被封装成一个map,_parameter就是代表这个map
_databaseId 如果配置了databaseIdProvider标签, _databaseId就是代表当前数据库的别名,mysql或者oracle -->
<select id="selectUserByDataBaseId" resultType="user">
    <bind name="nameLike" value="'%' + lastName + '%'"/>
<if test="_databaseId=='mysql'">
select * from user
<if test="_parameter!=null">
where last_name=#{_parameter.lastName}
</if>
</if>
<if test="_databaseId=='oracle'">
select * from user where last_name = #{nameLike}
</if>
</select>

<9>sql片段

<sql id="userColumn">
id,email,last_name,gender
</sql>
<select id="selectUser" resultType="user">
select
<include refid="userColumn"/>
from user where id=#{id}
</select>

2.

<sql id="userColumn">
id,email,last_name,gender,${AA}
</sql>
<select id="selectUser" resultType="user">
select
<include refid="userColumn">
<property name="AA" value="depart_id"/>
</include>
from user where id=#{id}
</select>

mybatis四(动态sql)的更多相关文章

  1. 一分钟带你了解下MyBatis的动态SQL!

    MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格.列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下. 一. ...

  2. MyBatis的动态SQL详解

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

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

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

  4. mybatis 使用动态SQL

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

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

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

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

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

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

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

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

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

  9. mybatis.5.动态SQL

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

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

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

随机推荐

  1. css居中方法小结

    水平居中 行内元素 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的. 块状元素 当被设置元素为 块状元素 时用 text-align: ...

  2. S型顺序遍历二叉树(c++实现)

    //1.s型顺序访问二叉树,默认先左后右:利用两个栈来实现:如果先右后左的话,改变一下入栈的顺序就行 //2.注意s1 s2插入栈的顺序是不同的 void S_LevelOrderPrint(Tree ...

  3. [转][Oracle]常见报错及处理

    IIS 在安装 Oracle Client 后,需要命令行执行: iisreset 1.ORA-00257 参考自:https://jingyan.baidu.com/article/f71d6037 ...

  4. jquery插件:textarea的字数提示、textBox的文字提示

    引用文件:    <script src=”/TextTip/TextTip.js” type=”text/javascript”></script> 一.textarea的字 ...

  5. php的方法

    字符串: strlen(): 字符串的个数: . : 通过.来进行字符串的拼接: 数组: count(): 数组的 个数: is_array(): 判断是不是一个数组: 实参的方法: func_num ...

  6. 集合--(List、Set、Map)遍历、删除、比较元素时的小陷阱

    6,Map集合遍历的4中方法? 5,List遍历时如何remove元素 4.漏网之鱼-for循环递增下标方式遍历集合,并删除元素 如果你用for循环递增下标方式遍历集合,在遍历过程中删除元素,你可能会 ...

  7. Android毛玻璃模糊化效果处理

    三种方法 第一种:比较简单,性能比较低 /**     * 通过调用系统高斯模糊api的方法模糊     *     * @param bitmap  source bitmap     * @par ...

  8. Redis 主从+哨兵+监控 (centos7.2 + redis 3.2.9 )

    环境准备: 192.168.0.2  redis01 主 192.168.0.3  redis02 从 192.168.0.4  redis03 从 Redis 主从搭建 一:下载并安装redis软件 ...

  9. 怎样找到微信小程序功能呢?

    怎样找到微信小程序功能呢? 17年1月9号这个功能刚推出时,非常火,但我在微信中迟迟找不到微信小程序功能,微信明明更新到了最新版也没有:后来经过以下步骤才找到这个功能! 1.微信版本不能过低,这个一般 ...

  10. RabbitMQ install (Ubuntu)

    1. key 1) Online way apt-key adv --keyserver "hkps.pool.sks-keyservers.net" --recv-keys &q ...