一、<if>

  使用<if>可以根据具体情况来拼接SQL语句,使其更加灵活更加适应我们的需求。

  <if>的标签体中是需要拼接的语句,满足条件才会将其进行拼接。

  <if>标签中的test属性用于判断添加。

  例如我们现在有这样一个查询需求,如果客户名不为空则按客户名模糊查询,

  如果客户职业不为空,则按职业查询。如果两者都不为空则按客户名和职业查询。

    <select id = "findByUsernameOrJobs" resultMap = "re">
select * from t_customer where 1=1
<if test = "username == null and username = '' ">
and t_username like concat('%',#{username},'%')
</if>
<if test = "jobs != null and jobs != '' ">
and t_jobs = #{jobs}
</if>
</select>

      <if>元素中满足条件的语句会被拼接入查询语句。

  

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisTest {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
//获取配置文件输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
//通过配置文件输入流构建sqlSessionFactory,
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"my");
//通过sqlSessionFactory获取sqlSession,true代表设置为自动提交。
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//执行CustomerMapper.xml文件中,id为xxxCustomer的语句,参数为Integer对象。
Customer customer = new Customer();
customer.setJobs("student");
customer.setUsername("zrx");
List<Customer> customers = sqlSession.selectList("com.mybatis.mapper.CustomerMapper.findByUsernameOrJobs", customer);
for(Customer temp : customers) {
System.out.println(customers);
}
//如果没有设置自动提交,使用insert、update、delete时需要使用sqlSession.commit()手动提交。
sqlSession.close();
}
}

可以看到查询语句拼接了username 和jobs这两个条件。

二、<choose>、<when>、<otherwise>

  但有时候我们可能有这样的需求,

  如果username不为空则按username查询。

  如果usernae为空,jobs不为空,则按jobs查询。

  如果jobs为空则查询所有电话不为空的用户。

  则使用<if>进行处理是不方便的。

  这时我们就可以使用<choose>、<when>、<otherwise>配合完成。

  <chosse>类似switch,<when> 类似 case  xx:xxxxx:break;<otherwise>类似default:xxx.

  但某一个<when>中的test为true时,会拼接该标签中的语句,然后结束<choose>.也就是说所<choose>中

  只会执行一个语句。如果所有<when>都不满足,则执行<otherwise>。

CustomerMapper.xml

    <select id = "findByUsernameOrJobs" resultMap = "re">
select * from t_customer where 1=1
<choose>
<when test = "username != null and username != '' ">
and t_username = #{username}
</when>
<when test = "jobs != null and jobs != '' ">
and t_jobs = #{jobs}
</when>
<otherwise>
and t_phone is not null
</otherwise>
</choose>
</select>

测试代码与上例相同。

运行结果:

  

  可以看到,查询语句只拼接了username.

三、<where><trim>

  上例子中where后面都有一个1==1,

  这时因为如果条件不满足时或拼接语句时有一个1==1,不会导致sql语句错误。

  如果没有这个1==1,第一个添加满足语句就变成了

  select * form t_customer where and username = #{usernmae}.

  这样显然是不行的,所以添加了一个1==1避免错误。

  当然也有标签可以避免这个问题,那就是<where>标签。

