您的位置:首页 > 其它

sugar orm使用介绍

2016-05-31 16:06 411 查看
首先介绍一下sugar orm为什么诞生!!!

借用官网上面的几句话:

a.Fairly large and complex data model.

b.Primary work was to save/retrieve and iterate over the business objects.

c.Lot of boiler plate code for the db operations.

d.We wanted to write our business logic in less technical and more business fashion.

接下来,步入正题!!!!!!!

本人用得是androidStudio,用eclipse的童鞋们请前往官网down jar包~~

第一步,gradle引入!!!

compile ‘com.github.satyan:sugar:1.5’

第二步,manifest配置它

<application android:label="@string/app_name" android:icon="@drawable/icon"
android:name="com.orm.SugarApp">
<meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
</application>


数据库的名字,版本号,log,你的JavaBean所在的包(会自动找到这个包,通过映射关系,给你生成对应的表)

血泪提示:

第一,别忘了在你的Application类的oncreate和onTerminate中调用sugarContext对应的方法,当然也可以直接让你的application类继承SugarAPP

第二,假如使用的是androidstudio2.0以上版本,第一次使用sugar 不要开启intant run模式,不然建表不成功,会一直报错,crash掉。。。

第三步:建表,这里你的JavaBean的类名就是表名

写一个javaBean吧

有两种使用sugar orm建立javaBean的方式

1.继承sugarRecord类,默认的空构造函数一定要写,完全按照规范来,人家就是这么说的

public class Book extends SugarRecord {
String title;
String edition;

public Book(){
}

public Book(String title, String edition){
this.title = title;
this.edition = edition;
}
}


2.使用注解,用这种方式的话,你就要手动定义一个id字段,作为默认的主键,类型long!!!必须得做得!!!

@Table
public class Book {
private Long id;

public Book(){
}

public Book(String title, String edition){
this.title = title;
this.edition = edition;
}

public Long getId() {
return id;
}
}


Ps:1.如果某个字段不是表字段不想添加的表中,直接加上@Ignore这个注解,就可以了 2.还有一个就是假如一个字段的名字是shortSummary,那么它对应到表的列名称其实是short_summary,这个需要注意一下。

第四步:简单用法

到这里基本上就差不多了,你就可以来操作你的表了!!!

CURD如下:

Save Entity:
Book book = new Book("Title here", "2nd edition")
book.save();
Load Entity:
Book book = Book.findById(Book.class, 1);
Update Entity:
Book book = Book.findById(Book.class, 1);
book.title = "updated title here"; // modify the values
book.edition = "3rd edition";
book.save(); // updates the previous entry with new values.
Delete Entity:
Book book = Book.findById(Book.class, 1);
book.delete();
Bulk Operations:
List<Book> books = Book.listAll(Book.class);
Book.deleteAll(Book.class);
Book.find(Book.class, "name = ? and title = ?", "satya", "title1")

find(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit)


更多用法:

1.两张表建立关系,例如book和author,像这种表关系,存的时候要先存author再存book

book肯定是由人写得,所以会有一个作者

book–>author

public class Author extends SugarRecord {
String name;

public Author() {}

// Get all books of this author
List<Book> getBooks() {
return Book.find(Book.class, "author = ?", new String{getId()})
}
}


public class Book extends SugarRecord {
String name;
String ISBN;
String title;
String shortSummary;

// defining a relationship
Author author;
}


最后说一下数据库的更新操作:

1.在assets目录中创建一个sugar_upgrades的文件夹

2.创建升级版本的文件,命名规范是.sql。例如 1.sql 2.sql

3.然后将你manifest里面里面的version改为对应的升级版本号

4.如果目前是1版本,现在要更新到4版本,suagr会从2.sql,3.sql,4.sql挨个去查询你的更新记录

5.最关心的来了,就是sql文件里面的内容是啥呢?? 支持脚本语言!!!

例如,你某一次更新想在book表里加一个字段只需要这样

alter table BOOK add NAME TEXT;

6.最后说一句,upgrade的代码每一行都要以“;”结尾!!!

**原话在此:

This is a normal sql script file. You can add all your alter and insert/update queries, one line at a time. Each line is terminated by a ; (semicolon).**
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: