• UID12
  • 登录2016-06-14
  • 粉丝111
  • 关注50
  • 发帖1415
  • 主页
  • 金币8544枚
社区居民
最爱沙发
忠实会员
喜欢达人
原创写手
极分享 发布于2016-06-11 22:08
0/1256

requery:轻量但功能强大的ORM和SQL查询生成,支持RxJava和Java8

楼层直达

requery:轻量但功能强大的ORM和SQL查询生成,支持RxJava和Java8 

 

requery

 

源码:https://github.com/requery/requery

 

A light but powerful object mapping and SQL generator for Java/Android with RxJava and Java 8 support. Easily map to or create databases, perform queries and updates from any platform that uses Java.

 

Examples

Define entities from an abstract class:

@Entity
abstract class AbstractPerson {

    @Key @Generated
    int id;

    @Index(name = "name_index")              // table specification
    String name;

    @OneToMany                               // relationships 1:1, 1:many, many to many
    Set<Phone> phoneNumbers;

    @Converter(EmailToStringConverter.class) // custom type conversion
    Email email;

    @PostLoad                                // lifecycle callbacks
    void afterLoad() {
        updatePeopleList();
    }
    // getter, setters, equals & hashCode automatically generated into Person.java
}

or from an interface:

@Entity
public interface Person {

    @Key @Generated
    int getId();

    String getName();

    @OneToMany
    Set<Phone> getPhoneNumbers();

    String getEmail();
}

or use immutable types such as those generated by @AutoValue:

@AutoValue
@Entity
abstract class Person {

    @AutoValue.Builder
    static abstract class Builder {
        abstract Builder setId(int id);
        abstract Builder setName(String name);
        abstract Builder setEmail(String email);
        abstract Person build();
    }

    static Builder builder() {
        return new AutoValue_Person.Builder();
    }

    @Key
    abstract int getId();

    abstract String getName();
    abstract String getEmail();
}

(Note some features will not be available when using immutable types, see here)

Queries: dsl based query that maps to SQL

Result<Person> query = data
    .select(Person.class)
    .where(Person.NAME.lower().like("b%")).and(Person.AGE.gt(20))
    .orderBy(Person.AGE.desc())
    .limit(5)
    .get();

Relationships: represent relations more efficiently with Java 8 Streams, RxJava Observables or plain iterables. (sets and lists are supported to)

@Entity
abstract class AbstractPerson {

    @Key @Generated
    int id;

    @ManyToMany
    Result<Group> groups;
    // equivalent to:
    // data.select(Group.class)
    // .join(Group_Person.class).on(Group_ID.equal(Group_Person.GROUP_ID))
    // .join(Person.class).on(Group_Person.PERSON_ID.equal(Person.ID))
    // .where(Person.ID.equal(id))
}

Java 8 streams:

data.select(Person.class)
    .orderBy(Person.AGE.desc())
    .get()
    .stream().forEach(System.out::println);

Java 8 optional and time support:

public interface Person {

    @Key @Generated
    int getId();

    String getName();
    Optional<String> getEmail();
    ZonedDateTime getBirthday();
}

RxJava Observables:

Observable<Person> observable = data
    .select(Person.class)
    .orderBy(Person.AGE.desc())
    .get()
    .toObservable();

RxJava observe query on table changes:

Observable<Person> observable = data
    .select(Person.class)
    .orderBy(Person.AGE.desc())
    .get()
    .toSelfObservable().subscribe(::updateFromResult);

Read/write separation Along with immutable types optionally separate queries (reading) and updates (writing):

int rows = data.update(Person.class)
    .set(Person.ABOUT, "student")
    .where(Person.AGE.lt(21)).get().value();

Features

  • No Reflection
  • Fast startup & performance
  • No dependencies (RxJava is optional)
  • Typed query language
  • Table generation
  • Supports JDBC and most popular databases (MySQL, Oracle, SQL Server, Postgres and more)
  • Supports Android (SQLite, RecyclerView, Databinding, SQLCipher)
  • Blocking and non-blocking API
  • Partial objects/refresh
  • Upsert support
  • Caching
  • Lifecycle callbacks
  • Custom type converters
  • Compile time entity validation
  • JPA annotations (however requery is not a JPA provider)

