Build Android ORM Framework Opendroid (i) Use of the--orm framework

Source: Internet
Author: User

First, my view

I remember that there was a blog about the use of Litepal, which I mentioned in this blog: I thought that the API provided by Android itself was already packaged well enough to have no ORM framework at all, but after using Litepal, I felt it was necessary to use the ORM framework. , here are my thoughts on ORM and the Android API:

1, as the API level, Android can only be generalized encapsulation, and can not be specific to encapsulate an API, so the Android package for SQLite is very powerful.

2. As a developer, we need to provide a solution that is adequate for the project, and the ORM framework may be more suitable for the project than the API.

So, the ORM framework and the native API do not conflict, not using the ORM to abandon the native API, and even we can use the native API two times package, encapsulate an ORM framework, this is the topic of this small series blog-the use of native API to build our own ORM framework.


Ii. entry into the theme

Let's start by introducing a opendroid's own ORM framework and then using it for a few minutes to learn to use the framework, where learning to use is not an end, but rather to implement an ORM if you know how to use it. Of course opendroid (my ORM framework name is called Opendroid, is it a very nasty name?) ) has been developed for only 4 days of fragmented, so currently only supports the most basic crud and database upgrade scenarios, and there are likely to be a lot of bugs, but the goal has been achieved, is to make a self-orm.


Third, the use of opendroid

If you've used an ORM framework before (no matter what the platform is), you'll soon be able to get started with opendroid, and if you haven't used an ORM, it's okay to believe it's just one more time to get it done!

1, first need to download opendroid, now this project I open source to [email protected], can go to the following address download jar package and source code

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

2. After downloading, copy the Opendroid.jar in the library to the Libs directory of the project.

3, modify the Androidmenifest.xml file application, add Anrdroid:name= "Android:name=" org.loader.opendroid.app.DroidApplication "

4. Create a new Java bean that will be mapped to the database.

5. Create a new Open_droid.xml file in the asserts directory and copy the code into 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 names of the databases, the version node specifies the versions of the database, and in future development only the values of value can be modified to enable the database to be upgraded; mapping may use multiple, specified Java bean with data table mapping , there will certainly be a student table in the corresponding database for student.

6. Create a Java Bean file with database mappings

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 very common Java bean, which defines two fields and provides getter and setter methods, it is important to note that student inherits the Opendroid class, which is a class in our opendroid library.

7, start using Opendroid's crud function

Once configured, we can easily implement CRUD functionality in our business.

7.1. Insert data:

Student stu = new Student () stu.setstuname ("Bingbin"); Stu.setstuage (+); Stu.save ();
after a series of sets, you just need to call the Save method in student to save the data to the database, where you might have two questions: 1), where did the database be created? 2), where did the Save method come from? This method was not seen in the student.

Answer the above questions:

1), where is the database created? --Opendroid will automatically help us create a database and a table of the specified entities in the mapping when there is a database operation.

2), where did the Save method come from? --of course, inherited from the opendroid, the future blog in the implementation of the opendroid process, will talk about this method.

7.2. Update data

Opendroid provides a series of methods to simplify the update operation .

Student stu = new Student () stu.setstuname ("loader"); Stu.update ("_ID>?", "4");
The above code is to update the ID greater than 4 stuname to loader, here you may have questions: student We do not define the ID ah, here is how to use the ID it? So, when defining an entity class and creating an ID field, Opendroid automatically creates a _id field for us.

What updates do you have?

Student stu = new Student () stu.setstuname ("loader"); Stu.update (4);
the code above is to update the Stuname with ID 4 to loader

Of course, you can also use Contentvalues to update:

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 IDs greater than 8 to opendroid.

Well, the update will say so much, I believe, is enough to cope with the daily development work.

7.3. Delete data

Opendroid is also easy to delete, and is as intuitive as the code above.

int length = Opendroid.delete (Student.class, 1, 2, 3); System.out.println (length);
a line of code can be deleted, the above code is to delete the data ID 1 or 2 or 3.

Use conditional Delete

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

In the crud is a relatively troublesome point is the query operation, of course, opendroid the query operation is also encapsulated.

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

The code above is a query ID of 1 and maps the query results to the student class.

list<student> result = OpenDroid.query.find (Student.class); for (Student Res:result) {System.out.println ( Res.getstuname ());}
haha, the condition is omitted is to query all the data, of course, returned here the list collection.


list<student> result = OpenDroid.query.find (Student.class, 1, 5, ten); for (Student Res:result) {System.out.println (Res.getid () + ":" + res.getstuname ());}

Query the data with ID 1 or 5 or 10, and note that it is a method of calling a static variable in the Opendroid query.

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 the query statement in the where, order, limit, and so on,

The columns () method is to specify the field to query

The Where () method is the condition of the specified query

The order () method specifies how the query data is arranged

The limit () method specifies the limit of the SELECT statement

Finally, the Find method is called to query the data.


Unconsciously, we have opendroid the most important crud operations basically all mastered, I believe it is easy to understand the code here, below to see how to upgrade the database using Opendroid.

1. Modify the value of the Version field in the Open-droid.xml file.

2. Add or remove mapping, or adjust the field of an instance class based on business upgrade requirements

Only two steps above, the next time you use the database, Opendroid will automatically help us upgrade the database, and will update the current data to the new table, without worrying about the database upgrade process lost data, of course, we discard the field opendroid will be automatically ignored.


A blog time, we have mastered the use of opendroid, the vast majority of ORM framework usage is also very poor, so you also mastered the basic ORM of the syntax.

In the next few blogs, I'll step-by-step to explain how opendroid is implemented, and ultimately create an ORM framework that belongs to you!



Build Android ORM Framework Opendroid (i) Use of the--orm framework

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.