Android ORM--litepal (1)

Source: Internet
Author: User

ORM, which is Object Relation Mapping, objects relational mapping, realizes the correspondence between the class inside the program and the data inside the database, and the operation of the database can be realized by the operation of the class, without writing the SQL statement, thus improving the development efficiency and saving the development time.

In Java Web Development, there are many ORM frameworks, such as Hibernate. In Android development, there are also some ORM frameworks that enable the operation of the SQLite database. such as Litepal, Greendao, Ormlite and so on. This article focuses on Litepal.

1. mapping files and entity classes

Download the Litpal jar file and copy it to the Libs folder below. Create a new Litepal.xml file under the Assets folder:

<?XML version= "1.0" encoding= "Utf-8"?><Litepal>    <dbnamevalue= "Easydb" />    <versionvalue= "1" />    <List>        <Mappingclass= "Com.example.hzhi.fragmentdemo.Myclass" />    </List></Litepal>

Where MyClass is the entity class:

 PackageCom.example.hzhi.fragmentdemo;ImportOrg.litepal.crud.DataSupport;/*** Created by Administrator on 2016/8/27.*/ Public classMyclassextendsDatasupport {PrivateInteger Id; PrivateString Name; PrivateString Teacher; PrivateInteger Studytime; PrivateString picture;  PublicMyclass () {} PublicMyclass (integer ID, string name, Integer studytime, string teacher, string picture) {ID=ID; Name=name; Studytime=Studytime; Teacher=teacher; picture=Picture ; }     PublicInteger getId () {returnId; }     Public voidsetId (Integer ID) {ID=ID; }     PublicString GetName () {returnName; }     Public voidsetName (String name) {Name=name; }     PublicString Getteacher () {returnTeacher; }     Public voidSetteacher (String teacher) {teacher=teacher; }     PublicInteger Getstudytime () {returnStudytime; }     Public voidsetstudytime (Integer studytime) {studytime=Studytime; }     PublicString getpicture () {returnPicture ; }     Public voidsetpicture (String picture) { picture=Picture ; }}

This sets the database's table and entity class correspondence, when the database operation, if the database does not have this table, will create a new table, the name of the table and the entity class name is the same, the table field is the entity class properties.

2. Inserting Data

The code is simple:

        New Myclass ();        Mc.setid (0);        Mc.setname ("Computer network");        Mc.setteacher ("Zhang San");        Mc.setstudytime (+);        Mc.save ();

Is it almost identical to hibernate's insert data? Eliminates the hassle of writing SQL statements!

3. Querying Data
 Public Static list<myclass>= Datasupport.findall (Myclass.  Class);

The above statement is to query out all records. There are a few other ways:

(1) Datasupport.findall (Myclass. Class, 1, 3, 5), indicating query 1th, 3, 5 data;

(2) Datasupport.select (). where (). order (). Find (Myclass. Class), indicating the field to query and the query criteria;

(3) cursor cursor = DATASUPPORT.FINDBYSQL (), query by SQL statement.

Android ORM--litepal (1)

Related Article

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.