笔记要点
出错分析与总结

/** 笔记:
* 查询的时候,如果某些条件,没带可能SQL拼装会有问题;
* 1.-->给where 后面加上 1=1, 以后的条件都and XXX
* 2. <where> </where> 标签加上后,就不用写SQL的 where 条件语句!
* 3. trim 字符串截取 (~where(封装查询条件),set(封装修改条件) )
* 4. foreach()
*/

工程组织

数据库组织

0.重新修改Bean类

1.定义接口

package com.dao;

import com.bean.Employee;

import java.util.List;

public interface EmployeeMapper_DynamicSQL {

    public List<Employee> getEmpsByConditionIf(Employee e);

    public List<Employee> getEmpsByConditionTrim(Employee e);

    public List<Employee> getEmpsByConditionChoose(Employee e);

    public void updateEmp(Employee e);  //执行更新操作,能更新多少更新多少,弹性最大限度地进行更新!

    public void updateEmpByTrim(Employee e);  //修改

}

2.定义XML映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--动态SQL几个主要模块:-->
<!--if 标签
test="判断表达式,OGNL语法(参照PPT)"
:从传入的参数中取值判断,
遇见特殊符号,写转义字符!-> and : &amp;&amp; "" : &quot;&quot;
或者写单引号也可以!!
网址: W3CSchool->html教程->ISO-8859-> ASCII编码
OGNL : 会进行字符串与数字的转换判断 "0"==0 ,"1"==1 ;
-->
<!--where 标签
查询的时候,如果某些条件,没带可能SQL拼装会有问题;
给where 后面加上 1=1, 以后的条件都and XXX
2.mybatis 使用where 标签来将所有的查询条件包含在内,
自动去掉拼接后的SQL 的前面的多出来的and 或者or ;在后面的就不可以了!
-->
<!--Trim标签
如果 where 元素没有按正常套路出牌,我们可以通过自定义 trim 元素来定制 where 元素的功能;
prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。
它的作用是移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容。
--> <!--choose (when, otherwise) : 分支选择 ,类似于Java的switch-case
eg:带了id用id查,没有就用lastName来查; 只会选择一个!
-->
<!--trim (where, set)-->
<!--foreach-->
<mapper namespace="com.dao.EmployeeMapper_DynamicSQL">
<!--1.查询员工,要求,携带了那个字段查询条件就带上那个条件的值;
传入两个条件进行模糊查询-->
<select id="getEmpsByConditionIf" resultType="com.bean.Employee">
select * from tbl_employee <where>
<if test="lastName !=null and lastName !='jerry' ">
and last_name like #{lastName}
</if>
<if test="id != null">
and id=#{id}
</if>
</where> </select> <select id="getEmpsByConditionTrim" resultType="com.bean.Employee">
select * from tbl_employee <trim prefix="where" prefixOverrides="and | or">
<if test="lastName !=null and lastName !='jerry' ">
and last_name like #{lastName} and
</if>
<if test="id != null">
and id=#{id}
</if>
</trim>
</select> <select id="getEmpsByConditionChoose" resultType="com.bean.Employee">
select * from tbl_employee
<where>
<choose>
<when test="id != null">
id=#{id}
</when>
<when test="lastName !=null and lastName !='jerry' ">
and last_name like #{lastName}
</when>
<otherwise>
1=1
</otherwise>
</choose> </where> </select> <!--public void updateEmp(Employee e);-->
<update id="updateEmp">
update tbl_employee
<set>
<if test="lastName!=null">last_name=#{lastName},</if>
<if test="email!=null">email=#{email},</if>
<if test="gender!=null">gender=#{gender}</if>
</set>
where id=#{id}
</update> <update id="updateEmpByTrim">
update tbl_employee
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">last_name=#{lastName},</if>
<if test="email!=null">email=#{email},</if>
<if test="gender!=null">gender=#{gender},</if>
</trim>
where id=#{id}
</update>
</mapper>

3.编写测试代码

   public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test09() throws Exception {
SqlSession openSession = getSqlSessionFactory().openSession();
try {
System.out.println("++++++++++---- 3.测试 动态SQL元素:choose关键字!");
EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
Employee employee = new Employee(null, "%e%", null, null);
List<Employee> emps = mapper.getEmpsByConditionChoose(employee);
for (Employee e:emps)
System.out.println(e);
openSession.commit();
} finally {
openSession.close();
}
}
@Test
public void test10() throws Exception {
SqlSession openSession = getSqlSessionFactory().openSession();
try {
// System.out.println("++++++++++---- 4.测试 动态SQL元素:update与set关键字!");
// EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
// Employee employee = new Employee(1, "Jerry", "...@...", "1");
// mapper.updateEmp(employee);
System.out.println("++++++++++---- 5.测试 动态SQL元素:trim与set关键字!");
EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
Employee employee = new Employee(1, "Jerry2333", "233@...", null);
mapper.updateEmpByTrim(employee);
openSession.commit();
} finally {
openSession.close();
}
}

