Android 数据库框架ormlite 使用精要

package com.devilwwj.ormlite.model;

import java.io.Serializable;

import com.j256.ormlite.dao.ForeignCollection;

import com.j256.ormlite.field.DatabaseField;

import com.j256.ormlite.field.ForeignCollectionField;

import com.j256.ormlite.table.DatabaseTable;

/**

  • 摄影主题

  • @author wwj_748

*/

@DatabaseTable

public class Theme implements Serializable{

@DatabaseField(id = true)

public String id;

@DatabaseField

public String tid;

@DatabaseField

public String photographerId;

@DatabaseField

public String packageId; // 隶属套餐

@DatabaseField

public String status; // 后台审核状态

@DatabaseField

public String title; // 标题

@DatabaseField

public String coverId; // 封面Id

@DatabaseField

public String coverUrl; // 封面img

@DatabaseField

public String detail; // 详情

@DatabaseField

public int photoCount; // 图片个数

@DatabaseField

public String photos; //图集

@DatabaseField

public String createTime; // 上传时间

@DatabaseField

public String recordTime; // 拍摄时间

@DatabaseField

public double cost; // 花费

@DatabaseField

public String tags; // 标签

@DatabaseField

public String address;// 地址

@DatabaseField

public String loacationCode; // 位置代码

@DatabaseField

public int popularCount; // 热度

@DatabaseField(defaultValue = “0”)

public int favStatus; // 收藏状态

// 外部对象字段

@DatabaseField(foreign = true, foreignAutoRefresh = true)

public PackageInfo mPackage;

@DatabaseField(foreign = true, foreignAutoRefresh = true)

public Photographer photographer;

/**

  • 这里需要注意的是:属性类型只能是ForeignCollection或者Collection

  • 如果需要懒加载(延迟加载)可以在@ForeignCollectionField加上参数eager=false

  • 这个属性也就说明一个部门对应着多个用户

*/

@ForeignCollectionField(eager = true)

public ForeignCollection imgs;

}

我们这里不关注其他字段,关注它的外键字段,前面我们说到,一个套餐对应多个主题,所以我们在主题这个实体类中也需要定义一个关联套餐的字段。

// 外部对象字段

@DatabaseField(foreign = true, foreignAutoRefresh = true)

public PackageInfo mPackage;

注:要实现一对多关系,一定要这样定义,不然会出错。

定义Bean的DAO,对数据库进行增、删、改、查

这里笔者举个例子,大家以后开发根据这样来添加相应的业务逻辑方法:

package com.devilwwj.ormlite.dao;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import android.content.Context;

import com.devilwwj.ormlite.db.DBHelper;

import com.devilwwj.ormlite.model.Theme;

import com.j256.ormlite.dao.Dao;

/**

  • 定义数据访问对象,对指定的表进行增删改查操作

  • @author devilwwj

*/

