xUtils系列之DbUtils-增,删,更新,替换操作

DbUtils实现了很方便的数据操作,基本一行代码就能搞定,所以实在是没啥写的,姑且贴下相关代码,方便之后查看.

增:

public void save(Object entity) throws DbException {
    try {
        beginTransaction();

        createTableIfNotExist(entity.getClass());
        execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void saveAll(List entities) throws DbException {
    if (entities == null || entities.size() == 0) return;
    try {
        beginTransaction();

        createTableIfNotExist(entities.get(0).getClass());
        for (Object entity : entities) {
            execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity));
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}
下面这两个函数和save类似,不同的是,如果id是自增,DbUtils会自动给对象的id字段赋值.

public boolean saveBindingId(Object entity) throws DbException {
    boolean result = false;
    try {
        beginTransaction();

        createTableIfNotExist(entity.getClass());
        result = saveBindingIdWithoutTransaction(entity);

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
    return result;
}

public void saveBindingIdAll(List entities) throws DbException {
    if (entities == null || entities.size() == 0) return;
    try {
        beginTransaction();

        createTableIfNotExist(entities.get(0).getClass());
        for (Object entity : entities) {
            if (!saveBindingIdWithoutTransaction(entity)) {
                throw new DbException("saveBindingId error, transaction will not commit!");
            }
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}
当不确定是保存还是更新时,可以用下面两个函数:

public void saveOrUpdate(Object entity) throws DbException {
    try {
        beginTransaction();

        createTableIfNotExist(entity.getClass());
        saveOrUpdateWithoutTransaction(entity);

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void saveOrUpdateAll(List entities) throws DbException {
    if (entities == null || entities.size() == 0) return;
    try {
        beginTransaction();

        createTableIfNotExist(entities.get(0).getClass());
        for (Object entity : entities) {
            saveOrUpdateWithoutTransaction(entity);
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

删:

public void deleteById(Class entityType, Object idValue) throws DbException {
    if (!tableIsExist(entityType)) return;
    try {
        beginTransaction();

        execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entityType, idValue));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void delete(Object entity) throws DbException {
    if (!tableIsExist(entity.getClass())) return;
    try {
        beginTransaction();

        execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entity));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void delete(Class entityType, WhereBuilder whereBuilder) throws DbException {
    if (!tableIsExist(entityType)) return;
    try {
        beginTransaction();

        execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entityType, whereBuilder));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void deleteAll(List entities) throws DbException {
    if (entities == null || entities.size() == 0 || !tableIsExist(entities.get(0).getClass())) return;
    try {
        beginTransaction();

        for (Object entity : entities) {
            execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entity));
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void deleteAll(Class entityType) throws DbException {
    delete(entityType, null);
}
更新:
public void update(Object entity, String... updateColumnNames) throws DbException {
    if (!tableIsExist(entity.getClass())) return;
    try {
        beginTransaction();

        execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, updateColumnNames));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void update(Object entity, WhereBuilder whereBuilder, String... updateColumnNames) throws DbException {
    if (!tableIsExist(entity.getClass())) return;
    try {
        beginTransaction();

        execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, whereBuilder, updateColumnNames));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void updateAll(List entities, String... updateColumnNames) throws DbException {
    if (entities == null || entities.size() == 0 || !tableIsExist(entities.get(0).getClass())) return;
    try {
        beginTransaction();

        for (Object entity : entities) {
            execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, updateColumnNames));
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void updateAll(List entities, WhereBuilder whereBuilder, String... updateColumnNames) throws DbException {
    if (entities == null || entities.size() == 0 || !tableIsExist(entities.get(0).getClass())) return;
    try {
        beginTransaction();

        for (Object entity : entities) {
            execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, whereBuilder, updateColumnNames));
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}
替换:(替换和更新类似,但是替换会先删除原表匹配的数据项,然后增加新的数据项)

public void replace(Object entity) throws DbException {
    try {
        beginTransaction();

        createTableIfNotExist(entity.getClass());
        execNonQuery(SqlInfoBuilder.buildReplaceSqlInfo(this, entity));

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

public void replaceAll(List entities) throws DbException {
    if (entities == null || entities.size() == 0) return;
    try {
        beginTransaction();

        createTableIfNotExist(entities.get(0).getClass());
        for (Object entity : entities) {
            execNonQuery(SqlInfoBuilder.buildReplaceSqlInfo(this, entity));
        }

        setTransactionSuccessful();
    } finally {
        endTransaction();
    }
}

你可能感兴趣的:(xUtils)