三、現有的O/R Mapping產品介紹
來源:互聯網
上載者:User
接上)
具體過程如下:
(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介紹》)