public class ThemeDao {

private Dao<Theme, Integer> themeDao;

private DBHelper dbHelper;

/**

  • 构造方法

  • 获得数据库帮助类实例,通过传入Class对象得到相应的Dao

  • @param context

*/

public ThemeDao(Context context) {

try {

dbHelper = DBHelper.getHelper(context);

themeDao = dbHelper.getDao(Theme.class);

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 添加一条记录

  • @param theme

*/

public void add(Theme theme) {

try {

themeDao.create(theme);

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 删除一条记录

  • @param theme

*/

public void delete(Theme theme) {

try {

themeDao.delete(theme);

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 更新一条记录

  • @param theme

*/

public void update(Theme theme) {

try {

themeDao.update(theme);

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 查询一条记录

  • @param id

  • @return

*/

public Theme queryForId(int id) {

Theme theme = null;

try {

theme = themeDao.queryForId(id);

} catch (SQLException e) {

e.printStackTrace();

}

return theme;

}

/**

  • 查询所有记录

  • @return

*/

public List queryForAll() {

List themes = new ArrayList();

try {

themes = themeDao.queryForAll();

} catch (SQLException e) {

e.printStackTrace();

}

return themes;

}

}

上面笔者定义了一个Dao类,用来进行数据访问的,定义了增加、删除、更新、查询几个方法,我们在应用中就可以使用这个几个方法来帮助我们完成相关的操作。

具体使用方法:

Theme theme = new Theme();

// 赋值

theme.id = 1;

theme.title = “主题”;

theme.detail = “主题详情”;

new ThemeDao(context).add(theme);

ormlite的批处理操作

/**

  • 转化json对象为数据库对象

  • @param context

  • @param theme

  • @return

  • @throws SQLException

  • @throws Exception

*/

public static Theme ConvertTheme(Context context, final JSONObject theme) throws SQLException, Exception {

JSONObject photographerObj = theme.getJSONObject(“photographer”);

JSONObject packageObj = theme.getJSONObject(“package”);

ThemeDao themeDao = new ThemeDao(context);

PhotographDao photographDao = new PhotographDao(context);

// 根据id查询摄影师

Photographer mPhotographer = photographDao.queryForId(theme.optInt(“photographerId”));

if (mPhotographer == null)

mPhotographer = new Photographer();

mPhotographer.id = theme.optString(“photographerId”);

mPhotographer.name = photographerObj.optString(“nickname”);

mPhotographer.serviceArea = photographerObj.optString(“serviceArea”);

mPhotographer.avatar = photographerObj.optString(“avatar”);

// 这里创建或更新摄影师

photographDao.createOrUpdate(mPhotographer);

PackageDao packageDao = new PackageDao(context);

// 根据id查询套餐

PackageInfo mPackage = packageDao.queryForId(packageObj.optInt(“id”));

if (mPackage == null)

mPackage = new PackageInfo();

mPackage.id = packageObj.optInt(“id”);

mPackage.name = packageObj.optString(“title”);

mPackage.cost = packageObj.optInt(“cost”);

mPackage.detail = packageObj.optString(“detail”);

// 这里创建或更新套餐

packageDao.createOrUpdate(mPackage);

// 根据id查询作品

Theme mThemeTmp = themeDao.queryForId(

theme.optInt(“id”));

if (mThemeTmp == null)

mThemeTmp = new Theme();

final Theme mTheme = mThemeTmp;

mTheme.id = theme.optString(“id”);

mTheme.title = theme.optString(“title”);

// mTheme.coverId = theme.optString(“place”);

// mTheme.coverUrl = theme.optString(“coverUrl”);

mTheme.photographerId = theme.optString(“photographerId”);

mTheme.detail = theme.optString(“detail”);

// mTheme.cost = theme.optDouble(“cost”);

// mTheme.recordTime = theme.optString(“recordTime”);

mTheme.favStatus = theme.optInt(“isFav”);

mTheme.photoCount = theme.optInt(“photoCount”);

mTheme.tags = theme.optString(“tags”);

mTheme.packageId = theme.optString(“packageId”);

// 同步更新

mTheme.photographer = mPhotographer;

mTheme.mPackage = mPackage;

// 创建或更新主题

themeDao.createOrUpdate(mTheme);

final ImgDao mDao = new ImgDao(context);

Dao<Img, Integer> imgDao = mDao.getImgDao();

// 执行批处理操作

imgDao.callBatchTasks(new Callable() {

@Override

public Void call() throws Exception {

JSONArray imgs = theme.getJSONArray(“photos”);

for (int i = 0; i < imgs.length(); i++) {

JSONObject jsonObject = imgs.getJSONObject(i);

Img mImg = mDao.queryForId(jsonObject.optInt(“id”));

if (mImg == null)

mImg = new Img();

mImg.id = jsonObject.optString(“id”);

mImg.isCover = jsonObject.optInt(“isCover”);

mImg.imgUrl = jsonObject.optString(“url”);

mImg.theme = mTheme;

mDao.createOrUpdate(mImg);

}

return null;

}

});

return mTheme;

}

上面的代码就是把我们从服务端获取的json对象进行数据转化,我们对json数据进行解析,得到相应的数据库对象,再进行创建或更新的操作,如果涉及到较多的插入,就可以使用ormlite为我们提供的批处理回调方法,具体看代码。

在Android中使用

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

文末

初级工程师拿到需求会直接开始做,然后做着做着发现有问题了,要么技术实现不了,要么逻辑有问题。

而高级工程师拿到需求会考虑很多,技术的可行性?对现有业务有没有帮助?对现有技术架构的影响?扩展性如何?等等…之后才会再进行设计编码阶段。

而现在随着跨平台开发,混合式开发,前端开发之类的热门,Android开发者需要学习和掌握的技术也在不断的增加。

通过和一些行业里的朋友交流讨论,以及参考现在大厂面试的要求。我们花了差不多一个月时间整理出了这份Android高级工程师需要掌握的所有知识体系。你可以看下掌握了多少。

混合式开发,微信小程序。都是得学会并且熟练的

这些是Android相关技术的内核,还有Java进阶

高级进阶必备的一些技术。像移动开发架构项目实战等

Android前沿技术;包括了组件化,热升级和热修复,以及各种架构跟框架的详细技术体系

以上即是我们整理的Android高级工程师需要掌握的技术体系了。可能很多朋友觉得很多技术自己都会了,只是一些新的技术不清楚而已。应该没什么太大的问题。

而这恰恰是问题所在!为什么别人高级工程师能年限突破30万,而你只有十几万呢?

就因为你只需补充你自己认为需要的,但并不知道企业需要的。这个就特别容易造成差距。因为你的技术体系并不系统,是零碎的,散乱的。那么你凭什么突破30万年薪呢?

我这些话比较直接,可能会戳到一些人的玻璃心,但是我知道肯定会对一些人起到点醒的效果的。而但凡只要有人因为我的这份高级系统大纲以及这些话找到了方向,并且付出行动去提升自我,为了成功变得更加努力。那么我做的这些就都有了意义。

喜欢的话请帮忙转发点赞一下能让更多有需要的人看到吧。谢谢!

以上系统大纲里包含的所有技术资料,我这里都有的。可以免费分享给有需要的朋友!

发,微信小程序。都是得学会并且熟练的

[外链图片转存中…(img-vpdp1mzs-1710767287409)]

这些是Android相关技术的内核,还有Java进阶

[外链图片转存中…(img-ZSGPAz9M-1710767287409)]

高级进阶必备的一些技术。像移动开发架构项目实战等

[外链图片转存中…(img-F5xY6Uua-1710767287410)]

Android前沿技术;包括了组件化,热升级和热修复,以及各种架构跟框架的详细技术体系

[外链图片转存中…(img-uv5ucNIw-1710767287410)]

以上即是我们整理的Android高级工程师需要掌握的技术体系了。可能很多朋友觉得很多技术自己都会了,只是一些新的技术不清楚而已。应该没什么太大的问题。

而这恰恰是问题所在!为什么别人高级工程师能年限突破30万,而你只有十几万呢?

就因为你只需补充你自己认为需要的,但并不知道企业需要的。这个就特别容易造成差距。因为你的技术体系并不系统,是零碎的,散乱的。那么你凭什么突破30万年薪呢?

我这些话比较直接,可能会戳到一些人的玻璃心,但是我知道肯定会对一些人起到点醒的效果的。而但凡只要有人因为我的这份高级系统大纲以及这些话找到了方向,并且付出行动去提升自我,为了成功变得更加努力。那么我做的这些就都有了意义。

喜欢的话请帮忙转发点赞一下能让更多有需要的人看到吧。谢谢!

以上系统大纲里包含的所有技术资料,我这里都有的。可以免费分享给有需要的朋友!

资料领取方式:点击我的GitHub

  • 28
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值