Android ORM and androidorm

Source: Internet
Author: User

Android ORM and androidorm

ORM, that is, Object Relation Mapping, Object relationship ing, implements the correspondence between classes in the program and data in the database. operations on the database can be achieved through class operations, you do not need to write SQL statements to improve development efficiency and reduce development time.

In Java Web development, there are many ORM frameworks, such as Hibernate. In Android development, there are also some ORM frameworks that implement operations on the SQLite database. For example, Litepal, GreenDAO, or OrmLite. This article focuses on Litepal.

1. ing files and object classes

Download the Litpal Jar file and copy it to the libs folder. Create a new litepal. xml file under the assets Folder:

<?xml version="1.0" encoding="utf-8"?><litepal>    <dbname value="easydb" />    <version value="1" />    <list>        <mapping class="com.example.hzhi.fragmentdemo.Myclass" />    </list></litepal>

Here, Myclass is an entity class:

package com.example.hzhi.fragmentdemo;import org.litepal.crud.DataSupport;/** * Created by Administrator on 2016/8/27. */public class Myclass extends DataSupport {    private Integer Id;    private String Name;    private String Teacher;    private Integer Studytime;    private String Picture;    public Myclass() {    }    public Myclass(Integer id, String name, Integer studytime, String teacher, String picture) {        Id = id;        Name = name;        Studytime = studytime;        Teacher = teacher;        Picture = picture;    }    public Integer getId() {        return Id;    }    public void setId(Integer id) {        Id = id;    }    public String getName() {        return Name;    }    public void setName(String name) {        Name = name;    }    public String getTeacher() {        return Teacher;    }    public void setTeacher(String teacher) {        Teacher = teacher;    }    public Integer getStudytime() {        return Studytime;    }    public void setStudytime(Integer studytime) {        Studytime = studytime;    }    public String getPicture() {        return Picture;    }    public void setPicture(String picture) {        Picture = picture;    }}

In this way, the ing between the database table and the object class is set. When a database operation is performed, if the database does not have this table, a new table is created, the table name is the same as the object class name. The table field is the attribute of the object class.

Add the following configuration to the application node in the AndroidManifest. xml file:

android:name="org.litepal.LitePalApplication"
2. Insert data

The code is simple:

Mc = new Myclass (); mc. setId (0); mc. setName ("Computer Network"); mc. setTeacher ("Zhang San"); mc. setStudytime (120); mc. save ();

Is it almost the same as the data inserted by Hibernate? Saving the trouble of writing SQL statements!

3. query data
public static List<Myclass> listClass;listClass = DataSupport.findAll(Myclass.class);

The preceding statement queries all records. There are other methods:

(1) DataSupport. findAll (Myclass. class, 1, 3, 5), query data with IDs of 1, 3, and 5;

(2) DataSupport. select (). where (). order (). find (Myclass. class), specifying the fields to be queried and query conditions;

(3) Cursor cursor = DataSupport. findBySQL (), which is queried by SQL statement;

(4) DataSupport. findFirst (), query the first record;

(5) DataSupport. findLast (): query the last record.

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.