5 个最好的 Android ORM 框架

本文介绍了几个在Android应用中使用的ORM框架,包括OrmLite、SugarORM、GreenDAO、ActiveAndroid和Realm,提供了它们的基本使用方法和性能对比,并讨论了如何在实际项目中选择合适的ORM框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转:http://www.open-open.com/news/view/1ca9196

如果你正在开发一个Android应用程序,您可能需要在某个地方存储数据。您可以选择云服务(在这种情况下,使用 SyncAdapter 将是一个不错的主意),或存储您的数据在嵌入式SQLite数据库中。如果你选择第二个选项,你可能需要使用ORM。 在这篇文章中,将介绍一些你可以考虑在你的Android应用程序中使用的ORM框架。

 

OrmLite

OrmLite is the first Android ORM that comes to my mind. However OrmLite is not an Android ORM, it’s a Java ORM with SQL databases support. It can be used anywhere Java is used, such as JDBC connections, Spring, and also Android.

It makes heavy usage of annotations, such as @DatabaseTable for each class that defines a table, or @DatabaseField for each field in the class.

A simple example of using OrmLite to define a table would be something like this:

01 @DatabaseTable(tableName = "users")
02 public class User {
03     @DatabaseField(id = true)
04     private String username;
05     @DatabaseField
06     private String password;
07  
08     public User() {
09         // ORMLite needs a no-arg constructor
10     }
11     public User(String username, String password) {
12         this.username = username;
13         this.password = password;
14     }
15  
16     // Implementing getter and setter methods
17     public String getUserame() {
18         return this.username;
19     }
20     public void setName(String username) {
21         this.username = username;
22     }
23     public String getPassword() {
24         return this.password;
25     }
26     public void setPassword(String password) {
27         this.password = password;
28     }
29 }

OrmLite for Android is open source and you can find it on GitHub. For more information readits official documentation here.

SugarORM

SugarORM is an ORM built only for Android. It comes with an API which is both simple to learn and simple to remember. It creates necessary tables itself, gives you a simple methods of creating one-to-one and one-to-many relationships, and also simplifies CRUD by using only 3 functions, save()delete() and find() (or findById()).

Configure your application to use SugarORM by adding these four meta-data tags to your apps AndroidManifest.xml:

1 <meta-data android:name="DATABASE" android:value="my_database.db" />
2 <meta-data android:name="VERSION" android:value="1" />
3 <meta-data android:name="QUERY_LOG" android:value="true" />
4 <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.my-domain" />

Now you may use this ORM by extending it in the classes you need to make into tables, like this:

01 public class User extends SugarRecord<User> {
02     String username;
03     String password;
04     int age;
05     @Ignore
06     String bio; //this will be ignored by SugarORM
07  
08     public User() { }
09  
10     public User(String username, String password,int age){
11         this.username = username;
12         this.password = password;
13         this.age = age;
14     }
15 }

So adding a new user would be:

1 User johndoe = new User(getContext(),"john.doe","secret",19);
2 johndoe.save(); //stores the new user into the database

Deleting all the users of age 19 would be:

1 List<User> nineteens = User.find(User.class,"age = ?",new int[]{19});
2 foreach(user in nineteens) {
3     user.delete();
4 }

For more, read SugarORM’s online documentation.

GreenDAO

When it comes to performance, ‘fast’ and GreenDAO are synonymous. As stated on its website, “most entities can be inserted, updated and loaded at rates of several thousand entities per second. If it wasn’t that good, these apps wouldn’t be using it. Compared to OrmLite, it is almost 4.5 times faster.

greendao-performance.png
greenDAO vs OrmLite

Speaking of size, it is smaller than 100kb, so doesn’t affect APK size very much.

Follow this tutorial, which uses Android Studio to show the usage of GreenDAO in an Android application. You can view the GreenDAO source code on GitHub, and read the GreenDAO official documentation.

Active Android

Much like other ORMs, ActiveAndroid helps you store and retrieve records from SQLite without writing SQL queries.

Including ActiveAndroid in your project involves adding a jar file into the /libs folder of your Android project. As stated in the Getting started guide, you can clone the source code from GitHub and compile it using Maven. After including it, you should add these meta-data tags into your app’s AndroidManifest.xml:

1 <meta-data android:name="AA_DB_NAME" android:value="my_database.db" />
2 <meta-data android:name="AA_DB_VERSION" android:value="1" />

After adding these tags, you can call ActiveAndroid.initialize() in your activity like this:

1 public class MyActivity extends Activity {
2     @Override
3     public void onCreate(Bundle savedInstanceState) {
4         super.onCreate(savedInstanceState);
5         ActiveAndroid.initialize(this);
6  
7         //rest of the app
8     }
9 }

Now that the application is configured to use ActiveAndroid, you may create Models as Java classes by using Annotations:

01 @Table(name = "User")
02 public class User extends Model {
03     @Column(name = "username")
04     public String username;
05  
06     @Column(name = "password")
07     public String password;
08  
09     public User() {
10         super();
11     }
12  
13     public User(String username,String password) {
14         super();
15         this.username = username;
16         this.password = password;
17     }
18 }

This is a simple example of ActiveAndroid usage. The documentation will help you understand the usage of ActiveAndroid ORM further.

Realm

Finally Realm is a ‘yet-to-come’ ORM for Android which currently only exists. It is built on C++, and runs directly on your hardware (not interpreted) which makes it really fast. The code for iOS is open source, and you can find it on GitHub.

On the website you will find some use cases of Realm in both Objective-C and Swift, and also a Registration form to get the latest news for the Android version.

Final words

These are not the only Android ORMs on the market. Other examples are Androrm and ORMDroid.

SQL knowledge is a skill that every developer should have, but writing SQL queries is boring, especially when there are so many ORMs out there. When they make your job simpler, why not use them in the first place?

How about you? What Android ORM do you use? Comment your choice below


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值