需求:模拟实际业务情况,传入多条件进行查询

/**
* 需求:模拟实际业务,用户传入多个条件,进行用户列表信息的查询
* @param roleids
* @return
*/
public List<User> getUserListByMulConditions(@Param("usercode")String usercode,@Param("userName")String userName,@Param("userRole")Integer userRole,@Param("creationDate")java.util.Date date);

<!--模拟多个条件,进行用户列表信息的查询 -->
<select id="getUserListByMulConditions" resultMap="userListArray" >
  select * from smbms_user where 1=1
  <choose>
    <when test="userName!=null and userName!='' "> and userName like concat('%',#{userName},'%')</when>
    <when test="usercode!=null and usercode!='' "> and usercode like concat('%',#{usercode},'%')</when>
    <when test="userRole!=null and userRole!='' "> and userrole= #{userRole} </when>
    <otherwise> and year(creationDate)=year(#{creationDate}) </otherwise>
  </choose>
</select>

<resultMap type="User" id="userListArray">
   <id property="id" column="id"/>
  <result property="userCode" column="userCode" />
  <result property="userName" column="userName" />
  <result property="userRole" column="userRole" />
</resultMap>

 //模拟实际业务情况,传入多条件进行查询
@Test
public void testGetUserListByMulConditions(){
SqlSession sqlSession = null;
String usercode="";
String userName="";
Integer userRole=1; List<User> userListShow=new ArrayList<User>();
try {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2019-12-22");
sqlSession = MyBatisUtil.createSqlSession();
userListShow = sqlSession.getMapper(UserMapper.class).getUserListByMulConditions(usercode,userName,userRole,date); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user: userListShow){
logger.debug("testGetUserByForeach_Gender_Roleids UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
} }

运行结果:

 [DEBUG] 2019-12-22 17:09:46,696 cn.smbms.dao.user.UserMapper.getUserListByMulConditions - ==>  Preparing: select * from smbms_user where 1=1 and userrole= ?
[DEBUG] 2019-12-22 17:09:46,711 cn.smbms.dao.user.UserMapper.getUserListByMulConditions - ==> Parameters: 1(Integer)
[DEBUG] 2019-12-22 17:09:46,723 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@65d0d124]
[DEBUG] 2019-12-22 17:09:46,724 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@65d0d124]
[DEBUG] 2019-12-22 17:09:46,724 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1708183844 to pool.
[DEBUG] 2019-12-22 17:09:46,724 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: admin and UserName: 系统管理员and userRole:1

mybatis框架-choose when otherwise 的使用的更多相关文章

  1. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  2. 【Java】关于MyBatis框架的总结

    最近在学习MyBatis框架,我在这里记录一下学习MyBatis过程中的心得体会. Mybatis是什么?使用它我们可以做什么? MyBatis是一个开源的数据持久层框架,它内部封装了通过JDBC访问 ...

  3. 关于mybatis框架的总结【转载】

    原文地址:https://www.cnblogs.com/xiaotie666/p/LiujinMybatisSummary.html 此文为转载.请支持原作者. 最近在学习MyBatis框架,我在这 ...

  4. 深入学习Mybatis框架(二)- 进阶

    1.动态SQL 1.1 什么是动态SQL? 动态SQL就是通过传入的参数不一样,可以组成不同结构的SQL语句. 这种可以根据参数的条件而改变SQL结构的SQL语句,我们称为动态SQL语句.使用动态SQ ...

  5. MyBatis框架 课程笔记

    MyBatis框架 课程笔记   第1章 MyBatis简介 1.1 MyBatis历史 1)MyBatis是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Softw ...

  6. MyBatis框架——动态SQL

    MyBatis 作为⼀个“半⾃动化”的 ORM 框架,需要开发者⼿动定义 SQL 语句. 在业务需求⽐较复杂的情 况下,⼿动拼接 SQL 语句的⼯作量会⾮常⼤,为了适⽤于不同的业务需求,往往需要做很多 ...

  7. SSM框架之MyBatis框架实现简单的增删改查

    MyBatis框架介绍 MyBatis是一个优秀的数据持久层框架,在实体类和SQL语句之间建立映射关系是一种半自动化的ORM实现,其封装性要低于Hibernate,性能优越,并且小巧,简单易学,应用也 ...

  8. mybatis框架整合及逆向工程

    mybatis框架整合及逆向工程 一.三大框架整合 ​ 整合SSM框架 1.导入pom文件 1.导入spring的pom依赖 <?xml version="1.0" enco ...

  9. 学习mybatis框架>从零开始学JAVA

    目录 学习mybatis框架 mybatis框架的介绍 什么是mybatis框架 为什么要使用mybatis框架 mybatis的优点 Mybatis框架环境搭建 去官网下载jar包 创建一个普通的j ...

随机推荐

  1. Python2.x升级python3.x【升级步骤和错误总结】

    网上帖子一大堆,按照那些教程操作,确实可以成功安装.但是安装成功之后呢,pip还是用的python2的pip. 切换到python3的pip之后,发现无法下载模块,还会有很多报错信息.以及" ...

  2. linux开启tcp_timestamps和tcp_tw_recycle引发的问题研究

    环境:centos7.4 内核版本3.10 最近看内核参数tcp_tw_recycle(该参数在内核 4.12 之后被移除),它用于快速回收处理TIME_WAIT状态的socket.搜索该参数相关的资 ...

  3. 极简 Spring Boot 整合 Thymeleaf 页面模板

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  4. 《 .NET并发编程实战》阅读指南 - 第3章

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  5. java报错 pom.xml第一行报"org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project...

    https://www.cnblogs.com/appium/p/11168441.html 新建Maven项目时,每个pom文件第一行都报错. 一.问题分析 原因就是你的maven的配置文件不是最新 ...

  6. 针对接口编程能帮助达到面向对象开发和设计中"低耦合"的要求. 某公司...打印机...(笔试中遇到的题目)

    针对接口编程能帮助达到面向对象开发和设计中"低耦合"的要求.         举个例子:某公司有一台特殊打印机,还可以使用一年,一年后可能换为另一种打印机,这两种打印机都特殊而贵. ...

  7. python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple

    # -*- coding: utf-8 -*- # 请使用迭代查找一个list中最小和最大值,并返回一个tuple from collections import Iterable def findM ...

  8. C#多线程下如何保证线程安全?

    多线程编程相对于单线程会出现一个特有的问题,就是线程安全的问题.所谓的线程安全,就是如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是 ...

  9. validateField方法对部分表单字段进行校验

    原文:https://blog.csdn.net/qq_37782076/article/details/85123602 代码 <template> <div class=&quo ...

  10. 基于YOLO3对图像加框的函数draw_image()

    def draw_bbox(image, bboxes, class_i, show_label=True): # 将中心点坐标与w,h通过变化为左上角与右下角坐标 bboxes_change = n ...