OrmLite 的基础使用

OrmLite 基础知识  

一、OrmLite 框架介绍:  

  Ormlite 框架是第三方对数据库操作的封装的一个框架,为了提高开发效率,尤其是对某些  

数据库操作特别频繁的 app议使用 OrmLite 框架是一个非常轻量级的数据库操作框架,  

它的底层是根据反射机制来实现的。  

二、OrmLite 框架使用步骤:  

  1. 在 http://ormlite.com/releases/ 官网下载 core 包 和 Android包  

  2. 创建相应的 javaBean,使用注解的方式别在属性上添加@DatabaseField(columnName  

namecolumnName 的值为该字段在数据中的列名,  

@DatabaseField(generatedId = true) generatedId 表示 id 为主键且自动生成。  

  3. 编写该 Bean 的数据库层的封装,通常为 xxDao,并在类中完成对 Bean 的相关操作的封装。  

  4. 编写数据库的帮助类,让其继承 OrmLiteSqliteOpenHelper,在该类中完成数据库和表的创  

建,并创建获取所有 Bean 的 Dao 层的方法,并使用单例方式实现数据库帮助对象的初始  

化,最后释放资源。  

  5. 重写以下两个回调方法:  

  1) onCreate(SQLiteDatabase database,ConnectionSourceconnectionSource)创建表,  

我们直接使用 ormlite 提供的 TableUtils.createTable(connectionSource, User.class);  

进行创建。  

  2) onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource,  

intoldVersion, int newVersion),更新表,使用 ormlite 提供的  

TableUtils.dropTable(connectionSource, User.class, true);进行删除操作,删除完成  

后,别忘了,创建操作:onCreate(database, connectionSource);  

三 :下载包的导入

  1.在project试图下找到lib文件夹,将下载的包复制进去

  2.选中两个包,右键->点击 as a library   ok搞定

demo演示;

创建两个实体类:Student School

package com.example.ormalite;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "stu_info") //设置表名,默认为类名
public class Student {
    @DatabaseField(generatedId = true) //
    private  String id;
    @DatabaseField
    private  String name;
    @DatabaseField
    private  int age;
    @DatabaseField
    private  String phone;
    /**
     * foreign = true 是否来自外部
     * foreignAutoRefresh = true  是否自动更新
     * 一个学生对应一个学校
     */
    @DatabaseField(columnName = "school_id",canBeNull = false,foreign = true,foreignAutoRefresh = true)
    private  School school;

    public Student(String name, int age, String phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
package com.example.ormalite;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

import java.util.Collection;
@DatabaseTable(tableName = "school_info")
public class School {
    @DatabaseField
    private String name;
    @DatabaseField
    private String address;
    //一个学校有多名学生
    @ForeignCollectionField
    private Collection<Student> students;
}

创建数据库帮助类,对数据库进行创建以及基本的增删改查方法

package com.example.ormalite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class SqlHelper extends OrmLiteSqliteOpenHelper {

    private SqlHelper(Context context) {
        super(context, "orm_stu", null, 1);
    }
    private static SqlHelper sqlHelper = null;

    public static synchronized SqlHelper getInstace(Context context){
        if (sqlHelper!=null){
            sqlHelper = new SqlHelper(context);
        }
        return sqlHelper;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            //建表
            TableUtils.createTable(connectionSource,Student.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource,Student.class,true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/conglingkaishi/p/9480227.html