最近在对开发项目的性能进行优化。由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁的读写、查询等操作。因此首先想到了对整个项目的数据库框架进行优化。
原先使用android本身内置的sqllite,也就是用的最基本的SQLiteOpenHelper方法,这种方法对自己来说比较方便易懂。但是在使用过程中感觉很繁琐,从建表到对表的增删改查等操作,如果表对象的属性很多,就需要使用大量的代码来执行建表、插入等。在代码执行中还需要对数据库和游标的进行及时关闭(开启使用,用完关闭),而且还需要部分sql语言,这在开发中产生bug进行调试时尤其不方便。
目前android经常用的orm框架主要有greenDAO、OrmLite、AndrORM。 综合了网上的各种评价,greenDAO的运行效率最高,内存消耗最少,性能最佳。因此决定采用greenDAO框架,对项目的orm框架进行改进。
   
  1. package de.greenrobot.daogenerator.gentest;
  2. import de.greenrobot.daogenerator.DaoGenerator;
  3. import de.greenrobot.daogenerator.Entity;
  4. import de.greenrobot.daogenerator.Property;
  5. import de.greenrobot.daogenerator.Schema;
  6. import de.greenrobot.daogenerator.ToMany;
  7. /** * Generates entities and DAOs for the example project DaoExample. * * Run it as a Java application (not Android). * * @author Markus */
  8. public class ExampleDaoGenerator
  9. {        public static void main(String[] args) throws Exception        {        Schema schema = new Schema(3, "de.greenrobot.daoexample");        addNote(schema);        addCustomerOrder(schema);        new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");        }        private static void addNote(Schema schema)        {        Entity note = schema.addEntity("Note");        note.addIdProperty();        note.addStringProperty("text").notNull();        note.addStringProperty("comment");        note.addDateProperty("date");        }        private static void addCustomerOrder(Schema schema)        {        Entity customer = schema.addEntity("Customer");        customer.addIdProperty();        customer.addStringProperty("name").notNull();        Entity order = schema.addEntity("Order");        order.setTableName("ORDERS"); // "ORDER" is a reserved keyword        order.addIdProperty();        Property orderDate = order.addDateProperty("date").getProperty();        Property customerId = order.addLongProperty("customerId").notNull().getProperty();        order.addToOne(customer, customerId);        ToMany customerToOrders = customer.addToMany(order, customerId);        customerToOrders.setName("orders");        customerToOrders.orderAsc(orderDate);        }
  10. }
复制代码

相关文章:

  • 2021-06-02
  • 2021-08-19
  • 2021-07-07
  • 2022-02-05
  • 2021-06-01
  • 2021-04-16
  • 2021-04-04
猜你喜欢
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-07-05
相关资源
相似解决方案