接上)
 
     具体过程如下:
     (1)首先建立数据库配置文件,我们在这里定为database.xml,当然也可以改成是其它名字。
<?xml version="1.0" encoding="gb2312"?>
<database name="CustomerDemo" engine="mysql">
   <driver url="jdbc:mysql://cwb:3306/quickstart" class-name="org.gjt.mm.mysql.Driver">
       <param name="user" value="dbusername"/>
       <param name="password" value="dbpassword "/>
   </driver> 
   <mapping href="Customer.xml"/>
</database>
 
 
     建立影射文件Customer.xml
<?xml version="1.0" encoding="gb2312"?>
   <class name="Demo.Customer" access="shared" identity="customerID">
       <map-to table="users"/>
       <field name="customerID" type="integer">
            <sql name="customerID" type="integer"/>
       </field>
       <field name="name" type="string">
            <sql name="name" type="varchar"/>
       </field> 
   </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文件:
<sources xmlns="http://www.microsoft.com/ObjectSpaces-v1">
 
<!-数据连接的配置-->
<source name="Demo" adapter="sql" connection="Data Source=LocalHost; Integrated Security=SSPI; Database=CustomerDemo"/>
</sources>
 
     
     每个持久化类也有对应的一个map.xml:
<map xmlns="http://www.microsoft.com/ObjectSpaces-v1">
   <type name="Customer" dataSource="customer">
        <property name="customerID" dataSource="customerID"/>
        <property name="Name" dataSource="CustomerName"/>
</type>
</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"; 
 
 
     以上简单的介绍了一下Hibernate、JDO和ObjectSpaces的使用,要想更加的深入理会,那要好好自己研究下了。
     
      
(下一章 《四、我的第一版O/R Mapping介绍》)                    
三、现有的O/R Mapping产品介绍
                    80酷酷网    80kuku.com 
       
  
 
 
  
