android OrmLite 入门

来源:互联网 发布:语音信箱软件 编辑:程序博客网 时间:2024/05/02 17:11

android OrmLite 入门

android ormlite


大家在开发中经常会用到数据库,但是数据库的操作其实挺繁琐的,为了方便我们的使用,所以我们来使用当下流行的orm框架进行数据库的操作,下面来简单的介绍一下怎么使用OrmLite框架.

1.下载jar包

可以到官网去下载ormlite的jar包 下载地址 
我这里使用的是ormlite-core-5.0.jar 和 ormlite-android-5.0.jar 
将下载的jar包放到我们项目的libs文件夹下

2.创建我们的实体类

每一个实体类对应一张表,在我们项目中的bean目录下创建一个Student类

@DatabaseTable(tableName = Student.TABLE_NAME)public class Student {    public static final String TABLE_NAME = "t_student";    @DatabaseField(generatedId = true)    private int id;    @DatabaseField(columnName = "name")    private String name;    //空的构造方法一定要有,否则数据库会创建失败    public Student() {    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Student{" +                "id=" + id +                ", name='" + name + '\'' +                '}';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

类中使用了 @DatabaseTable@DatabaseField 两个注解来分别确定了表名称和表字段

注解说明

1.@DatabaseTable

DatabaseTable是一个类注解,一般用在实体类的上面,被添加该注解的类会创建一张表,表名称默认为类名称的小写,我们也可以通过 tableName 来指定表名称

2.@DatabaseField

DatabaseField是一个属性注解,使用在被添加DatabaseTable注解的类中的属性字段上,被添加该注解的类字段,在表中也会有一个相对应的表字段 
常用参数:

generatedId = treu 
主键,自动生成id 该注解下的字段必须是整形(int ,long)

id = true 
主键

unique = true 
唯一约束 默认false

columnName = “name” 
表字段名,默认为变量名称

canBeNull = false 
非空约束,默认为true,可以为null,设为false就是不能为null

foreign = true 
外键引用,字段不能是一个原始类型,应该定义一个对象当做外键引用,在外键对象的类中,必须要有一个ID字段(ID, generatedId,generatedIdSequence)

foreignAutoRefresh = true 
在使用外键引用时,由于ormlite的外键引用使用的是对象,所以添加这个字段的话在查询时,会吧外键的对象数据都查询回来,否者外键数据就只有那个对象的主键有值,其余的值都是null

defaultValue = “老王” 
默认值

index = true 
建立索引 默认为false

uniqueIndex = true 
唯一索引 默认为false


3.编写我们的数据操作类 helper类

这里我们进行数据库和表的创建

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {    private static final String DB_NAME = "db_myorm";    private static final int DB_VERSION = 1;    private static DatabaseHelper instance;    private DatabaseHelper(Context context) {        super(context, DB_NAME, null, DB_VERSION);    }    public static DatabaseHelper getInstance(Context context){        if(instance == null){            synchronized (DatabaseHelper.class){                if(instance == null){                    instance = new DatabaseHelper(context.getApplicationContext());                }            }        }        return instance;    }    @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);            onCreate(sqLiteDatabase,connectionSource);        } catch (SQLException e) {            e.printStackTrace();        }    }    public Dao getDao(Class clazz) throws SQLException {        return super.getDao(clazz);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

我们的Helper类要继承自OrmLiteSqliteOpenHelper,需要重写onCreate和onUpgrage方法 
在onCreate里只需要一句话就可以创建我们的表。 
当数据库版本发生变化时会走onUpgrade,我们这里暂时先删除原来的表,在重新创建

4.编写我们的Dao类进行crud操作

其实crud操作OrmLite已经帮我们做好了,我们这里在封装一层, 
我们的dao类是从helper类中获取的,我们来看一下代码:

public class StudentDao {    //两个泛型约束 一个是对应的实体类类型,一个是主键类型    private Dao<Student,Integer> dao;    public StudentDao(Context context) {        try {            dao = DatabaseHelper.getInstance(context).getDao(Student.class);        } catch (SQLException e) {            e.printStackTrace();        }    }    public int add(Student student){        try {            return dao.create(student);        } catch (SQLException e) {            e.printStackTrace();        }        return -1;    }    public Student query(int id){        try {            return dao.queryForId(id);        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }    public int updata(Student student){        try {            return dao.update(student);        } catch (SQLException e) {            e.printStackTrace();        }        return -1;    }    public int delete(int id){        try {            return dao.deleteById(id);        } catch (SQLException e) {            e.printStackTrace();        }        return -1;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

5.总结

到这里基本上是已经完成了我们的OrmLite的使用。后面我还会在写一篇关于ormLite的实际应用,包括外键的引用,数据库升级时,onUpgrade方法中正确的做法,而不是简单的删表了事。

原创粉丝点击