笔记要点
出错分析与总结

/** 笔记:
* 查询的时候,如果某些条件,没带可能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. 【作业】Kitchen Plates(拓扑排序)

    题目链接:https://vjudge.net/contest/345791#problem/O [问题描述] You are given 5 different sizes of kitchen p ...

  2. SSH无密码(密钥验证)登录的配置

    进入到我的home目录 cd ~/.ssh [root@shtppATTSTPUBv03 home]# cd ~/.ssh [root@shtppATTSTPUBv03 .ssh]# pwd /roo ...

  3. log4j向指定文件输出日志

    一.log4j.properties中的配置 log4j.logger.cache=INFO,ERROR,batchFile log4j.addivity.batchFile=false log4j. ...

  4. todo---callback

    todo---callback https://blog.csdn.net/u010158267/article/details/51426963/

  5. Java调用SqlLoader将大文本导入数据库

    Java调用SqlLoader将大文本导入数据库 业务场景:将一千万条数据,大约500M的文本文档的数据导入到数据库 分析:通过Java的IO流解析txt文本文档,拼接动态sql实现insert入库, ...

  6. QT 创建一个线程播放监控视频

    1.创建一个线程类(PlayVideoThread): PlayVideoThread.h头文件 #pragma once #include <QObject> #include &quo ...

  7. Python中关于Lambda函数的使用总结

    lambda表达式是一种匿名函数,对应python中的自定义函数def,是定义某个函数时比较高级的一种写法.作为python初学者,本文整理了lambda的一些基本用法和特点. lambda和def的 ...

  8. Python基础 — 八种数据类型

    Python 3.x 的八种数据类型 八种数据类型分别是: number(数字).string(字符串).Boolean(布尔值).None(空值) list(列表).tuple(元组).dict(字 ...

  9. js中__proto__和prototype的区别和关系?(转)

    转自知乎:https://www.zhihu.com/question/34183746

  10. JQuery 的优先级

    1.使用最新的jQuery版本 2.用对选择器.   2.1 jquery最快的选择器是ID选择器:来源于js的getElementById()方法 注释:需要选择多个元素,必然涉及到Dom遍历和循环 ...