<select id = "findByUsernameOrJobs" resultMap = "re" parameterType="com.mybatis.first.Customer">
select * from t_customer
<where>
<if test = "username == null and username = '' ">
and t_username like concat('%',#{username},'%')
</if>
<if test = "jobs != null and jobs != '' ">
and t_jobs = #{jobs}
</if>
</where>
</select>

  使用where标签,只有在<where>标签类的有满足语句才会添加where关键字,且会去掉多余的'and' 和'or'。

测试: (拼接where关键字及语句、去取多余and) 

     Customer customer = new Customer();
customer.setJobs("student");
customer.setUsername("zrx");
List<Customer> customers = sqlSession.selectList("com.mybatis.mapper.CustomerMapper.findByUsernameOrJobs", customer);
for(Customer temp : customers) {
System.out.println(customers);
}

(没有满足条件语句,不添加where关键字)

 List<Customer> customers = sqlSession.selectList("com.mybatis.mapper.CustomerMapper.findByUsernameOrJobs", null);

  <trim>也可以实现<where>的效果,而且比<where>更灵活。

  <trim>中有四个属性

  prefix :代表要添加的前缀;prefix = "where"代表添加前缀 “where”。

  prefixOverrides:代表要删除的前缀,例如prefixOverrides = “and“.代表删除前缀and。

  suffix:代表要添加的后缀;

  suffixOverrides :代表要删除的后缀。

  使用<trim> 代替<where> 

    <select id = "findByUsernameOrJobs" resultMap = "re" parameterType="com.mybatis.first.Customer"
flushCache = "true">
select * from t_customer
<trim prefix = "where" prefixOverrides="and">
<if test = "username != null and username != '' ">
and t_username like concat('%',#{username},'%')
</if>
<if test = "jobs != null and jobs != '' ">
and t_jobs = #{jobs}
</if>
</trim>
</select> <!--添加前缀where 同时去掉第一个and-->

运行结果与<where>标签相同。

  

  

四、<set>

  有时只需要更新某一个字段,这时就可以使用<set>标签和<if>标签来实现生成对应的SQL语句。

   

    <update id = "updateDy" parameterType = "com.mybatis.first.Customer">
update t_customer
<set>
<if test = "username != null and username != '' ">
t_username = #{username},
</if>
<if test = "jobs != null and jobs != '' ">
t_jobs = #{jobs},
</if>
<if test = "phone != null and phone != '' ">
t_phone = #{phone},
</if>
</set>
where t_id = #{id}
</update>
     Customer customer = new Customer();
customer.setId(1);
customer.setUsername("hcf");
customer.setPhone("133xxxxx1");
//更新表中id为1的数据的username和phone。
int num = sqlSession.update("com.mybatis.mapper.CustomerMapper.updateDy", customer);
//查询
List<Customer> customers = sqlSession.selectList("com.mybatis.mapper.CustomerMapper.findByUsernameOrJobs", customer);
for(Customer temp : customers) {
System.out.println(customers);
}
//如果没有设置自动提交,使用insert、update、delete时需要使用sqlSession.commit()手动提交。
sqlSession.close();

更新前:

  

更新后:

  

生成的更新语句中只拼接了属性不为空的选项,属性为空的选项(jobs)没有拼接入语句中。

这样可以动态的对需要更新的属性进行更新,而不是对其全部更新。

<set>还去除了最后的“,”。(t_phone = #{phone},)后面的","被去除了。

同样<set>可以用<trim>来实现,只需要添加前缀set,和去除后缀“,”即可。

五、<foreach>

  例如我们需要查询id为1~10的客户,这个用手写也可以,但如果是需要查询id为1-1000的客户,这个手写的工作量就比较打了。

  这时我们可以采用<foreach>元素来实现这个功能。

  例如我们先构建一个list,通过for循环添加1000个元素到list中,然后将list传递给sql语句,再使用foreach读取每一个元素,最后根据元读取素拼接成语句。

  <foreach>属性:

  item:可以看做一个临时变量,用于存放迭代目标中单个元素的值。

  index:当前元素在集合中的下标,如果是字典类型则代表key。

  collection:指传递过来的集合类型。

  open,close:代表开始和结束符号。

  separator:代表元素分隔符。

  

    <select id="findByDyID" resultMap = "re" parameterType = "List">
select * from t_customer where t_id in
<foreach collection="list" item = "id" index = "index"
open = "(" separator = "," close = ")" >
#{id}
</foreach>
</select>
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10 ; i++) {
list.add(i);
}
List<Customer> customers = sqlSession.selectList("com.mybatis.mapper.CustomerMapper.findByDyID", list);
for(Customer temp : customers) {
System.out.println(customers);
}
//如果没有设置自动提交,使用insert、update、delete时需要使用sqlSession.commit()手动提交。
sqlSession.close();

六、<bind>  

  <bind>元素可以通过一个OGNL表达式来创建一个上下文变量。

  

    <!-- 根据用户名模糊查询 -->
<select id = "findCustomerByName" parameterType = "com.mybatis.first.Customer"
resultMap = "re">
<bind name="bindFindByName" value="'%' + _parameter.getUsername() + '%'"/>
select * from t_customer where t_username like #{bindFindByName}
</select>

其中_parameter.getUsername是获取customer对象中的username属性值,也可以直接写username.

        Customer customer = new Customer();
customer.setId(1);
customer.setUsername("h");
customer.setPhone("133xxxxx1");
//查询
List<Customer> customers = sqlSession.selectList("com.mybatis.mapper.CustomerMapper.findCustomerByName", customer);
for(Customer temp : customers) {
System.out.println(temp);
}
//如果没有设置自动提交,使用insert、update、delete时需要使用sqlSession.commit()手动提交。
sqlSession.close();

1.3(Mybatis学习笔记)动态SQL的更多相关文章

  1. Mybatis学习笔记-动态SQL

    概念 根据不同环境生成不同SQL语句,摆脱SQL语句拼接的烦恼[doge] 本质:SQL语句的拼接 环境搭建 搭建数据库 CREATE TABLE `blog`( `id` VARCHAR(50) N ...

  2. mybatis学习 十 动态 SQL

    1.  根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...

  3. mybatis学习之动态sql

    mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录: 1.select查询 简单的select类似如下: <select id=" ...

  4. mybatis 学习五 动态SQL语句

    3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...

  5. Mybatis进阶学习笔记——动态sql

    1.if标签 <select id="queryByNameAndTelephone" parameterType="Customer" resultTy ...

  6. Mybatis学习笔记17 - sql标签和include标签

    示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import org.apache.ibatis.anno ...

  7. Mybatis学习笔记导航

    Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...

  8. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  9. Mybatis学习笔记之二(动态mapper开发和spring-mybatis整合)

    一.输入映射和输出映射 1.1 parameterType(输入类型) [传递简单类型] 详情参考Mybatis学习笔记之一(环境搭建和入门案例介绍) 使用#{}占位符,或者${}进行sql拼接. [ ...

  10. mybatis 学习笔记(二):mybatis SQL注入问题

    mybatis 学习笔记(二):mybatis SQL注入问题 SQL 注入攻击 首先了解下概念,什么叫SQL 注入: SQL注入攻击,简称SQL攻击或注入攻击,是发生于应用程序之数据库层的安全漏洞. ...

随机推荐

  1. 牛客 国庆七天乐 day1 L

    https://www.nowcoder.com/acm/contest/201/L 题意:给你两条平行的直线和n个圆,在直线上面行走和在圆上和在圆内行走不需要耗费体力,除了这些区域外平面上经过任意两 ...

  2. sls语法:创建file,创建文件夹

    http://blog.kukafei520.net/html/2014/942.html /tmp/aaa.txt: file.managed /tmp/salt_test: file.direct ...

  3. bzoj 4624 农场种植 fft

    4624: 农场种植 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 48  Solved: 31[Submit][Status][Discuss] D ...

  4. import pymongo exceptions.ImportError: No module named pymongo

    最近用Scrapy写爬虫,将爬取的数据存入Mongodb中,使用的是pymongo这个库,但是运行的时候报错如标题所示 搜了好多网站包括stackoverflow都没有解决,后来发现自己用的是虚拟环境 ...

  5. APP兼容性测试

    一.APP兼容性范围以及问题 1.硬件 各个硬件结构 2.软硬件之间 硬件dll库(C++) 软硬件之间的通信,各个厂商提供的ROM 3.软件 浏览器.操作系统.数据库.手机.功能兼容性(功能修改,二 ...

  6. Spring学习--xml 中 Bean 的自动装配

    Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...

  7. iOS 单元测试(Unit Test 和 UI Test)

    之前一直搞过~~最近试了一下下,完美~~ 附上一篇文章,不同的伙伴可以看看: http://www.jianshu.com/p/009844a0b9edUnitTest(简单的单元测试使用) http ...

  8. 【hdu4436/LA6387-str2int】sam处理不同子串

    题意:给出n个数字,数字很长,用字符串读入,长度总和为10^5.求这n个字符串的所有子串(不重复)的和取模2012 . 例如字符串101,和就是1+10+101=112. 题解: 就是求不同的子串连成 ...

  9. 百练2505:A multiplication game

    传送门:http://bailian.openjudge.cn/practice/2505/ [题解] 我们找找规律: 1~9显然是Stan wins. 10~18是Ollie wins. 19~16 ...

  10. git分支开发,分支(feature)同步主干(master)代码,以及最终分支合并到主干的操作流程

    由于rebase执行速度慢,分支同步主干代码时,分支的每次提交都可能和主干产生冲突,需要解决的次数太多,影响提交效率. 同时,为了保证主干提交线干净(可以安全回溯),所以采用下面所说的merge法. ...