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

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

增:

<span style="font-size:14px;">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();
    }
}</span>
下面这两个函数和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();
    }
}

删:

<span style="font-size:14px;">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);
}</span>
更新:
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();
    }
}
替换:(替换和更新类似,但是替换会先删除原表匹配的数据项,然后增加新的数据项)

<span style="font-size:14px;">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();
    }
}</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值