Create an android ORM framework opendroid (1) -- use of the orm framework, ormopendroid

Source: Internet
Author: User

Create an android ORM framework opendroid (1) -- use of the orm framework, ormopendroid

I. My opinion

I remember a blog about the use of litepal. In this blog, I mentioned that the APIS provided by android have been encapsulated enough, I don't need any ORM framework at all, but after using litepal, I feel it is necessary to use the ORM framework. The following are my views on ORM and android APIs:

1. As an API level, android can only encapsulate an API in a broad sense, but cannot encapsulate an API in a specific way. Therefore, android can encapsulate sqlite very effectively.

2. As a developer, we need to provide sufficient adaptation solutions for the project. It is possible that the ORM framework is more suitable for the project than the API.

Therefore, The ORM framework does not conflict with the native API. Instead of using the ORM, We have to discard the native API. Even, we can use the native API for secondary encapsulation to encapsulate An ORM framework, this is also the topic of this small series of blogs-using native APIs to build our own ORM framework.


Ii. Enter the topic

First, we will introduce an opendroid ORM framework. Then, we will spend several minutes learning how to use this framework. Here, learning to use this framework is not an aim, but to know how to use it, to implement an ORM. Of course, opendroid (My ORM framework name is opendroid. Is it a cool name ?) It takes only four days to develop the SDK. Currently, only the most basic CRUD and Database Upgrade solutions are supported, and a large number of bugs may exist, but the goal has been achieved, it is to make an ORM of its own.


3. Use opendroid

If you have used the ORM framework (no matter what platform) before, you will soon be able to get started with opendroid. If you have not used the ORM, it doesn't matter, I believe it can be done only once!

1. First, you need to download opendroid. Now I open source this project to git @ osc. You can download the jar package and source code at the following address:

Http://git.oschina.net/qibin/OpenDroid

2. Copy opendroid. jar in the library to the libs directory of the project.

3. modify the Application in the AndroidMenifest. xml file and add anrdroid: name = "android: name =" org. loader. opendroid. app. DroidApplication"

4. Create a java bean to be mapped to the database.

5. Create the open_droid.xml file in the asserts directory and copy the code to the open_droid.xml file.

<?xml version="1.0" encoding="utf-8"?><open-droid>    <name value="school" /><version value="1" /><mapping class="org.loader.opendroid.Student" /></open-droid>
In this xml, the name node specifies the database name; the version node specifies the database version. In future development, you only need to modify the value to upgrade the database; mapping may use multiple java Beans mapped to data tables. The database corresponding to Student here will certainly have a Student table.

6. Create a java bean file mapped to the database

package org.loader.opendroid;import org.loader.opendroid.db.OpenDroid;public class Student extends OpenDroid {private String stuName;private int stuAge;public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;}}
A common java bean defines two fields and provides the getter and setter methods. It must be noted that Student inherits the OpenDroid class, which is a class in our opendroid library.

7. start to use the CRUD function of opendroid.

After the above configuration, we can easily implement the CRUD function in the business,

7.1 Insert data:

Student stu = new Student (); stu. setStuName ("Bin"); stu. setStuAge (18); stu. save ();
After a series of sets, you only need to call the save method in Student to save the data to the database. Here you may have two questions: 1) Where is the database created? 2) Where does the save method come from? This method is not seen in Student.

Answer the above question:

1) Where is the database created? -- When database operations are performed, opendroid will automatically help us create tables with specified entities in the database and mapping.

2) Where does the save method come from? -- Of course, it is inherited from OpenDroid. This method will be mentioned in future blogs when implementing opendroid.

7.2. Update Data

Opendroid provides a series of methods to simplify update operations.

Student stu = new Student();stu.setStuName("loader");stu.update("_id>?", "4");
The above code updates stuName with id greater than 4 to loader. You may have another question: we have not defined the id in Student. How can we use id here? Yes. You need to create an id field when defining the object class. opendroid will automatically create a _ id field for us.

What are the update operations?

Student stu = new Student();stu.setStuName("loader");stu.update(4);
The above code updates stuName with id 4 to loader.

Of course, you can also use ContentValues for updates:

ContentValues cv = new ContentValues();cv.put("stuName", "opendroid");OpenDroid.update(Student.class, cv, "_id=?", "8");
It is easy to understand that this is to update stuName with id greater than 8 to opendroid.

Well, there are so many update operations. I believe it is enough to cope with daily development work.

7.3 delete data

The delete function of opendroid is also very simple, and it is as intuitive as the above Code.

int length = OpenDroid.delete(Student.class, 1, 2, 3);System.out.println(length);
You can delete a row of Code. The Code above deletes data with id 1, 2, or 3.

Conditional Deletion

int length = OpenDroid.delete(Student.class, "_id>?", "5");System.out.println(length);
The above code is used to delete data with an id greater than 5.

In CRUD, the query operation is relatively troublesome. Of course, opendroid also encapsulates the query operation.

Student result = OpenDroid.query.find(Student.class, 1);System.out.println(result.getStuName());

The code above is to query the data with id 1 and map the query result to the Student class.

List<Student> result = OpenDroid.query.find(Student.class);for(Student res : result) {System.out.println(res.getStuName());}
Haha, omitting the condition is to query all the data. Of course, the List set returned here.


List<Student> result = OpenDroid.query.find(Student.class, 1, 5, 10);for(Student res : result) {System.out.println(res.getId() + " : " + res.getStuName());}

Query the data whose id is 1, 5, or 10. Note that it is the method of calling a static variable query in OpenDroid.

List<Student> result = OpenDroid.query.columns("stuName", "stuAge").where("_id>?", "5").order("_id DESC").limit(3)
<span></span>     .find(Student.class);for(Student res : result) {System.out.println(res.getStuName() + " : " + res.getStuAge());}

Of course, query also provides a series of methods to set where, order, limit, and so on in the query statement,

The columns () method specifies the fields to be queried.

The where () method specifies the query conditions.

The order () method is used to specify the data arrangement for query.

The limit () method specifies the limit of the select statement.

Finally, call the find method to query the data.


Without knowing it, we have mastered the most important crud operations in opendroid. I believe it is easy to understand the code here. Let's take a look at how to use opendroid to upgrade the database.

1. Modify the value of the version field in the open-droid.xml file.

2. add or delete mapping, or adjust the fields of an instance class based on service upgrade requirements.

The preceding two steps are required. When you use the database next time, opendroid will automatically upgrade the database and update the current data to the new table, there is no need to worry about data loss during the Database Upgrade process. Of course, the discarded field opendroid will be automatically ignored.


We have mastered the use of opendroid as part of a blog. The usage of most ORM frameworks is not bad, so you have mastered the basic ORM syntax.

In the following blogs, I will explain how opendroid is implemented step by step and finally build An ORM framework of your own!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.