1、Hibernate是全ORM(对象关系映射)框架,利用完整的javabean对象与数据库映射结构来自动生成sql。
2、Mybatis是半ORM框,仅有字段映射,需要手写sql语句和对象字段结合生成最终的执行sql语句。
3、Mybatis-plus是Mybatis的增强版,支持所有Mybatis的原生特性。核心的特性的是ActiveRecord,实体类只需继承 Model 类即可实现基本 CRUD 操作。
mybtis-plus适合快速地单表CRUD,将业务逻辑写在代码服务层,不必再拼接复杂的sql。
复杂sql在数据库层面执行效率低,且数据库不适于扩容增加性能;
代码层面结合java8的流式编程,可以快速进行数据的聚合、过滤处理。
官方讲的比较详细:https://baomidou.com/pages/24112f/
/**
* <p>
* 根据根据 entity 条件,删除记录,QueryWrapper实体对象封装操作类(可以为 null)
* 下方获取到queryWrapper后删除的查询条件为name字段为null的and年龄大于等于12的and email字段不为null的
* 同理写法条件添加的方式就不做过多介绍了。
* </p>
/
@Test
public void delete() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.isNull("name")
.ge("age", 12)
.isNotNull("email");
int delete = mapper.delete(queryWrapper);
System.out.println("delete return count = " + delete);
}
/*
* <p>
* 根据 entity 条件,查询一条记录,
* 这里和上方删除构造条件一样,只是seletOne返回的是一条实体记录,当出现多条时会报错
* </p>
/
@Test
public void selectOne() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "lqf");
User user = mapper.selectOne(queryWrapper);
System.out.println(user);
}
/*
* <p>
* 根据 Wrapper 条件,查询总记录数
* </p>
*
* @param queryWrapper 实体对象
/
@Test
public void selectCount() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "lqf");
Integer count = mapper.selectCount(queryWrapper);
System.out.println(count);
}
/*
* <p>
* 根据 entity 条件,查询全部记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)为null查询全部
/
@Test
public void selectList() {
List<User> list = mapper.selectList(null);
System.out.println(list);
}
/*
* <p>
* 根据 Wrapper 条件,查询全部记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
/
@Test
public void selectMaps() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNotNull("name");
List<Map<String, Object>> maps = mapper.selectMaps(queryWrapper);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
/*
* 打印结果
* {name=lqf, id=1046282328366391406, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391407, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391408, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391409, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391410, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391411, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391412, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391413, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391414, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391415, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391416, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391417, age=12, email=lqf@163.com, status=false}
* {name=lqf, id=1046282328366391418, age=12, email=lqf@163.com, status=false}
* json类型的键值对模式
/
/*
* <p>
* 根据 entity 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
/
@Test
public void selectPage() {
Page<User> page = new Page<>(1, 5);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
IPage<User> userIPage = mapper.selectPage(page, queryWrapper);
System.out.println(userIPage);
}
/*
* 打印结果
* ==> Preparing: SELECT COUNT(1) FROM user
* ==> Parameters:
* <== Columns: COUNT(1)
* <== Row: 100
* ==> Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5
* ==> Parameters:
* <== Columns: id, name, age, email, status
* <== Row: 1046282328366391319, lqf, 12, lqf@163.com, 0
* <== Row: 1046282328366391320, lqf, 12, lqf@163.com, 0
* <== Row: 1046282328366391321, lqf, 12, lqf@163.com, 0
* <== Row: 1046282328366391322, lqf, 12, lqf@163.com, 0
* <== Row: 1046282328366391323, lqf, 12, lqf@163.com, 0
* <== Total: 5
*
*
* 这里需要在项目中加入分页插件
* @Bean
* public PaginationInterceptor paginationInterceptor() {
* return new PaginationInterceptor();
* }
/
/*
* <p>
* 根据 Wrapper 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
/
@Test
public void selectMapsPage() {
Page<User> page = new Page<>(1, 5);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
IPage<Map<String, Object>> mapIPage = mapper.selectMapsPage(page, queryWrapper);
System.out.println(mapIPage);
}
/*
* 和上个分页同理只是返回类型不同
/
/*
* <p>
* 根据 whereEntity 条件,更新记录
* </p>
*
* @param entity 实体对象 (set 条件值,不能为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
/
@Test
public void update() {
//修改值
User user = new User();
user.setStatus(true);
user.setName("zhangsan");
//修改条件s
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name", "lqf");
int update = mapper.update(user, userUpdateWrapper);
System.out.println(update);
}
/*
* 打印结果
* ==> Preparing: UPDATE user SET name=?, status=? WHERE name = ?
* ==> Parameters: zhangsan(String), true(Boolean), lqf(String)
* <== Updates: 100
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56a4f272]
* 100
* 2018-10-02 15:08:03.928 INFO 7972 --- [ Thread-2] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@37313c65: startup date [Tue Oct 02 15:08:00 CST 2018]; root of context hierarchy
* 2018-10-02 15:08:03.937 INFO 7972 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
* 2018-10-02 15:08:04.053 INFO 7972 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
*
* Process finished with exit code 0
*/
}
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我在Rails上使用带有ruby的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s
我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params
我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使
例如,假设我有一个名为Products的模型,并且在ProductsController中,我有以下代码用于product_listView以显示已排序的产品。@products=Product.order(params[:order_by])让我们想象一下,在product_listView中,用户可以使用下拉菜单按价格、评级、重量等进行排序。数据库中的产品不会经常更改。我很难理解的是,每次用户选择新的order_by过滤器时,rails是否必须查询,或者rails是否能够以某种方式缓存事件记录以在服务器端重新排序?有没有一种方法可以编写它,以便在用户排序时rails不会重新查询结果
我目前正在尝试了解RoR。我将两个字符串传递到我的Controller中。一个是随机的十六进制字符串,另一个是电子邮件。该项目用于对数据库进行简单的电子邮件验证。我遇到的问题是当我输入如下内容来测试我的页面时:http://signup.testsite.local/confirm/da2fdbb49cf32c6848b0aba0f80fb78c/bob.villa@gmailcom我在:email的参数散列中得到的全部是'bob'。我在gmail和com之间留下了.,因为那样会导致匹配根本不起作用。我的路由匹配如下:match"confirm/:code/:email"=>"conf
我正在寻找一种方便实用的方法来将编码值添加到Ruby中的URL查询字符串。目前,我有:require'open-uri'u=URI::HTTP.new("http",nil,"mydomain.example",nil,nil,"/tv",nil,"show="+URI::encode("Rosie&Jim"),nil)pu.to_s#=>"http://mydomain.example/tv?show=Rosie%20&%20Jim"这不是我要找的,因为我需要得到“http://mydomain.example/tv?show=Rosie%20%26%20Jim”,这样show=值就
目录H2数据库入门以及实际开发时的使用1.H2数据库的初识1.1H2数据库介绍1.2为什么要使用嵌入式数据库?1.3嵌入式数据库对比1.3.1性能对比1.4技术选型思考2.H2数据库实战2.1H2数据库下载搭建以及部署2.1.1H2数据库的下载2.1.2数据库启动2.1.2.1windows系统可以在bin目录下执行h2.bat2.1.2.2同理可以通过cmd直接使用命令进行启动:2.1.2.3启动后控制台页面:2.1.3spring整合H2数据库2.1.3.1引入依赖文件2.1.4数据库通过file模式实际保存数据的位置2.2H2数据库操作2.2.1Mysql兼容模式2.2.2Mysql模式