快速开发之xUtils(三)DbUtils详细介绍

来源:互联网 发布:亚信数据全球总部 编辑:程序博客网 时间:2024/06/05 13:22

转载:http://www.apkbus.com/forum.php?mod=viewthread&tid=157644&highlight=xUtils

案例下载:http://download.csdn.net/detail/huningjun/8645595或者https://github.com/wyouflf/xUtils

DbUtils主要是在数据操作上进行封装,实现了直接对对象进行操作。而无须让用户自己再写sql语句,当然你也可以自己写。
      1.首先DbUtils要创建一个SQLiteDatabase对象。
  1. private synchronized static DbUtils getInstance(DaoConfig daoConfig) { 
  2.         DbUtils dao = daoMap.get(daoConfig.getDbName()); 
  3.         if (dao == null) { 
  4.             dao = new DbUtils(daoConfig); 
  5.             daoMap.put(daoConfig.getDbName(), dao); 
  6.         } else { 
  7.             dao.daoConfig = daoConfig; 
  8.         } 

  9.         // update the database if needed 
  10.         SQLiteDatabase database = dao.database; 
  11.         int oldVersion = database.getVersion(); 
  12.         int newVersion = daoConfig.getDbVersion(); 
  13.         if (oldVersion != newVersion) { 
  14.             if (oldVersion != 0) { 
  15.                 DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener(); 
  16.                 if (upgradeListener != null) { 
  17.                     upgradeListener.onUpgrade(dao, oldVersion, newVersion); 
  18.                 } else { 
  19.                     try { 
  20.                         dao.dropDb(); 
  21.                     } catch (DbException e) { 
  22.                         LogUtils.e(e.getMessage(), e); 
  23.                     } 
  24.                 } 
  25.             } 
  26.             database.setVersion(newVersion); 
  27.         } 

  28.         return dao; 
  29.     }
复制代码
                       
2.对象的保存 
  1.   private boolean saveBindingIdWithoutTransaction(Object entity) throws DbException { 
  2.         Class<?> entityType = entity.getClass(); 
  3.         //根据对象和注解获取到表名 
  4.         String tableName = TableUtils.getTableName(entityType); 
  5.         Id idColumn = TableUtils.getId(entityType); 
  6.         //id是否自增长 
  7.         if (idColumn.isAutoIncrement()) { 
  8.                 //根据对象的属性值获取到KeyValueList 
  9.             List<KeyValue> entityKvList = SqlInfoBuilder.entity2KeyValueList(this, entity); 
  10.             if (entityKvList != null && entityKvList.size() > 0) { 
  11.                 //设置ContentValues 
  12.                 ContentValues cv = new ContentValues(); 
  13.                 DbUtils.fillContentValues(cv, entityKvList); 
  14.                 //插入数据库 
  15.                 long id = database.insert(tableName, null, cv); 
  16.                 if (id == -1) { 
  17.                     return false; 
  18.                 } 
  19.                 idColumn.setAutoIncrementId(entity, id); 
  20.                 return true; 
  21.             } 
  22.         } else { 
  23.              //创建sql语句,运行sql语句 
  24.             execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity)); 
  25.             return true; 
  26.         } 
  27.         return false; 
  28.     }
复制代码


                               
3.对象查询
  1. public <T> T findFirst(Object entity) throws DbException { 
  2.         if (!tableIsExist(entity.getClass())) return null; 
  3.         //根据类类型构造Selector 
  4.         Selector selector = Selector.from(entity.getClass()); 
  5.         然后根据属性值得到KeyValueList 
  6.         List<KeyValue> entityKvList = SqlInfoBuilder.entity2KeyValueList(this, entity); 
  7.         if (entityKvList != null) { 
  8.             //然后根据KeyValueList,得到WhereBuilder,相当于sql里的where子句部分 
  9.             WhereBuilder wb = WhereBuilder.b(); 
  10.             for (KeyValue keyValue : entityKvList) { 
  11.                 Object value = keyValue.getValue(); 
  12.                 if (value != null) { 
  13.                     wb.and(keyValue.getKey(), "=", value); 
  14.                 } 
  15.             } 
  16.             selector.where(wb); 
  17.         } 
  18.         //根据Selector加载对象 
  19.         return findFirst(selector); 
  20.     } 
复制代码


                               
对象的属性值映射成数据库的字段,或从数据库的字段得到对象的属性时其中一个重要的接口就是

  1. public interface ColumnConverter<T> {

  2. //得到数据库字段值

  3. T getFiledValue(final Cursor cursor, int index);

  4. //得到数据库字段值

  5. T getFiledValue(String fieldStringValue);

  6. //根据对象的属性值,得到数据库字段值

  7. Object fieldValue2ColumnValue(T fieldValue);

  8. //得到字段的数据类型

  9. String getColumnDbType();

  10. }
复制代码


        然后各种属性都有相应的实现。如下图:

从名字已经相当清楚每种属性是怎么转换的了。所以不一个一个的介绍了。
参考:http://blog.csdn.net/jjwwmlp456/article/details/44084411

0 0