MyBatis3_[tp_41-42-43]-_动态sql_trim_自定义字符串截取_choose分支选择_update的set与if-trim 结合的动态更新的更多相关文章

  1. 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive

    ng-repeat是AngularJS中一个非常重要和有意思的directive,常见的用法之一是将某种自定义directive和ng-repeat一起使用,循环地来渲染开发者所需要的组件.比如现在有 ...

  2. IK-Analyzer(5.3.1)动态配置自定义词典

    参考文献:http://blog.csdn.net/fatpanda/article/details/37911079 jar包: IK-Analyzer-extra-5.3.1.jar IKAnal ...

  3. Spring AOP源码分析(二)动态A0P自定义标签

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 之前讲过Spring中的自定义注解,如果声明了自定义的注解,那么就一定 ...

  4. Flutter实战视频-移动电商-44.详细页_首屏自定义Widget编写

    44.详细页_首屏自定义Widget编写 把详细页的图片.标题.编号和价格形成一个单独的widget去引用 详情页的顶部单独封装个插件 在pages下面新建detials_page的文件件并在里面新建 ...

  5. 动态代理的两种方式,以及区别(静态代理、JDK与CGLIB动态代理、AOP+IoC)

    Spring学习总结(二)——静态代理.JDK与CGLIB动态代理.AOP+IoC   目录 一.为什么需要代理模式 二.静态代理 三.动态代理,使用JDK内置的Proxy实现 四.动态代理,使用cg ...

  6. 《Entity Framework 6 Recipes》中文翻译系列 (38) ------ 第七章 使用对象服务之动态创建连接字符串和从数据库读取模型

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第七章 使用对象服务 本章篇幅适中,对真实应用中的常见问题提供了切实可行的解决方案. ...

  7. struts2:遍历自定义字符串数组,遍历Action实例所引用对象中的数组

    在struts2:OGNL表达式,遍历List.Map集合:投影的使用一文中已经讲述了OGNL遍历List.Map集合等功能. 本文简单写一个遍历数组的示范程序. 1. 遍历自定义字符串数组 < ...

  8. Dotfuscator自定义规则中的元素选择

    Dotfuscator是专业的.NET程序代码保护软件.是支持规则自定义的,你可以对重命名.程序控制流.字符串加密等等功能自定义规则.在进行规则自定义过程中,可以通过元素的不同选择,满足自己的程序需要 ...

  9. 函数-->指定函数--->默认函数--->动态函数--> 动态参数实现字符串格式化-->lambda表达式,简单函数的表示

    #一个函数何以接受多个参数#无参数#show(): ---> 执行:show() #传入一个参数 def show(arg): print(arg) #执行 show(123) #传入两个参数 ...

随机推荐

  1. swift 修改 Navigationbar Tabbar 字体颜色背景等属性

    1.navigationBar的设置 let navColor = UIColor(red: 41/255, green: 160/255, blue: 230/255, alpha: 1) func ...

  2. CF1223D Sequence Sorting

    思路: 首先把数组离散化,然后观察可以发现,在最优解中最终没有被移动过的那些数字一定是连续的,那么找到最长的连续的数字长度即可. 实现: #include <bits/stdc++.h> ...

  3. Swoole练习 Web

    WEB 服务端代码 $http = new swoole_http_server("0.0.0.0", 9501); $http->on('request', functio ...

  4. 网络I/O模型

    事件驱动模型 与传统编程模式不同,事件驱动程序在启动之后,就在那等待,等待什么呢?等待被事件触发.传统编程下也有“等待”的时候,比如在代码块D中,你定义了一个input(),需要用户输入数据.但这与下 ...

  5. python 工具的URL

    Python取得大数据之后如何把数据图形化,之后让客户很清晰的看到你的结果 下面的图形化参照 matplotlib.3.0.2 https://matplotlib.org/gallery/index ...

  6. a标签添加移除事件及开启禁用事件

    一.添加移除点击事件 <script type="text/javascript" src="jquery.min.js"></script& ...

  7. java8 : 流

    package day02.com.offcn.test; import java.io.IOException; import java.nio.charset.Charset; import ja ...

  8. Go语言学习笔记(10)——错误处理示例

    // 定义一个 DivideError 结构 type DivideError struct { dividee int divider int } // 实现 `error` 接口 func (de ...

  9. Scala 面向对象编程之类

    定义一个简单的类 // 定义类,包含field以及方法 class HelloWorld { private var name = "leo" def sayHello() { p ...

  10. python装饰器无参及有参案例

    装饰器无参和有参案例: