前言

  个人感觉android上sqlite已经比较好用,但是如果需要在android上像J2EE那样开发的话那么sqlite还是显得比较复杂,这个时候你当然可以选择一些android平台上的ORM框架。ORM框架是做什么的,android平台上有哪些好用的ORM框架,这些问题你通通可以google得到你满意的结果,在此不做解释。本系列主要是翻译ORMlite文档。

  为什么翻译ORMlite?简单点说就是因为个人觉得ORMlite是android平台上不错的ORM框架,官方也提供了很多相关介绍。但是几乎没有看到有中文文档。为了更多人可以学习到这个框架所以决定把官方的文档翻译成中文。个人英语水平非常有限,但是本人会尽力,如果文中有不妥的翻译之处敬请告之。文档中的比较杂的内容就不在文档中进行翻译,比如参见xxx连接,这些没有意义。主要翻译的技术文档而非专业英语,但基本会保持原文档的内容。

-------------------------------------------------------------------------------------

1获得开始

1.1下载ORMlitejar

为了使用ORMLite,你需要下载相关的jar文件。ORMLite发布包是一个默认库,不过相关jar文件也可以通过网络从内部资源库里面获得。

通过JDBC连接SQL数据库的用户需要下载ormlite-jdbc-4.41.jar和ormlite-core-4.41.jar两个文件。在android应用中使用,你需要下载ormlite-android-4.41.jarormlite-core-4.41.jar两个文件。在有ORMlite后台实现的JDBC中或者是Android中你都需要ormlite-core发布包。ORMLite没有任何外部依赖,即使可能有你想用的其他选用包。

1.2配置class

下面是个class通过使用ORMlite注解配置持久化到数据的例子。@DatabaseTable注解配置Access类到数据库名为accounts的表。@DatabaseField注解映射Account中的每个字段到数据库中相同名字的字段。

一个字段需要配置成数据库中表的主键那么可以通过使用id=true注解字段。并且,值得一提的是一个无参的构造器是必须的,这样的话通过查询可以返回一个对象。

@DatabaseTable(tableName = "accounts")public class Account {@DatabaseField(id = true)private String name;@DatabaseFieldprivate String password;public Account() {// ORMLite needs a no-arg constructor}public Account(String name, String password) {this.name = name;this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}


1.3 配置 DAO

一个典型的java隔离数据库操作的模式是用数据访问对象类(DataAccessObjects,即DAO)。每个DAO提供增、删、改等操作。这中功能专注于处理一个单一持久化的类。一种简单的创建DAO的方式是使用DaoManager类的静态方法createDao。例如,为上面定义的Account类创建一个DAO你可以这样做:

Dao<Account, String> accountDao =DaoManager.createDao(connectionSource, Account.class);Dao<Order, Integer> orderDao =DaoManager.createDao(connectionSource, Order.class);


1.4 代码示例

这个示例使用本地JavaH2数据库,创建一个在内存中运行的测试数据库。如果你想把示例代码跑起来那么你需要下载并且添加H2jar文件到你的classpath中。注意:android用户需要看本手册android部分具体的文档。

代码执行下面几步:

①创建connection,这个connection可以操作数据库。

②为Account对象实例化一个DAO

accounts数据库表已经创建过。如果这个表已经创建好的话那么这几步就不必要了。

public class AccountApp {public static void main(String[] args) throws Exception {// this uses h2 by default but change to match your databaseString databaseUrl = "jdbc:h2:mem:account";// create a connection source to our databaseConnectionSource connectionSource =new JdbcConnectionSource(databaseUrl);// instantiate the daoDao<Account, String> accountDao =DaoManager.createDao(connectionSource, Account.class);// if you need to create the 'accounts' table make this callTableUtils.createTable(connectionSource, Account.class);Once we have congured our database objects, we can use them to persist an Accountto the database and query for it from the database by its ID:// create an instance of AccountAccount account = new Account();account.setName("Jim Coakley");// persist the account object to the databaseaccountDao.create(account);// retrieve the account from the database by its id field (name)Account account2 = accountDao.queryForId("Jim Coakley");System.out.println("Account: " + account2.getName());// close the connection sourceconnectionSource.close();}}


通过这些要点你应该能够开始使用ORMLite了。为了理解ORMlite更多有效的功能,继续学习下一章。

PS:翻译持续更新中。如果转载请说明出处。

更多相关文章

  1. Android(安卓)创建与解析XML(一)—— 概述
  2. Android开发手记一 NDK编程实例
  3. Android(安卓)创建与解析XML(一)—— 概述
  4. Android(安卓)开发手记一NDK编程实例
  5. android 数据库sqlite的使用
  6. Android(安卓)开发中使用 SQLite 数据库
  7. Android创建和使用数据库详细指南(1)
  8. Android移植到VirtualBox及详细流程
  9. Android控件开发——ListView

随机推荐

  1. PHP高级编程函数的返回值和闭包与作用域
  2. OSG第一课学习
  3. [牛客习题]逆置字符串
  4. 最新网络安全指南,2021年你值得收藏的100
  5. PHP的接口(interface)和抽象类(abstract)
  6. Python学习系列之七大数据类型
  7. 强大:MyBatis 流式查询
  8. 面试被吊打系列 - 事务隔离级别
  9. 36 张图梳理 Intellij IDEA 常用设置
  10. TCP协议(可靠传输),滑动窗口机制,拥塞机制,避