mybatis框架-choose when otherwise 的使用
需求:模拟实际业务情况,传入多条件进行查询
/**
* 需求:模拟实际业务,用户传入多个条件,进行用户列表信息的查询
* @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 的使用的更多相关文章
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 【Java】关于MyBatis框架的总结
最近在学习MyBatis框架,我在这里记录一下学习MyBatis过程中的心得体会. Mybatis是什么?使用它我们可以做什么? MyBatis是一个开源的数据持久层框架,它内部封装了通过JDBC访问 ...
- 关于mybatis框架的总结【转载】
原文地址:https://www.cnblogs.com/xiaotie666/p/LiujinMybatisSummary.html 此文为转载.请支持原作者. 最近在学习MyBatis框架,我在这 ...
- 深入学习Mybatis框架(二)- 进阶
1.动态SQL 1.1 什么是动态SQL? 动态SQL就是通过传入的参数不一样,可以组成不同结构的SQL语句. 这种可以根据参数的条件而改变SQL结构的SQL语句,我们称为动态SQL语句.使用动态SQ ...
- MyBatis框架 课程笔记
MyBatis框架 课程笔记 第1章 MyBatis简介 1.1 MyBatis历史 1)MyBatis是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Softw ...
- MyBatis框架——动态SQL
MyBatis 作为⼀个“半⾃动化”的 ORM 框架,需要开发者⼿动定义 SQL 语句. 在业务需求⽐较复杂的情 况下,⼿动拼接 SQL 语句的⼯作量会⾮常⼤,为了适⽤于不同的业务需求,往往需要做很多 ...
- SSM框架之MyBatis框架实现简单的增删改查
MyBatis框架介绍 MyBatis是一个优秀的数据持久层框架,在实体类和SQL语句之间建立映射关系是一种半自动化的ORM实现,其封装性要低于Hibernate,性能优越,并且小巧,简单易学,应用也 ...
- mybatis框架整合及逆向工程
mybatis框架整合及逆向工程 一.三大框架整合 整合SSM框架 1.导入pom文件 1.导入spring的pom依赖 <?xml version="1.0" enco ...
- 学习mybatis框架>从零开始学JAVA
目录 学习mybatis框架 mybatis框架的介绍 什么是mybatis框架 为什么要使用mybatis框架 mybatis的优点 Mybatis框架环境搭建 去官网下载jar包 创建一个普通的j ...
随机推荐
- Spring Boot 知识笔记(定时任务与异步)
一.定时任务 1.启动类里面增加注入 @SpringBootApplication //@SpringBootApplication = @Configuration+@EnableAutoConfi ...
- oracle--数据库扩容后出现ORA-27102
一,问题描述 Connected to an idle instance. SQL> startup nomount ORA: obsolete or deprecated parameter( ...
- Kubelet 证书如何自动续期
一.问题现象和原因 Kubernetes 日志错误 当 Kubernetes 集群日志中出现 certificate has expired or is not yet valid 错误信息时,表明证 ...
- 通过欧拉计划学习Rust编程(第22~25题)
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,所以先补一下Rust的基础知识.学习了一段时间,发现Rust的学习曲线非常陡峭,不过仍有快速入门的办法. 学习任何一项技能最怕没有 ...
- torch_02_多项式回归
""" torch.float64对应torch.DoubleTensor torch.float32对应torch.FloatTensor 将真实函数的数据点能够拟合成 ...
- Linux启动/停止/重启gitlab
# Start all GitLab components sudo gitlab-ctl start # Stop all GitLab components sudo gitlab-ctl sto ...
- bean的shutdown
使用@Bean注解,在不配置destroyMethod时,其默认值为: String destroyMethod() default AbstractBeanDefinition.INFER_METH ...
- CEF4Delphi初识
代码模块与职责 所有的代码都在src目录下,这会导致一上手的时候无法快速划分模块,不便于理解,如果分类然后放文件夹就会好一些. 最关键的部分在于uCEFApplication,是和dll链接的部分 u ...
- C#异步的世界【下】(转)
接上篇:<C#异步的世界[上]> 上篇主要分析了async\await之前的一些异步模式,今天说异步的主要是指C#5的async\await异步.在此为了方便的表述,我们称async\aw ...
- 笔记本CPU性能排行
截图如下: 1. 图1 2. 图2 3. 4. 5. 6. 7. 8. 谢谢浏览!