笔记要点
出错分析与总结

/** 笔记:
* 查询的时候,如果某些条件,没带可能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. Configuring and Running Django + Celery in Docker Containers

    Configuring and Running Django + Celery in Docker Containers  Justyna Ilczuk  Oct 25, 2016  0 Commen ...

  2. 【VS开发】如何判断客户端SOCKET已经断开连接?

    http://biancheng.dnbcw.info/linux/366100.html    最近在做一个服务器端程序,C/S结构.功能方面比较简单就是client端与server端建立连接,然后 ...

  3. 一个unsigned int 数的二进制表示中有多少个1

    这是一道面试题可以用以下的一些方案.第一种是很容易想到的采用循环的方式并且与1进行位与运算,具体代码如下.  1unsigned int GetBitNumOfOne_ByLoop1(unsigned ...

  4. Hive 企业调优

    9.企业级调优 9.1 Fetch 抓取 Fetch 抓取:Hive 中对某些情况的查询可以不必使用 MapReduce 计算: hive.fetch.task.conversion:more 9.2 ...

  5. 通过docker安装elasticsearch和安装ik分词器插件及安装kibana

    前提: 已经安装好docker运行环境: 步骤: 1.安装elasticsearch 6.2.2版本,目前最新版是7.2.0,这里之所以选择6.2.2是因为最新的SpringBoot2.1.6默认支持 ...

  6. 外网访问虚拟机搭建的web服务

    凌晨了,就简单写个一定可行的思路吧,有时间了再补上. 设置虚拟机为桥接模式,当然NAT也行,只是我嫌NAT麻烦 设置路由器,将虚拟机端口映射到外网

  7. 机器学习-LDA主题模型笔记

    LDA常见的应用方向: 信息提取和搜索(语义分析):文档分类/聚类.文章摘要.社区挖掘:基于内容的图像聚类.目标识别(以及其他计算机视觉应用):生物信息数据的应用; 对于朴素贝叶斯模型来说,可以胜任许 ...

  8. MySQL DBA的KPI考核指标有哪些

    绩效考核是对一名员工所作工作的数量.质量.难度.强度.效率的量化考量.由于DBA的工作性质与纯粹的研发人员或运维人员有所区别,对DBA的KPI考核指标也有其特殊性.参考以往的经验和一些较大的DBA t ...

  9. linux下的打包与解包的简单总结

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...

  10. elk 流程图

    ELK流程图 单纯使用ElK实现分布式日志收集缺点: 1.logstash太多了,扩展不好. 2.读取IO文件,可能会产生日志丢失 3.不是实时性 这时候就需要引入 kafka. kafka基于主题模 ...