zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0

          [1]表結(jié)構(gòu)
          ? CREATE TABLE "POI_BEIJING"."XML_TEST"
          ?? (?
          ??? "ITEM_ID" NUMBER(10,0) NOT NULL ENABLE,
          ??? "ITEM_NAME" VARCHAR2(255 BYTE),
          ??? "ITEM_VALUE" "SYS"."XMLTYPE" ,
          ??? }
          [2]自定義類型(參考網(wǎng)上資料)
          參考了此老兄的思路http://blog.csdn.net/wmbb/archive/2006/08/10/1045742.aspx
          package com.csc.poimanager.dao.type;
          import java.io.Serializable;
          import java.sql.Connection;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import oracle.jdbc.driver.OracleResultSet;
          import oracle.sql.OPAQUE;
          import org.hibernate.HibernateException;
          import org.hibernate.usertype.UserType;
          public class PoiAdditionalXmlType implements UserType, Serializable {
          ??? private static final Class returnedClass = String.class;
          ??? private static final int[] SQL_TYPES = new int[] { oracle.xdb.XMLType._SQL_TYPECODE };
          ??? public int[] sqlTypes() {
          ??? ??? return SQL_TYPES;
          ??? }
          ??? public Class returnedClass() {
          ??? ??? return returnedClass;
          ??? }
          ??? public boolean equals(Object arg0, Object arg1) throws HibernateException {
          ??? ??? if (arg0 == null || arg1 == null) {
          ??? ??? ??? throw new HibernateException("None of the arguments can be null.");
          ??? ??? }
          ??? ??? if (arg0 instanceof oracle.xdb.XMLType
          ??? ??? ??? ??? && arg1 instanceof oracle.xdb.XMLType) {
          ??? ??? ??? return arg0.equals(arg1);
          ??? ??? }
          ??? ??? return false;
          ??? }
          ??? public int hashCode(Object arg0) throws HibernateException {
          ??? ??? return 0;
          ??? }
          ??? public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)
          ??? ??? ??? throws HibernateException, SQLException {
          ??? ??? OracleResultSet ors = (OracleResultSet) rs;
          ??? ??? OPAQUE op = ors.getOPAQUE(names[0]);
          ??? ??? oracle.xdb.XMLType xt = oracle.xdb.XMLType.createXML(op);
          ??? ??? return xt.getStringVal();
          ??? }

          ??? public void nullSafeSet(PreparedStatement st, Object value, int index)
          ??? ??? ??? throws HibernateException, SQLException {
          ??? ??? Connection conn = st.getConnection();
          ??? ??? OPAQUE aClob = oracle.xdb.XMLType.createXML(conn, (String) value);
          ??? ??? st.setObject(index, aClob);
          ??? }

          ??? public Object deepCopy(Object value) throws HibernateException {
          ??? ??? return value;
          ??? }
          ??? public boolean isMutable() {
          ??? ??? return false;
          ??? }
          ??? public Serializable disassemble(Object arg0) throws HibernateException {
          ??? ??? return null;
          ??? }
          ??? public Object assemble(Serializable arg0, Object arg1)
          ??? ??? ??? throws HibernateException {
          ??? ??? return null;
          ??? }
          ??? public Object replace(Object arg0, Object arg1, Object arg2)
          ??? ??? ??? throws HibernateException {
          ??? ??? return null;
          ??? }
          }

          [3]POJO
          package com.csc.poimanager.dao;
          import com.csc.poimanager.dao.type.PoiAdditionalXmlType;
          public class XmlTest implements java.io.Serializable {
          ??? private Long itemId;
          ??? private Long poiId;
          ??? private String itemName;
          ??? private String itemValue;
          ??? public XmlTest() {
          ??? }
          ??? public XmlTest(String itemName) {
          ??? ??? this.itemName = itemName;
          ??? }
          ??? public Long getItemId() {
          ??? ??? return this.itemId;
          ??? }
          ??? public void setItemId(Long itemId) {
          ??? ??? this.itemId = itemId;
          ??? }
          ??? public Long getPoiId() {
          ??? ??? return this.poiId;
          ??? }
          ??? public void setPoiId(Long poiId) {
          ??? ??? this.poiId = poiId;
          ??? }
          ??? public String getItemName() {
          ??? ??? return this.itemName;
          ??? }
          ??? public void setItemName(String itemName) {
          ??? ??? this.itemName = itemName;
          ??? }
          ??? public String getItemValue() {
          ??? ??? return this.itemValue;
          ??? }
          ??? public void setItemValue(String itemValue) {
          ??? ??? this.itemValue = itemValue;
          ??? }
          }

          [4]映射文件
          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
          <!--
          ??? Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
          ??? <class name="com.csc.poimanager.dao.XmlTest" table="XML_TEST">

          ??????? <id name="itemId" type="java.lang.Long">
          ??????????? <column name="ITEM_ID" precision="10" scale="0" />
          ??????????? <generator class="increment" />
          ??????? </id>

          ??????? <property name="poiId" type="java.lang.Long">
          ??????????? <column name="POI_ID" precision="10" scale="0" />
          ??????? </property>

          ??????? <property name="itemName" type="java.lang.String">
          ??????????? <column name="ITEM_NAME" />
          ??????? </property>??? ??? ?

          ??????? <property name="itemValue" type="com.csc.poimanager.dao.type.PoiAdditionalXmlType" >
          ??????????? <column name="ITEM_VALUE" />
          ??????? </property>


          ??? </class>
          </hibernate-mapping>

          [5]查詢操作
          ??????? XmlTestDAO xtdao = new XmlTestDAO();
          ??? ??? Session sess = xtdao.getSession();
          ??? ??? Transaction tx = sess.beginTransaction();
          ??? ???
          ??? ??? XmlTest xt =xtdao.findById((long)1);
          ??? ???
          //??? ??? xtdao.save(xt);
          ??? ???
          ??? ??? System.out.println("xt item_value : " + xt.getItemName());
          ??? ??? System.out.println("xt item_value : " + xt.getItemValue());
          ??? ???
          ??? ???
          ??? ??? tx.commit();
          ??? ??? sess.close();
          ??? ???
          ??? ??? System.out.println("getting xmltest ok ");
          ?? 執(zhí)行結(jié)果
          ??????? xt item_value : WIFI
          ??????? xt item_value : <?xml version="1.0"?>
          ??????????????????????????????? <wifi>Yes</wifi>
          ??????? getting xmltest ok


          [6]插入操作
          ?? ??? ??? XmlTestDAO xtdao = new XmlTestDAO();
          ??? ??? Session sess = xtdao.getSession();
          ??? ??? Transaction tx = sess.beginTransaction();
          ??? ??
          ? ? ? ? XmlTest xt =xtdao.findById((long)1);
          ?? ???? System.out.println("xt item_value : " + xt.getItemName());
          ??? ??? System.out.println("xt item_value : " + xt.getItemValue());
          ??? ??? tx.commit();
          ??? ??? sess.close();
          ??? ??? System.out.println("getting xmltest ok ");
          ? ? 執(zhí)行結(jié)果:
          ? ? saving xmltest ok? ??

          [8]注意
          ?? (1)oracle的XmlType不是字符串,是oracle的一種數(shù)據(jù)類型,就像varchar一樣
          ?? (2)POJO中的itemValue類型是由你的自定義類解析后的對象類型
          ????? 比如上面的實現(xiàn),是把XmlType對象解析成xml的串,是字符串類型。所以在POJO中的定義是String類型,而不是PoiAdditionalXmlTyp類型
          ?? (3)此處僅實現(xiàn)了把oracle中XmlType的值解析成string串,同時,可以把xml的string串保存到XmlType類型的字段里面


          ??


          |----------------------------------------------------------------------------------------|
                                     版權(quán)聲明  版權(quán)所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2008-12-25 15:43 zhyiwww 閱讀(2899) 評論(0)  編輯  收藏 所屬分類: j2eedatabase
          主站蜘蛛池模板: 崇州市| 延长县| 宝丰县| 仁怀市| 平远县| 台南市| 安仁县| 百色市| 曲水县| 兖州市| 永清县| 四会市| 鞍山市| 南丰县| 日喀则市| 政和县| 九江县| 东安县| 临邑县| 湖北省| 黎川县| 昔阳县| 台安县| 札达县| 精河县| 玛多县| 鹤峰县| 额尔古纳市| 房产| 灵台县| 奉化市| 蚌埠市| 丰宁| 金华市| 榆中县| 定襄县| 浦江县| 静乐县| 元氏县| 固安县| 仙居县|