MyBatis-动态SQL

• 动态 SQL是MyBatis强大特性之一。极大的简化我们拼装SQL的操作。

• 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似。

• MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作。

– if

– choose (when, otherwise)

– trim (where, set)

– foreach

 

if

 

choose (when, otherwise)

 

trim (where, set)

 

foreach

• 动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候。

• 当迭代列表、集合等可迭代对象或者数组时

– index是当前迭代的次数,item的值是本次迭代获取的元素

• 当使用字典(或者Map.Entry对象的集合)时

– index是键,item是值

 

<!-- 批量保存 -->

1)、MySQL下批量保存:

<!--public void addEmps(@Param("emps")List<Employee> emps);  -->

          <!--MySQL下批量保存:可以foreach遍历   mysql支持values(),(),()语法-->

         <insert id="addEmps">

                 insert into tbl_employee(

                          <include refid="insertColumn"></include>

                 )

                   values

                   <foreach collection="emps" item="emp" separator=",">

                            (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})

                   </foreach>

          </insert><!--   -->

          

          <!-- 这种方式需要数据库连接属性allowMultiQueries=true;

                 这种分号分隔多个sql可以用于其他的批量操作(删除,修改) -->

          <!-- <insert id="addEmps">

                 <foreach collection="emps" item="emp" separator=";">

                          insert into tbl_employee(last_name,email,gender,d_id)

                          values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})

                 </foreach>

          </insert> -->

2、Oracle数据库批量保存:

<!-- Oracle数据库批量保存:

                 Oracle不支持values(),(),()

                 Oracle支持的批量方式

                 1、多个insert放在begin - end里面

                          begin

                                insert into employees(employee_id,last_name,email)

                                values(employees_seq.nextval,'test_001','test_001@atguigu.com');

                                insert into employees(employee_id,last_name,email)

                                values(employees_seq.nextval,'test_002','test_002@atguigu.com');

                            end;

                   2、利用中间表:

                            insert into employees(employee_id,last_name,email)

                          select employees_seq.nextval,lastName,email from(

                                 select 'test_a_01' lastName,'test_a_e01' email from dual

                                 union

                                 select 'test_a_02' lastName,'test_a_e02' email from dual

                                 union

                                 select 'test_a_03' lastName,'test_a_e03' email from dual

                          )

          -->

          <insert id="addEmps" databaseId="oracle">

                 <!-- oracle第一种批量方式 -->

                 <!-- <foreach collection="emps" item="emp" open="begin" close="end;">

                          insert into employees(employee_id,last_name,email)

                                values(employees_seq.nextval,#{emp.lastName},#{emp.email});

                 </foreach> -->

                

                 <!-- oracle第二种批量方式  -->

                 insert into employees(

                          <!-- 引用外部定义的sql -->

                          <include refid="insertColumn">

                                    <property name="testColomn" value="abc"/>

                          </include>

                 )

                                    <foreach collection="emps" item="emp" separator="union"

                                             open="select employees_seq.nextval,lastName,email from("

                                             close=")">

                                             select #{emp.lastName} lastName,#{emp.email} email from dual

                                    </foreach>

          </insert>

 

bind

• bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:

 

sql片段

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值