Ultra-detailed Greendao configuration and use, with Android Studio basic use Method

Source: Internet
Author: User

This article is the beginning of an ORM framework for an Android database, and my goal is to understand the features of each ORM framework and choose the best method for different projects. About the advantages and disadvantages of Greendao in the SQLite database framework Ormlite and Greendao Simple comparison This article has done a very good introduction, I will not repeat the introduction.
Disclaimer: The Android Studio part of this article is translated from Tutorial-greendao from Scratch-part 1, this article is not only a very good introduction to the use of Greendao, but also for Android Studio can also be very helpful in terms of use.

1. Model of database tables

Person table

Person
Id
Name
Comment

Lease table

Lease
Item
Comment
Leasedate
Returndate
2.Android Studio 2.1 New Project

Open our Android Studio if you already have a project already open, close the current project in file–> "Close Project". On the Right small window select "Start a new Android Studio project"

Next, refine other information

Next Choose "Target Android Devices", because this tutorial is only to introduce the use of Greendao, so directly selected API21

As wants to create a default activity for us, we don't need to, so select "Add no Activity"

After the project was created, it looked bare and we couldn't even see our project structure, but we clicked on "project"

The view tree we see is not the view tree for our project file structure, so we chose "android-> project"

2.2 Download the jar package

The previous preparation has been completed, then download the jar package, we need 3 jar packages altogether
1.greendao.jar
2.greendao-generator.jar
3.freemarker.jar
Select this when downloading Freemarker.jar

2.3 Adjusting the project structure

Greendao can help us generate a data model directly from a generator, which is a Java project that separates him from our project, so we need to create a new module. Right-click "Leasegreendao" and select "New->module"

Select "Java Library"

Improve other information

2.4 Importing JAR Packages

Now we copy and paste Greendao-generator.jar and Freemarker.jar into the Libs folder of the newly created leasedaogenerator.

First, we have to let gradle know our libs, so we go into the "Build.gradle" under the "dependencies" to insert the following code:

files(‘libs/greendao-generator-1.3.1.jar‘files(‘libs/freemarker-2.3.22.jar‘)

Note that the name and version information of the jar package you downloaded is filled in
As mentioned earlier, this leasedaogenerator is an independent project, so we need to add the following code in the first part:

‘application‘

We need to let Gradle know the class name to start.

mainClassName = "com.devteam83.tutorials.leasegreendao.LeaseDaoGenerator"

The complete code for Leasedaogenerator's Build.gradle is as follows:

‘application‘‘java‘"com.devteam83.tutorials.leasegreendao.LeaseDaoGenerator"dependencies {    ‘libs‘include: [‘*.jar‘])    files(‘libs/greendao-generator-1.3.1.jar‘)    files(‘libs/freemarker-2.3‘2.jar``````)}

Don't forget to click on the Little yellow button "Sync now".

2.5 Creating a data model

Now that's interesting, let's start creating models! Now open "Leasedaogenerator" and add the following code:

publicstaticvoidmainthrows Exception {        new Schema(1"com.devteam83.tutorials.leasegreendao.model");    }

We created a Schema, the first parameter is the version number of the database, and the second parameter is the package name of the data model that we want to generate.

Next, we start creating the entity

Entity person = Schema. Addentity("Person");Person. Addidproperty();Person. Addstringproperty("Name");Person. Addstringproperty("comment");Entity Lease = Schema. Addentity("Lease");Lease. Addidproperty();Lease. Addstringproperty("Item");Lease. Addstringproperty("comment");Lease. Addlongproperty("Leasedate");Lease. Addlongproperty("Returndate");

I think it's pretty straightforward, but it creates two separate entities that don't have any relationship between them, and that's not enough, we want a lease to belong to a person, so we're going to add the following code:

Property personId = lease.addLongProperty("personId").getProperty();lease.addToOne(person, personId);

We have saved a PersonID this foreign key and implemented a 1:1 relationship by calling the Addtoone () method
On the other hand, we want a person to have multiple lease, so we'll add the following code:

ToMany personToLease = person.addToMany(lease, personId);personToLease.setName("leases");

The relationship of 1:n is realized by calling the Addtomany () method. The SetName () method is to give the relationship a name, the default is to use the name of the target entity, that is, lease, but sometimes using the default name will cause conflict, so we can customize the name, that is, leases.

The last sentence, we built the model generated class

new"../app/src/main/java");

The first parameter is "Schema" and the second parameter is the output of the generated data model to the SRC folder under the specified project.

The complete code is as follows:

public class Leasedaogenerator {public static void main (String args[]) throws Exception {Schema schema = new S Chema (1,"Com.devteam83.tutorials.leasegreendao.model");Entity person = Schema. Addentity("Person");Person. Addidproperty();Person. Addstringproperty("Name");Person. Addstringproperty("comment");Entity Lease = Schema. Addentity("Lease");Lease. Addidproperty();Lease. Addstringproperty("Item");Lease. Addstringproperty("comment");Lease. Addlongproperty("Leasedate");Lease. Addlongproperty("Returndate");Property PersonId = Lease. Addlongproperty("PersonId"). GetProperty();Lease. Addtoone(Person, PersonId);Tomany persontolease = person. Addtomany(Lease, PersonId);Persontolease. SetName("Leases");New Daogenerator (). Generateall(Schema,".. /app/src/main/java ");}}
2.6 Generating Classes

The next steps are few and we just need to click Gradle and run Leasedaogenerator
If there is no error, you will see the following information

2in185time4.1secs23:11:50‘run‘.

In our app directory, we can see the automatically generated classes

3. Eclipse article

With the basics of Android studio, Eclipse is easy, and I don't have to explain too much.

Create a new Java project named Daoexamplegenerator

Right-click Project->build path->configure Build Path in libraries Select Add External JARs ... Add Greendao-generator.jar and Freemarker.jar in, click OK

Next, add the following code in the Exampledaogenerator.java

 Public  class exampledaogenerator {     Public Staticvoid Main (string[] args) throwsException{Schema schema =NewSchema (1,"Com.example.daoexample.model"); Entity person = schema.addentity ("Person");        Person.addidproperty (); Person.addstringproperty ("Name"); Person.addstringproperty ("comment"); Entity lease = schema.addentity ("Lease");        Lease.addidproperty (); Lease.addstringproperty ("Item"); Lease.addstringproperty ("comment"); Lease.addlongproperty ("Leasedate"); Lease.addlongproperty ("Returndate"); Property PersonId = Lease.addlongproperty ("PersonId"). GetProperty ();        Lease.addtoone (person, personId);        Tomany persontolease = Person.addtomany (lease, personId); Persontolease.setname ("Leases");NewDaogenerator (). Generateall (Schema,".. /daoexample/src "); }}

Create a new Daoexample Android project and add Greendao.jar to the

After running Daoexamplegenerator, we can see the automatically generated classes in Daoexample

Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/nugongahou110

Ultra-detailed Greendao configuration and use, with Android Studio basic use Method

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.