一、什么是动态SQL之if语句
if很简单了,就是满足条件就执行,不满足条件不执行。
那么动态SQL中的if语句是怎么样的呢?
首先我们来看一张表blog:

如果我们执行下面的SQL语句:
select * from blog
肯定会将所有的数据都查出来。那么我们可以在后面加上where条件进行筛选,那么如果我们想不同的情况下执行不同的where甚至有时候多种情况一起发生怎么办,这时候我们就需要用到if进行判断并进行SQL语句的拼接了,就类似于下面这句SQL:
select * from blog where title="" and author =""
但是若果我们把tilte和author都作为if判断中的内容,where后面岂不是什么也没有了,这时候我们就需要这样来写SQL语句:
select * from blog where 1=1 and title="" and author =""
明白了动态SQLif的基本原理,我们就去具体的实现。
二、动态SQLif语句的实现
这里我会用四种方法来进行实现:
这四个方法的不同都是Mapper接口中的方法不同。
1.函数重载
BlogMapper接口中的方法有以下几个:
List<Blog> QueryBlogsByIf();
List<Blog> QueryBlogsByIf(@Param("title") String title, @Param("author") String author);
BlogMapper.xml:
<select id="QueryBlogsByIf" resultType="Blog">
select * from mybaties.blog where 1=1
<if test="author != null">
and author=#{author}
</if>
<if test="title != null">
and title=#{title}
</if>
</select>
if标签中的test就是判断语句。
我们进行测试:
@Test
public void queryBlogIf() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.QueryBlogsByIf();
for (Blog blog : blogList) {
System.out.println(blog);
}
sqlSession.close();
}
调用不传任何参数的方法应该是查询所有数据:

没有问题。
接下来我们让auto不为空:
@Test
public void queryBlogIf() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.QueryBlogsByIf(null, "jms");
for (Blog blog : blogList) {
System.out.println(blog);
}
sqlSession.close();
}
结果如下:

没有问题。
接下来我们让auther和title都不为空
@Test
public void queryBlogIf() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.QueryBlogsByIf("learn mybatis day 5", "jms");
for (Blog blog : blogList) {
System.out.println(blog);
}
sqlSession.close();
}
结果如下:

没有问题。
到这里我们可以发现一个问题,我们完全可以不进行函数的重构,就只用一个函数
List<Blog> QueryBlogsByIf(@Param("title") String title, @Param("author") String author);
来进行传参,调用时只需要设定参数是否为null即可。这也就是我们讲的第二个方法,单一函数通过@Param注解传参。
2.单一函数通过@Param注解传参
这就是对方法1的简化与改进,在此就不多讲述。
3.利用Map传参
首先在BlogMapper接口中声明方法:
List<Blog> QueryBlogsByIf2(Map<Object, Object> map);
在BlogMapper.xml中实现接口的方法:
<select id="QueryBlogsByIf2" parameterType="map" resultType="Blog">
select * from mybaties.blog where 1=1
<if test="author != null">
and author=#{author}
</if>
<if test="title != null">
and title=#{title}
</if>
</select>
测试:
@Test
public void queryBlogIf2() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
Map<Object, Object> map = new HashMap<>();
map.put("title", "learn mybatis day 5");
map.put("author", "jms");
List<Blog> blogList = blogMapper.QueryBlogsByIf2(map);
for (Blog blog : blogList) {
System.out.println(blog);
}
sqlSession.close();
}
测试结果:

没有问题。
4.利用JavaBean
首先在BlogMapper接口中声明方法:
List<Blog> QueryBlogsByIf3(Blog blog);
在BlogMapper.xml中实现接口的方法:
<select id="QueryBlogsByIf3" parameterType="Blog" resultType="Blog">
select * from mybaties.blog where 1=1
<if test="author != null">
and author=#{author}
</if>
<if test="title != null">
and title=#{title}
</if>
</select>
测试:
@Test
public void queryBlogIf3() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
Blog blog = new Blog();
blog.setTitle("learn mybatis day 5");
blog.setAuthor("jms");
List<Blog> blogList = blogMapper.QueryBlogsByIf3(blog);
for (Blog blog1 : blogList) {
System.out.println(blog1);
}
sqlSession.close();
}
测试结果:

没有问题。
但是始终有一个问题是不合适的,就是我们SQL语句中的where 1=1,这种情况官方给出了一个解决方法,那就是<where></where>标签。
我们先修改mxl文件中的SQL语句:
select id="QueryBlogsByIf" resultType="Blog">
select * from mybaties.blog
<where>
<if test="author != null">
and author=#{author}
</if>
<if test="title != null">
and title=#{title}
</if>
</where>
</select>
同时官方给出了这样一句说明:
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
(本文仅作个人学习记录用,如有纰漏敬请指正)
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
目录第1题连续问题分析:解法:第2题分组问题分析:解法:第3题间隔连续问题分析:解法:第4题打折日期交叉问题分析:解法:第5题同时在线问题分析:解法:第1题连续问题如下数据为蚂蚁森林中用户领取的减少碳排放量iddtlowcarbon10012021-12-1212310022021-12-124510012021-12-134310012021-12-134510012021-12-132310022021-12-144510012021-12-1423010022021-12-154510012021-12-1523.......找出连续3天及以上减少碳排放量在100以上的用户分析:遇到这类
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
在添加一些空格以使代码更具可读性时(与上面的代码对齐),我遇到了这个:classCdefx42endendm=C.new现在这将给出“错误数量的参数”:m.x*m.x这将给出“语法错误,意外的tSTAR,期待$end”:2/m.x*m.x这里的解析器到底发生了什么?我使用Ruby1.9.2和2.1.5进行了测试。 最佳答案 *用于运算符(42*42)和参数解包(myfun*[42,42])。当你这样做时:m.x*m.x2/m.x*m.xRuby将此解释为参数解包,而不是*运算符(即乘法)。如果您不熟悉它,参数解包(有时也称为“spl
有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.
我想从then子句中访问case语句表达式,即food="cheese"casefoodwhen"dip"then"carrotsticks"when"cheese"then"#{expr}crackers"else"mayo"end在这种情况下,expr是食物的当前值(value)。在这种情况下,我知道,我可以简单地访问变量food,但是在某些情况下,该值可能无法再访问(array.shift等)。除了将expr移出到局部变量然后访问它之外,是否有直接访问caseexpr值的方法?罗亚附注我知道这个具体示例很简单,只是一个示例场景。 最佳答案
ActiveRecord用于在每次调用保存方法时调用after_save回调,即使模型没有更改并且没有生成插入/更新查询也是如此。这实际上是默认行为。在大多数情况下这没问题。但是一些after_save回调对模型是否实际保存的事情很敏感。有没有办法确定模型是否实际保存在after_save中?我正在运行以下测试代码:classStage 最佳答案 ActiveRecordusetocallafter_savecallbackeachtimesavemethodiscalledevenifthemodelwasnotchangedan
我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO