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
    评论
xUtils包含了orm,http(s),image,view注解,但依然很轻量级(251K),并且特性强大,方便扩展。 xUtils特点: 1、orm:高效稳定的orm工具,使得http接口实现时更方便的支持cookie和缓存。 灵活的,类似linq表达式的接口。 和greenDao一致的性能。 2、http(s):基于UrlConnection,Android4。4以后底层为okHttp实现。 请求协议支持11种谓词:GET,POST,PUT,PATCH,HEAD,MOVE,COPY,DELETE,OPTIONS,TRACE,CONNECT 支持超大文件(超过2G)上传 支持断点下载(如果服务端支持Range参数,客户端自动处理断点下载) 支持cookie(实现了domain,path,expiry等特性) 支持缓存(实现了Cache-Control,Last-Modified,ETag等特性,缓存内容过多时使用过期时间+LRU双重机制清理) 支持异步和同步(可结合RxJava使用)调用 3、image:有了http(s)及其下载缓存的支持,image模块的实现相当的简洁。 支持内存缓存,磁盘缓存(缩略图和原图),并且支持回收被view持有,但被MemCache移除的图片,减少页面回退时的闪烁。 支持在ListView滑动时,自动停止被回收复用的item对应的下载任务(再次下载时断点续传) 支持webp,gif(部分比较老的系统只展示静态图) 支持圆角,圆形,方形等裁剪,支持自动旋转... 4、view注解:view注解模块仅仅400多行代码却灵活的支持了各种View注入和事件绑定。 事件注解支持且不受混淆影响...(参考混淆配置) 支持绑定拥有多个方法的listener
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、付费专栏及课程。

余额充值