Android数据库ORMlite框架翻译系列(第一章)

前言

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

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

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

1 获得开始

1.1 下载ORMlite jar

 
 

为了使用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;

@DatabaseField

private 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隔离数据库操作的模式是用数据访问对象类(Data Access Objects,即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 代码示例

这个示例使用本地Java H2数据库,创建一个在内存中运行的测试数据库。如果你想把示例代码跑起来那么你需要下载并且添加H2 jar文件到你的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 database

String databaseUrl = "jdbc:h2:mem:account";

// create a connection source to our database

ConnectionSource connectionSource =

new JdbcConnectionSource(databaseUrl);

// instantiate the dao

Dao<Account, String> accountDao =

DaoManager.createDao(connectionSource, Account.class);

// if you need to create the 'accounts' table make this call

TableUtils.createTable(connectionSource, Account.class);

Once we have congured our database objects, we can use them to persist an Account

to the database and query for it from the database by its ID:

// create an instance of Account

Account account = new Account();

account.setName("Jim Coakley");

// persist the account object to the database

accountDao.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 source

connectionSource.close();

}

}

 

 

 


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

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

posted on 2012-07-31 18:06  vanezkw  阅读(18290)  评论(4编辑  收藏  举报

导航