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>
1 //模拟实际业务情况,传入多条件进行查询 2 @Test 3 public void testGetUserListByMulConditions(){ 4 SqlSession sqlSession = null; 5 String usercode=""; 6 String userName=""; 7 Integer userRole=1; 8 9 10 List<User> userListShow=new ArrayList<User>(); 11 try { 12 Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2019-12-22"); 13 sqlSession = MyBatisUtil.createSqlSession(); 14 userListShow = sqlSession.getMapper(UserMapper.class).getUserListByMulConditions(usercode,userName,userRole,date); 15 16 } catch (Exception e) { 17 // TODO: handle exception 18 e.printStackTrace(); 19 }finally{ 20 MyBatisUtil.closeSqlSession(sqlSession); 21 } 22 for(User user: userListShow){ 23 logger.debug("testGetUserByForeach_Gender_Roleids UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole()); 24 } 25 26 27 } 28 29
运行结果:
1 [DEBUG] 2019-12-22 17:09:46,696 cn.smbms.dao.user.UserMapper.getUserListByMulConditions - ==> Preparing: select * from smbms_user where 1=1 and userrole= ? 2 [DEBUG] 2019-12-22 17:09:46,711 cn.smbms.dao.user.UserMapper.getUserListByMulConditions - ==> Parameters: 1(Integer) 3 [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] 4 [DEBUG] 2019-12-22 17:09:46,724 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@65d0d124] 5 [DEBUG] 2019-12-22 17:09:46,724 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1708183844 to pool. 6 [DEBUG] 2019-12-22 17:09:46,724 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: admin and UserName: 系统管理员and userRole:1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY