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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android xUtils3 是一个非常方便的 Android 开发工具包,其中包含了许多方便实用的功能,例如网络请求、图片加载等。下面是使用 xUtils3 获取网络图片的示例代码: 1. 在 build.gradle 中添加 xUtils3 的依赖: ```groovy dependencies { implementation 'org.xutils:xutils:3.5.0' } ``` 2. 在 AndroidManifest.xml 中添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 3. 在代码中使用 xUtils3 获取网络图片: ```java ImageView imageView = findViewById(R.id.image_view); String imageUrl = "http://example.com/image.jpg"; x.image().bind(imageView, imageUrl, new Callback.CommonCallback<Drawable>() { @Override public void onSuccess(Drawable result) { // 图片加载成功 } @Override public void onError(Throwable ex, boolean isOnCallback) { // 图片加载失败 } @Override public void onCancelled(CancelledException cex) { // 图片加载被取消 } @Override public void onFinished() { // 图片加载完成 } }); ``` 上面的代码中,我们通过 x.image().bind() 方法来获取网络图片。第一个参数是 ImageView 对象,第二个参数是图片的 URL,第三个参数是一个 Callback,用于监听图片加载的状态。 其中,onSuccess() 方法表示图片加载成功,onError() 方法表示图片加载失败,onCancelled() 方法表示图片加载被取消,onFinished() 方法表示图片加载完成。在实际使用中,我们通常只需要实现 onSuccess() 方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值