我是靠谱客的博主 乐观含羞草,这篇文章主要介绍mysql choose when_mybatis 基础(二) 动态sql 关于where if / where choose when otherwise,现在分享给大家,希望可以做个参考。

个人理解:

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 * from user

username=#{userName}

and gender=#{gender}

@Test

public void testFindByCondition() throws Exception{

User user=new User();

user.setUserName("xxx");

user.setGender("男");

List 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 这样的多个条件的拼接.

最后

以上就是乐观含羞草最近收集整理的关于mysql choose when_mybatis 基础(二) 动态sql 关于where if / where choose when otherwise的全部内容,更多相关mysql内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部