个人理解:

where if就相当于正常的java中的if 语句,如果有多个条件组合判断的话用 and, or连接

而where choose when otherwise

choose就好像是switch,when相当于case,可以有一种属性的对个判断,但不能同时去判断多个属性. 同时,只要有一个when满足,则break

otherwise就好像是default,如果前面的when都不满足,则进入otherwise

1.动态sql where if

传递参数为实体类user,if内的userName是user的属性 username=#{userName} 中的username是数据库表中的字段, #{userName}是实体类user中的.

 <select id="findByCondition" parameterType="user"  resultType="user">
select * from user
<!-- 这里的userName 对应的也是实体类中的属性-->
<!-- test里面的属性基本都来自于parameterType中的,如这里的userName,是实体类user中的属性-->
<where>
<if test="userName!=null">
username=#{userName}
</if>
<if test="gender!=null">
and gender=#{gender}
</if>
</where>
</select> @Test
public void testFindByCondition() throws Exception{
User user=new User();
user.setUserName("xxx");
user.setGender("男");
List<User> list = userDao.findByCondition(user);
for (User user1:list)
{
System.out.println(user1);
}
}

2.同时,if中可以由多个条件的拼接,如这里的ids !=null and ids.size()>0 或者是ids !=null and user!=null 这样的多个条件的拼接.

<!--多个id,新建实体类,传入id集合,并用foreach标签遍历ids-->
<select id="findByIds" parameterType="vo" resultType="user">
select * from user
<!-- 这里的userName 对应的也是实体类中的属性-->
<where>
<!--这里的ids 是vo里面的属性. -->
<if test="ids !=null and ids.size()>0">
<!--这里的item对应的是user里面的属性字段-->
<foreach collection="ids" open=" uid in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select> @Test
public void testFindByIds() throws Exception{
Vo vo=new Vo();
List<Integer> list=new ArrayList<>();
list.add(2);
list.add(5);
list.add(7);
vo.setIds(list);
List<User> users = userDao.findByIds(vo);
for (User user:users)
{
System.out.println(user);
}
}

3.但是如果是where choose when otherwise 的时候,切记不能使用不同属性条件的拼接,如 a1 and b2 这样是不可以的, 但是可以 a!=null and a==1 这样,对同一个属性进行不同的判断

 <select id="findByChoose" resultType="user" parameterType="user">
select * from user
<where>
<choose>
<!-- 这里test为双引号,则代表里面应该是字符串,而'男' 会默认为字符.所以gender=='男' 会出现不匹配错误,所以这里需要toString() -->
<when test=" gender!='女'.toString() and gender=='男'.toString()">
age=22
</when>
<!--这里写成单引号,内部可以使用双引号-->
<!-- <when test=' gender=="男" '>
age=22
</when>-->
<otherwise>
age=23
</otherwise>
</choose>
</where>
</select> @Test
public void testFindByChoose() throws Exception{
User user=new User();
user.setUserName("aaa");
user.setGender("男");
user.setAge(22);
List<User> users= userDao.findByChoose(user);
for (User user1:users){
System.out.println(user1);
}
}

mybatis 基础(二) 动态sql 关于where if / where choose when otherwise的更多相关文章

  1. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  2. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  3. mybatis 详解------动态SQL

    mybatis 详解------动态SQL   目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...

  4. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  5. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  6. mybatis入门基础(五)----动态SQL

    一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...

  7. 框架应用:Mybatis(二) - 动态SQL

    MybatisUtil工具类 在实际开发中,我们可以编写一个MybatisUtil辅助类来进行对进行操作. 1)在静态初始化块中加载mybatis配置文件和StudentMapper.xml文件一次 ...

  8. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...

  9. mybatis框架(5)---动态sql

    那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.Mybatis的动态S ...

随机推荐

  1. flask框架(六): 实现支持正则的路由

    一:默认路由 @app.route('/user/<username>') @app.route('/post/<int:post_id>') @app.route('/pos ...

  2. 顺序表应用5:有序顺序表归并(SDUT 3329)

    Problem Description 已知顺序表A与B是两个有序的顺序表,其中存放的数据元素皆为普通整型,将A与B表归并为C表,要求C表包含了A.B表里所有元素,并且C表仍然保持有序. Input ...

  3. 两列布局实现各自独立滚屏,类似与 scrollNav 的功能。

    现在移动端 web 开发越来越靠近 app 的功能.所以两列布局各自都能实现独立滚动也常见.基于固定侧边栏导航,另一侧实现内容展示. 这个功能的核心在于使用 vh 单位. 其中 CSS 的代码是核心点 ...

  4. 类中定义成员方法。加不加public有什么区别?

    class Trangle{ double sideA, sideB, sideC, area, length; boolean flag; Trangle(double a, double b, d ...

  5. Latex里面的\newtheorem*{xx}{yy}后面的*是干什么的?

    答案: 加了*表示不标号.例如: 同样使用命令: \begin{prb} xx\end{prb} 1. \newtheorem{prb}{Problem Formulation} → Problem ...

  6. apache httpd.conf 文件的 详解

    文章 摘自 :http://www.php100.com/html/webkaifa/apache/2009/0418/1192.html   ServerRoot /usr/local Server ...

  7. zookeeper系列(一)zookeeper图形化的客户端工具

    追加一个zookeeper图形化的客户端工具: 1.zookeeper图像化客户端工具的下载地址:https://issues.apache.org/jira/secure/attachment/12 ...

  8. 用Intellij idea搭建solr调试环境

    最近在使用solr时,配置会有一些问题,log里面打印出日志了,但是还是不知道发生这样错误的原因.于是想学习一下相关的solr源码,以下是如何搭建solr调试环境步骤. solr调试环境搭建,首先下载 ...

  9. AMBARI部署HADOOP集群(4)

    通过 Ambari 部署 hadoop 集群 1. 打开 http://192.168.242.181:8080  登陆的用户名/密码是 : admin/admin 2. 点击 “LAUNCH INS ...

  10. vue调试工具vue-devtools安装

    vue-devtools是一款基于chrome浏览器的插件,用于vue应用的调试,这款vue调试神器可以极大地提高我们的调试效率.帮助我们快速的调试开发vue应用. 这里介绍一下vue-devtool ...