全部条件都相等。
/**
* 使用条件构造器的allEq()方法
*
* @return
*/
public List<UserEntity> getListByAllEq() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("username", "1");
paramsMap.put("pickname", "张三");
queryWrapper.allEq(paramsMap);
return userService.list(queryWrapper);
}
指定条件相等。
/**
* 使用条件构造器的eq()方法
*
* @return
*/
public List<UserEntity> getListByEq() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}
指定条件不相等。
/**
* 使用条件构造器的ne()方法
*
* @return
*/
@GetMapping("/getListByNe")
public List<UserEntity> getListByNe() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().ne(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}
大于指定条件。
/**
* 使用条件构造器的gt()方法
*
* @return
*/
public List<UserEntity> getListByGt() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().gt(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}
大于等于指定条件。
/**
* 使用条件构造器的ge()方法
*
* @return
*/
@GetMapping("/getListByGe")
public List<UserEntity> getListByGe() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().ge(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}
小于指定条件。
/**
* 使用条件构造器的lt()方法
*
* @return
*/
@GetMapping("/getListByLt")
public List<UserEntity> getListByLt() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().lt(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}
小于等于指定条件。
/**
* 使用条件构造器的le()方法
*
* @return
*/
@GetMapping("/getListByLe")
public List<UserEntity> getListByLe() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().le(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}
介于指定范围之间。
/**
* 使用条件构造器的between()方法
*
* @return
*/
@GetMapping("/getListByBetween")
public List<UserEntity> getListByBetween() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().between(UserEntity::getUsername, "111", "123");
return userService.list(queryWrapper);
}
不介于指定范围之间。
/**
* 使用条件构造器的notBetween()方法
*
* @return
*/
@GetMapping("/getListByNotBetween")
public List<UserEntity> getListByNotBetween() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().notBetween(UserEntity::getUsername, "111", "123");
return userService.list(queryWrapper);
}
某个字符串包含指定字符串。
/**
* 使用条件构造器的like()方法
*
* @return
*/
@GetMapping("/getListByLike")
public List<UserEntity> getListByLike() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().like(UserEntity::getUsername, "11");
return userService.list(queryWrapper);
}
某个字符串不包含指定字符串。
/**
* 使用条件构造器的notLike()方法
*
* @return
*/
@GetMapping("/getListByNotLike")
public List<UserEntity> getListByNotLike() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().notLike(UserEntity::getUsername, "11");
return userService.list(queryWrapper);
}
某个字符串以指定字符串结尾。
/**
* 使用条件构造器的likeLeft()方法
*
* @return
*/
@GetMapping("/getListByLikeLeft")
public List<UserEntity> getListByLikeLeft() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().likeLeft(UserEntity::getUsername, "12");
return userService.list(queryWrapper);
}
某个字符串以指定字符串开头。
/**
* 使用条件构造器的likeRight()方法
*
* @return
*/
@GetMapping("/getListByLikeRight")
public List<UserEntity> getListByLikeRight() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().likeRight(UserEntity::getUsername, "12");
return userService.list(queryWrapper);
}
指定字段为null。
/**
* 使用条件构造器的isNull()方法
*
* @return
*/
@GetMapping("/getListByIsNull")
public List<UserEntity> getListByIsNull() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().isNull(UserEntity::getSex);
return userService.list(queryWrapper);
}
指定字段不为null。
/**
* 使用条件构造器的isNotNull()方法
*
* @return
*/
@GetMapping("/getListByIsNotNull")
public List<UserEntity> getListByIsNotNull() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().isNotNull(UserEntity::getSex);
return userService.list(queryWrapper);
}
满足指定条件之一。
/**
* 使用条件构造器的in()方法
*
* @return
*/
@GetMapping("/getListByIn")
public List<UserEntity> getListByIn() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().in(UserEntity::getUsername, "11", "123");
return userService.list(queryWrapper);
}
不满足指定条件之一。
/**
* 使用条件构造器的notIn()方法
*
* @return
*/
@GetMapping("/getListByNotIn")
public List<UserEntity> getListByNotIn() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().notIn(UserEntity::getUsername, "11", "123");
return userService.list(queryWrapper);
}
满足指定条件之一。
/**
* 使用条件构造器的inSql()方法
*
* @return
*/
@GetMapping("/getListByInSql")
public List<UserEntity> getListByInSql() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().inSql(UserEntity::getUsername, "11,123");
return userService.list(queryWrapper);
}
不满足指定条件之一。
/**
* 使用条件构造器的notInSql()方法
*
* @return
*/
@GetMapping("/getListByNotInSql")
public List<UserEntity> getListByNotInSql() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().notInSql(UserEntity::getUsername, "11,123");
return userService.list(queryWrapper);
}
按字段值分组,每一组只会出现一条数据。
/**
* 使用条件构造器的groupBy()方法
*
* @return
*/
@GetMapping("/getListByGroupBy")
public List<UserEntity> getListByGroupBy() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().groupBy(UserEntity::getSex);
return userService.list(queryWrapper);
}
根据指定字段升序排序。
/**
* 使用条件构造器的orderByAsc()方法
*
* @return
*/
@GetMapping("/getListByOrderByAsc")
public List<UserEntity> getListByOrderByAsc() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().orderByAsc(UserEntity::getUsername);
return userService.list(queryWrapper);
}
根据指定字段降序排序。
/**
* 使用条件构造器的orderByDesc()方法
*
* @return
*/
@GetMapping("/getListByOrderByDesc")
public List<UserEntity> getListByOrderByDesc() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().orderByDesc(UserEntity::getUsername);
return userService.list(queryWrapper);
}
根据指定字段升序/降序排序。
/**
* 使用条件构造器的orderBy()方法
*
* @return
*/
@GetMapping("/getListByOrderBy")
public List<UserEntity> getListByOrderBy() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().orderBy(true, false, UserEntity::getUsername);
return userService.list(queryWrapper);
}
(1)第一个参数必须为true。
(2)第二个参数为true则升序排序,为false则降序排序。
跟sql里面的having类似。
/**
* 使用条件构造器的having()方法
*
* @return
*/
@GetMapping("/getListByHaving")
public List<Map<String, Object>> getListByHaving() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.select("password,count(*) as sexCount")
.groupBy("password")
.having("count(*)>1");
return userService.listMaps(queryWrapper);
}
注:
(1)having()需要配合select()、groupBy()一起配合使用。
(2)having里面只能使用聚合函数。
主要方便在出现if...else下调用不同方法能不断链。
/**
* 使用条件构造器的func()方法
*
* @return
*/
@GetMapping("/getListByFunc")
public List<UserEntity> getListByFunc() {
LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.<UserEntity>lambdaQuery();
queryWrapper.func(x -> {
if (true) {
x.eq(UserEntity::getUsername, 1);
} else {
x.eq(UserEntity::getUsername, 1);
}
});
return userService.list(queryWrapper);
}
与逻辑判断。
/**
* 使用条件构造器的and()方法
*
* @return
*/
@GetMapping("/getListByAnd")
public List<UserEntity> getListByAnd() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getPassword, "123456")
.and(t -> t.eq(UserEntity::getSex, "02"));
return userService.list(queryWrapper);
}
或逻辑判断。
/**
* 使用条件构造器的or()方法
*
* @return
*/
@GetMapping("/getListByOr")
public List<UserEntity> getListByOr() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getPassword, "123")
.or(t -> t.eq(UserEntity::getSex, "02"));
return userService.list(queryWrapper);
}
exists用于检查子查询是否会返回数据,该子查询实际上并不返回任何数据,有查询数据返回值为true,没有查询数据返回值为false。
/**
* 使用条件构造器的exists()方法
*
* @return
*/
@GetMapping("/getListByExists")
public List<UserEntity> getListByExists() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.exists("select * from users where password='123'");
return userService.list(queryWrapper);
}
notExists用于检查子查询是否不会返回数据,该子查询实际上并不返回任何数据,有查询数据返回值为false,没有查询数据返回值为true。
/**
* 使用条件构造器的notExists()方法
*
* @return
*/
@GetMapping("/getListByNotExists")
public List<UserEntity> getListByNotExists() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.notExists("select * from users where password='123111'");
return userService.list(queryWrapper);
}
/**
* 使用条件构造器的select()方法
*
* @return
*/
@GetMapping("/getListBySelect")
public List<UserEntity> getListBySelect() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.select("username,password");
return userService.list(queryWrapper);
}
MyBatis-Plus官网教程
美团文章聊聊 clean code
阿里巴巴Java开发手册
阿里巴巴Android开发手册
Java8 Stream:集合的筛选、归约、分组、聚合等...
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or