Reflection free

requery uses compile time annotation processing to generate entity model classes and mapping attributes. On Android this means you get about the same performance reading objects from a query as if it was populated using the standard Cursor and ContentValues API.

Query with Java

The compiled classes work with the query API to take advantage of compile time generated attributes. Create type safe queries and avoid hard to maintain, error prone string concatenated queries.

Relationships

You can define One-to-One, One-to-Many, Many-to-One, and Many-to-Many relations in your models using annotations. Relationships can be navigated in both directions. Of many type relations can be loaded into standard java collection objects or into a more efficient Result type. From a Result easily create a Stream, RxJava Observable, Iterator, List or Map.

Many-to-Many junction tables can be generated automatically. Additionally the relation model is validated at compile time eliminating runtime errors.

vs JPA

requery provides a modern set of interfaces for persisting and performing queries. Some key differences between requery and JPA providers like Hibernate or EclipseLink:

  • Queries maps directly to SQL as opposed to JPQL.
  • Dynamic Queries easily done through a DSL as opposed to the verbose CriteriaQuery API.
  • Uses easily understandable extended/generated code instead of reflection/bytecode weaving for state tracking and member access

Android

Designed specifically with Android support in mind. Comparison to other Android libraries:

Feature requery ORMLite Squidb DBFlow GreenDao
Relational mapping Y Y(1) N Y Y(1)
Inverse relationships Y N N N N
Compile time Y N Y Y Y(2)
Query DSL Y N N(3) N(3) N(3)
JDBC Support Y Y N N N
Table Generation Y Y Y Y Y
JPA annotations Y Y N N N
RxJava support Y N Y(4) N N

1) Excludes Many-to-Many 2) Not annotation based 3) Builder only not DSL 4) Table changes only

See requery-android/example for an example Android project using databinding and interface based entities. For more information see the Android page.

Supported Databases

Tested on some of the most popular databases:

  • PostgresSQL (9.1+)
  • MySQL 5.x
  • Oracle 12c+
  • Microsoft SQL Server 2012 or later
  • SQLite (Android or with the xerial JDBC driver)
  • Apache Derby 10.11+
  • H2 1.4+
  • HSQLDB 2.3+

JPA Annotations

A subset of the JPA annotations that map onto the requery annotations are supported. See here for more information.

Upserts

Upserts are generated with the appropriate database specific query statements:

  • Oracle/SQL Server/HSQL: merge into when matched/not matched
  • PostgresSQL: on conflict do update (requires 9.5 or later)
  • MySQL: on duplicate key update

Using it

Currently beta versions are available on bintray jcenter / maven central.

repositories {
    jcenter()
}

dependencies {
    compile 'io.requery:requery:1.0.0-beta22'
    compile 'io.requery:requery-android:1.0.0-beta22' // for android
    apt 'io.requery:requery-processor:1.0.0-beta22'   // use an APT plugin
}

For information on gradle and annotation processing & gradle see the wiki.

License

Copyright (C) 2016 requery.io

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

0人打赏
您需要登录后才可以回帖
发表回复
极贡献
技术问答
专题荟萃
程序人生
视觉设计
Android开发
iOS开发
编程语言
前端开发
后端开发
服务器架构
软件测试
运维方案
创业路上



