Ormlite-笔记

Ormlite数据库

1. Ormlite是什么?

  • Android原生操作数据库的方式是这样的:
    • 首先定义一个类继承SQLiteOpenHelper,重写onCreate和onUpdate
    • 后续对数据库的增删改查以及创建和销毁都需要操作相对比较底层的sql语句,难以记忆又容易出错
    • 而且操作sql语句代码量多,sql语句的逻辑比较繁琐
  • 而Java是面向对象的语言,有没有一种方式可以让我们不去关心sql语句的编写,直接面向对象;我们操作对象就间接操作了数据库。那么orm数据库框架就是帮助我们完成这个事,并且能够将javabean和数据库的表映射起来,javabean的字段就是表的字段。这就是为什么叫o(对象)r(关系)m(映射)的原因。
  • Ormlite就是一个orm数据库框架之一。

2. 为什么选择Ormlite?

Andorid平台的orm数据库框架有很多:比如greenDao,DBFlow,Relm等,甚至xutil,afinal;每个orm框架各有优缺点,而Ormlite使用起来非常容易上手,效率也还不错,所以我们选择它作为学习的对象。试想想,如果一个类库光配置就需要搞一天,你还会有心情用它么?

3. Ormlite如何使用?

  • 准备工作:引入相关jar包
  • 首先,定义类继承OrmLiteSqliteOpenHelper:

    public class DBHelper extends OrmLiteSqliteOpenHelper {
        public static final String TNAME = "test.db";
        private static DBHelper mInstance = null;
        private DBHelper(Context context) {
            super(context, TNAME, null, 1);
        }
        public static DBHelper with(Context context){
            if (mInstance==null){
                synchronized (DBHelper.class){
                    if(mInstance==null){
                        mInstance = new DBHelper(context);
                    }
                }
            }
            return mInstance;
        }
        @Override
        public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
            try {
                //创建Stu表,根据Stu类上相关注解
                TableUtils.createTable(connectionSource,Stu.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
            try {
                TableUtils.dropTable(connectionSource,Stu.class,true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 其次,编写javabean,通过注解配置和表的映射关系:、

    @DatabaseTable(tableName = "t_stu")
    public class Stu {
        @DatabaseField(generatedId = true)//自增长id
        public int id;
    
        @DatabaseField
        public String name;
    
        @DatabaseField
        public int age;
    
        public Stu(){}
        public Stu(int age, String name) {
            this.age = age;
            this.name = name;
        }
    }
    
  • 最后,调用dao类的对应方法,直接操作对象,即可完成增删改查:

    Dao<Stu, Integer> dao = DBHelper.with(this).getDao(Stu.class);
    
    private void delete(Dao<Stu, Integer> dao) throws SQLException {
        dao.deleteById(1);
    }
    
    private void update(Dao<Stu, Integer> dao) throws SQLException {
        Stu stu = dao.queryForId(1);
        stu.name = "黎明";
        stu.age = 66;
        dao.update(stu);
    }
    private void query(Dao<Stu,Integer> dao) throws SQLException {
        List<Stu> stus = dao.queryForAll();
        Log.d("tag","查询到"+stus.size()+"条数据!");
        for (int i = 0; i < stus.size(); i++) {
            Stu stu = stus.get(i);
            Toast.makeText(this, stu.toString(), Toast.LENGTH_SHORT).show();
        }
    }
    private void save(Dao<Stu,Integer> dao) throws SQLException {
        Stu stu = new Stu(22, "刘德华");
        dao.create(stu);
        Log.d("tag","保存成功!");
    }
    

猜你喜欢

转载自blog.csdn.net/a94721990/article/details/81051267