(接上)
具体过程如下:
(1) 首先建立数据库配置文件,我们在这里定为 database.xml, 当然也可以改成是其它名字。
1<database engine="mysql" name="CustomerDemo">
2<driver class-name="org.gjt.mm.mysql.Driver" url="jdbc:mysql://cwb:3306/quickstart">
3<param name="user" value="dbusername"/>
4<param name="password" value="dbpassword "/>
5</driver>
6<mapping href="Customer.xml"></mapping>
7</database>
建立影射文件 Customer.xml
1<class access="shared" identity="customerID" name="Demo.Customer">
2<map-to table="users"></map-to>
3<field name="customerID" type="integer">
4<sql name="customerID" type="integer"></sql>
5</field>
6<field name="name" type="string">
7<sql name="name" type="varchar"></sql>
8</field>
9</class>
建立持久化类,与 hibernate 的是一样的类似 javabean 的类
package Demo ;
public class Customer {
private String name;
private int customerID;
public Customer() {
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
基本的实现后,我们可以看看这个 demo 怎么运行。
import java.util.*;
import org.exolab.castor.jdo.*;
import java.net.*;
public class CustomerManager {
JDO jdo;
Database db;
public CustomerManager() throws DatabaseNotFoundException,
PersistenceException {
// 定义一个 JDO 对象
jdo = new JDO();
jdo.setDatabaseName("CustomerDemo");
jdo.setConfiguration("database.xml");
jdo.setClassLoader(getClass().getClassLoader());
// 获得连接数据库
db = jdo.getDatabase();
}
/**
用于读取用户
@param id Customer 对象的主键
*/
public Customer loadCustomer(Integer id) throws DatabaseNotFoundException,
PersistenceException {
Customer result = null;
// 开始事务
db.begin();
result = (Customer) db.load(Customer.class, id);
// 完成事务,关闭数据库
db.commit();
db.close();
return result;
}
/**
用于建立用户
@param Customer newCustomer 新对象
*/
public void createCustomer(Customer newCustomer) throws
DatabaseNotFoundException,
PersistenceException {
Customer result = null;
db.begin();
// 新建 Customer
db.create(newCustomer);
db.commit();
db.close();
}
/**
- 更新旧的对象
*/
public Customer updateCustomer(Customer updateCustomer) throws
DatabaseNotFoundException,
PersistenceException {
db.begin();
// 更新 Customer
db.update(updateCustomer);
db.commit();
db.close();
return null;
}
public void removeCustomer(Customer removeCustomer) throws
DatabaseNotFoundException,
PersistenceException {
db.begin();
// 删除 Customer
db.remove(removeCustomer);
db.commit();
db.close();
}
}
在 Castor JDO 对象模型上执行查询
Castor 实现了对象查询语言( OQL )的 ODMG 3.0 规范的一个子集。 OQL 的语法类似于 SQL 的语法,但它却使您能够查询对象模型,而不是直接查询数据库。在支持多个数据库时,这可能是一项强大的功能。 Castor 的 OQL 实现在内部将 OQL 查询转换成用于数据库的适当的 SQL 。使用 bind() 方法将参数绑定到查询上。以下是 OQL 查询的一些简单示例。
Castor 的 OQL 实现并不在整个查询中继续使用全限定对象名,相反它支持对象别名的使用。在下面的这些查询中, c 就是这样的一个别名。
如果想要查询以找出所有 Customer ,可以执行下列查询:
SELECT c FROM Demo.Customer c
如果想要查询以找出标识等于 1234 的 Customer ,可以以:
SELECT c FROM Demo.Customer c WHERE c.CustomerID= $1
开始,后跟:
query.bind( 1234 )
要查询名称与特殊字符串相似的 Customer ,可以执行下列查询:
SELECT c FROM Demo.Customer c WHERE c.name LIKE $1
后跟:
query.bind( "%abcd%" )
3 、 ObjectSpaces
ObjectSpaces 是微软 .Net 下面的 O/R Mapping ,到目前为止还是 Beta 版,相信会在 VS.Net 2004 出现正式版。 .Net 下的 O/R Mapping 没有像 java 方面那样的兴旺,开放源码的也不多, OJB. Net 、 AtomsFramework 、 OPF.Net 等,都有相当的知名度,但还在不断的成熟之中。 ADO.Net 功能强大,与 JDBC 有很多不同的地方,所以 .Net 下的 O/R Mapping 有很多自己的特色。
现在简单的介绍下 ObjectSpaces 的用法,大家可以跟 Hibernate 和 JDO 比较一下。
ObjectSpaces 同样有一个配置 Source.xml 文件:
1<sources xmlns="http://www.microsoft.com/ObjectSpaces-v1">
2<!--- 数据连接的配置 \---->
3<source adapter="sql" connection="Data Source=LocalHost; Integrated Security=SSPI; Database=CustomerDemo" name="Demo"/>
4</sources>
每个持久化类也有对应的一个 map.xml :
1<map xmlns="http://www.microsoft.com/ObjectSpaces-v1">
2<type datasource="customer" name="Customer">
3<property datasource="customerID" name="customerID"></property>
4<property datasource="CustomerName" name="Name"></property>
5</type>
6</map>
大家有 Hibernate 上面的例子,相信很容易看得懂这段 xml ,很多都是大同小异。同样,也需要一个持久化类:
public abstract class Customer
{
// 定义主键
[UniqueId] public abstract int customerID { get; set; }
// 同样定义属性
public abstract string Name { get; set; }
public void OnCreate(int newId)
{
customerID = newId;
}
}
使用的例子:
// 装入 Source.xml ,建立 ObjectSpace 工厂
IObjectSpace os = ObjectSpaceFactory.CreateObjectSpace("Source.xml");
// 新建一个 Customer
Customer theCustomer = (Customer) os.CreateObject( typeof(Customer), "1" );
theCustomer.Name = "Karl";
// 保存新增的 Customer
os.UpdateAll();
如果需要用数据库保存持久化类,写法有点不同:
// 建立 Connection
string ConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=CustomerDemo;";
SqlConnection Connection = new SqlConnection( ConnectionString );
SqlDataAdapter theSqlDataAdapter = new SqlDataAdapter("select * from Customer",
Connection);
DataSet ds = new DataSet("Customer");
theSqlDataAdapter.Fill(ds, "Customer");
// 建立一个 DataSpace 实例
DataSpace theDataSpace = new DataSpace("Map.xml", ds);
// 从 DataSpace 取 Name 是 "Karl" 的 Customer.
Customer theCustomer = (Customer) theDataSpace.GetObject(typeof(Customer), "Name='Karl' ");
// 修改 Name
theCustomer.Name = "little karl"; </