最热文章墙

  • 70506/366   【精品推荐】200多种Android动画效果的强悍框架,太全了,不看这个,再有动画的问题,不理你了^@^

  • 40104/190   情人节福利,程序员表白的正确姿势:改几行代码就变成自己的表白了

  • 37795/0   Python爬虫:常用浏览器的useragent

  • 36810/259   【精品推荐】Android版产品级的音乐播放器源码,功能太强大了,最好的产品原型有木有?

  • 35801/145   省时省力的Android组件群来了,非常棒的原型参考

  • 28125/2   超全!整理常用的iOS第三方资源

  • 27431/71   原创表白APP,以程序员的姿势备战新年后的7夕,持续完善中!

  • 27035/142   2016抢红包软件及源码

  • 21789/30   麻省理工的一帮疯子,真的实现了随意操控万物!(绝对黑科技)

  • 21419/25   Android工程师面试题大全

  • 21213/27   2016程序员跳槽全攻略

  • 21131/155   Android版类似UC浏览器:非常赞,产品级的源码

  • 21044/9   GitHub上排名前50的iOS项目:总有一款你用得着

  • 20015/20   码魂:程序员的牛B漫画

  • 18234/10   2016年最全的Android面试考题+答案 精编版

  • 17997/3   吐槽那些程序员的搞笑牛逼注释

  • 17461/42   一个绚丽的loading动效分析与实现!

  • 17264/85   Android小而全的博客源码:非常适合全面掌握开发技巧

  • 17175/73   【持续更新中】Android福利贴(二):资料源码大放送

  • 16939/1   iOS 动画总结

  • 16313/45   惊艳的App引导页:背景图片切换加各个页面动画效果

  • 16044/23   个人收集的Android 各类功能源代码

  • 15791/10   女程序员的梦,众网友的神回复

  • 15664/81   仿京东商城客户端Android最新版,不错的原型和学习资料

  • 15594/5   新一代Android渠道打包工具:1000个渠道包只需要5秒

  • 15496/11   年会上现场review代码是怎么样的体验!

  • 15225/103   Android带弹幕的视频播放器源码,来自大名鼎鼎的Bilibili弹幕网站

  • 15091/53   基于瀑布流的美女图片浏览App,有注释的源代码

  • 15074/20   Android福利第三波【Android电子书】

  • 15027/17   用JavaScript 来开发iOS和Android 原生应用:React Native开源框架中文版来啦

  • 15013/23   珍藏多年的素材,灵感搜寻网站

  • 14581/78   【精品推荐】类似360安全卫士安Android源码:非常赞的产品原型

  • 14388/0   iOS中文版资源库,非常全

  • 14142/18   65条最常用正则表达式,你要的都在这里了

  • 14007/15   基于Android支付宝支付设计和开发方案

  • 13476/17   什么是真正的黑客:收获12200+Stars,人气远超微软开源VS

  • 13189/11   有木有这样一张酷图帮你集齐所有git命令超实用

  • 12849/7   一张图搞定iOS学习路线,非常全面

  • 12704/7   用程序员的姿势抢过年的火车票

  • 12680/46   在线音乐播放器完整版(商用级的源码):非常赞,可听免费高品质专辑

  • 12447/10   成为Java顶尖程序员 ,看这11本书就够了

  • 12344/0   GitHub iOS 库和框架Top100 

  • 12292/10   微信支付终于成功了(安卓,iOS),在此分享

  • 12253/18   一张图搞定Android学习路线,非常全面

  • 11971/61   【技巧一】搭配Android Studio,如何实现App远程真机debug?

  • 11932/3   基于Node.js的强大爬虫,能直接发布抓取的文章哦

  • 11795/29   【持续更新中】Android福利贴(一):资料源码

  • 11176/4   46 个非常有用的 PHP 代码片段

  • 11005/3   即时通信第三方库

  • 10707/8   流媒体视频直播方案

  • 10575/18   八个最优秀的Android Studio插件

  • 10575/9   B站建开源工作组:APP想支持炫酷弹幕的看过来

  • 10398/9   烧了5亿美金,这家神秘的公司即将颠覆人类未来!

  • 10253/2   【精品推荐】高质量PHP代码的50个实用技巧:非常值得收藏

  • 10243/10   中国黑客的隐秘江湖:攻守对立,顶尖高手月入千万美元

  • 9639/6   开箱即用!Android四款系统架构工具

  • 9451/10   十大技巧快速提升Android应用开发性能

  • 9387/3   10款GitHub上最火爆的国产开源项目——可以媲美西半球

  • 9342/3   一张图看清Linux 内核运行原理

  • 9175/1   Android性能优化视频,文档以及工具

  • 返回顶部