android开发Android知识Android开发

Gluttony:Model无需任何处理,即可使用的,超方便An

2016-11-21  本文已影响144人  森与渊

说是ORM框架,其实更该说是Android 数据库领域的 DSL 语言。(领域特定语言)
所以用起来自然超简约。

不多说了,Github地址:https://github.com/senyuyuan/Gluttony
文档如下:

Gluttony · 饕餮

Gluttony, a super convenient and simple library to using Android database

饕餮,高效而简约地使用Android数据库

Library is compatible with Kotlin 1.0.5-2 build.

饕餮 兼容 kotlin 1.0.5-2 版本(最新版)

Content · 目录

Feature · 特性

* The core concept, friendly to human, Gluttony will give you the perfect experience
* 核心理念,对人类友好,Gluttony将会给您完美的使用体验
* Automatic table and column naming through reflection
* 通过反射自动建表
* Entities do not need any processing
* 实体类无需任何处理
* The future will provide two kinds of the kernel for your choice, 
  【sqlite】 and 【realm】, the performance of the pursuit of perfection
* 未来将会提供两种内核供您选择,【sqlite】以及【realm】,追求极致的性能

Install · 安装

as a gradle dependency

作为一个gradle依赖库

    compile 'sen.yuan.dao:gluttony:1.1'

How to use · 如何使用

Configuration · 配置

        //configure databaseName,cursorFactory,databaseVersion 
        //配置 数据库名称,cursorFactory,数据库版本
        Gluttony.init(this, GluttonyConfig("gluttony_example",null,1))

In Application or the first Activity, initialize Gluttony.

在Application或是首个Activity中,初始化Gluttony·饕餮。

Entities · 实体

Entities do not need to do any processing.

实体类无需做任何处理。

Gluttony automatically for you to take care of everything.

Gluttony·饕餮 在数据库中自动为您打理好一切。

Annotation: @PrimaryKey is used to specify a PrimaryKey.

注解:@PrimaryKey 用来指定主键。

data class UserData(
        @PrimaryKey
        var id: Int = -1,
        var name: String = "",
        var age: Int = -1,
        var isChildren: Boolean = false
)

Save · 保存

Save Entity directly · 直接保存实体

        val user = UserData()
        user.id = 666
        user.name = "sen"
        user.age = 23
        user.isChildren = false
        user.save()

        //or
        UserData(2, "john", 12, true).save()

Find · 查询

Find Entity based on PrimaryKey · 根据primary key 查询数据

        val user1 = UserData().findOneByKey(666)

Find the first Entity based on Condition · 根据条件 查询第一个数据

        val user2 = UserData().findOne {
            condition {
                "age" between 7..16
                "isChild" Not false
            }
            orderBy("age", SqlOrderDirection.ASC)
        }

Find all Entities based on Condition · 根据条件 查询所有数据,返回值为一个列表

        val userList = UserData().findAll {
            condition {
                "age" moreThan 11
                "name" like "s%"    //find names witch is starting with "s"
            }
        }

Update · 更新

Update Entity directly base on PrimaryKey · 根据primary key 直接更新实体

    var user3 = UserData().findOne { condition { "name" equalsData "lucy" } }!!
        user3.age += 1
        user3.update()
        
        //or
        
        var user4 = UserData(7, "lucy", 10, true)   //user4.id == user3.id · 注意primary key相同
        user4.update()  //user4 will overwrite the old data · 将会覆盖掉旧数据

Update Entity directly or Save Entity when it doesn't exist · 直接 更新或保存实体 (如果实体是未保存过的话)

        var user5 = UserData(90, "white", 77, false)    // 90 is a new primary key
        user5.updateOrSave()    // Gluttony will save a new data

Update Entity based on PrimaryKey · 根据primary key 更新实体

    //update user who id is 90 to named black,age 80
    //lambda
        UserData().updateByKey(90) { arrayOf("name" to "black", "age" to 80) }

        //or pairs
        UserData().updateByKey(90, "name" to "green", "age" to 82)

Update all Entities based on Condition · 根据条件 更新所有实体

    //update user who name is green to name red,age 99
        UserData().update("name" to "red", "age" to 99) {
            condition {
                "name" equalsData "green"
            }
        }

Delete · 删除

Delete Entity directly · 直接删除实体

        var user9 = UserData(90)    //only need primary key
        user9.delete()

Delete Entity based on PrimaryKey · 根据primary key 删除实体

        UserData().deleteByKey(666) //delete user which id is 666

Delete all Entities based on Condition · 根据条件 删除所有实体

    //delete users who is child
        UserData().delete {
            condition {
                "isChild" equalsData true
            }
        }

Clear one Class's entities · 清空一个类的所有实体

        UserData().clear()

Condition · 条件


two marks: % and _ · 两个通配符:% 和 _

% : indefinite amount content · 不定数量的内容

_ : one amount content · 一个位置的内容

for example:    "gluttony%" -> find values witch is starting with "gluttony"

例如,"gluttony%" -> 查询所有以"gluttony"开头的数据

Welcome to staring the project, thanks!
欢迎关注(star)本项目,O(∩_∩)O谢谢!

QQ群:176275050

上一篇下一篇

猜你喜欢

热点阅读