Castor:基于XML的Java开源数据绑定框架(支援JDO)

官方网站:http://www.castor.org/index.html

所需Jar包一览:

castor-1.2.jar
castor-1.2-anttasks.jar
castor-1.2-codegen.jar
castor-1.2-ddlgen.jar
castor-1.2-jdo.jar
castor-1.2-xml.jar
castor-1.2-xml-schema.jar
jdbc-se2.0.jar
jta1.0.1.jar
xerces-J_1.4.0.jar
commons-logging-1.1.jar
mysql-connector-java-5.1.6-bin.jar
log4j-1.2.13.jar

 

castor.properties

  1. # Example properties indent the output, require validation
  2. # on input and turn debugging on.
  3. #
  4. org.exolab.castor.validation=false
  5. org.exolab.castor.indent=true
  6. org.exolab.castor.debug=true
  7. # True if database configuration should be initalization
  8. # when loading it (default: true).
  9. #
  10. #org.exolab.castor.jdo.DatabaseInitializeAtLoad=true
  11. # TransactionManagerFactory implementations
  12. org.castor.transactionmanager.Factories=/
  13.   org.castor.transactionmanager.WebSphereTransactionManagerFactory,/
  14.   org.castor.transactionmanager.WebSphere5TransactionManagerFactory,/
  15.   org.castor.transactionmanager.WebSphere51TransactionManagerFactory,/
  16.   org.castor.transactionmanager.LocalTransactionManagerFactory,/
  17.   org.castor.transactionmanager.JNDIENCTransactionManagerFactory,/
  18.   org.castor.transactionmanager.JOTMTransactionManagerFactory
  19. #
  20. # Shell the TransactionManager be initialized at registration or lazily when
  21. # requested for the first time? Defaults to false.
  22. #
  23. org.castor.transactionmanager.InitializeAtRegistration=false

工程布局:

 

jdo-conf.xml

  1. <?xml version="1.0" ?>
  2. <!DOCTYPE jdo-conf PUBLIC "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN"
  3.  "http://castor.org/jdo-conf.dtd">
  4. <jdo-conf>
  5.     <database name="castordb" engine="mysql" >
  6.     
  7.         <driver url="jdbc:mysql://localhost:3306/castordb" 
  8.             class-name="com.mysql.jdbc.Driver">
  9.             <param name="user" value="root" />
  10.             <param name="password" value="root" />
  11.         </driver>
  12.         
  13.         <mapping href="jdo/mapping.xml" />
  14.         
  15.     </database>
  16.     
  17.     <transaction-demarcation mode="local">
  18.         <transaction-manager name="local" />
  19.     </transaction-demarcation>
  20. </jdo-conf>

mapping.xml

  1. <?xml version="1.0" ?>
  2. <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
  3.                            "http://castor.org/mapping.dtd">
  4. <mapping>
  5.     <class name="myapp.ProductGroup" identity="id">
  6.         <description>Product group</description>
  7.         <map-to table="prod_group" xml="group" />
  8.         <field name="id" type="integer">
  9.             <sql name="id" type="integer" />
  10.             <bind-xml node="attribute" />
  11.         </field>
  12.         <field name="name" type="string">
  13.             <sql name="name" type="char" dirty="check" />
  14.             <bind-xml node="text" />
  15.         </field>
  16.     </class>
  17.     <class name="myapp.Product" identity="id">
  18.         <description>Product definition</description>
  19.         <map-to table="prod" xml="product" />
  20.         <field name="id" type="integer">
  21.             <sql name="id" type="integer" />
  22.             <bind-xml name="id" node="attribute" />
  23.         </field>
  24.         <field name="name" type="string">
  25.             <sql name="name" type="char" />
  26.             <bind-xml name="name" node="element" />
  27.         </field>
  28.         <field name="price" type="float">
  29.             <sql name="price" type="numeric" />
  30.             <bind-xml name="price" node="element" />
  31.         </field>
  32.         <field name="group" type="myapp.ProductGroup">
  33.             <sql name="group_id" />
  34.             <bind-xml name="group" node="element" />
  35.         </field>
  36.         <field name="details" type="myapp.ProductDetail" required="true"
  37.             collection="vector">
  38.             <sql many-key="prod_id" />
  39.             <bind-xml name="detail" node="element" />
  40.         </field>
  41.         <field name="categories" type="myapp.Category" required="true" collection="vector">
  42.             <sql name="category_id" many-table="category_prod"
  43.                 many-key="prod_id" />
  44.             <bind-xml name="category" node="element" />
  45.         </field>
  46.     </class>
  47.     <class name="myapp.ProductDetail" identity="id" depends="myapp.Product">
  48.         <description>Product detail</description>
  49.         <map-to table="prod_detail" xml="detail" />
  50.         <field name="id" type="integer">
  51.             <sql name="id" type="integer" />
  52.             <bind-xml node="attribute" />
  53.         </field>
  54.         <field name="product" type="myapp.Product">
  55.             <sql name="prod_id" />
  56.             <bind-xml name="product" node="element" reference="true" />
  57.         </field>
  58.         <field name="name" type="string">
  59.             <sql name="name" type="char" />
  60.             <bind-xml node="text" />
  61.         </field>
  62.     </class>
  63.     <class name="myapp.Computer" extends="myapp.Product" identity="id">
  64.         <description>
  65.             Computer definition, extends generic product
  66.         </description>
  67.         <map-to table="computer" xml="computer" />
  68.         <field name="id" type="integer">
  69.             <sql name="id" type="integer" />
  70.             <bind-xml name="id" node="attribute" />
  71.         </field>
  72.         <field name="cpu" type="string">
  73.             <sql name="cpu" type="char" />
  74.             <bind-xml node="element" />
  75.         </field>
  76.     </class>
  77.     <class name="myapp.Category" identity="id">
  78.         <description>
  79.             A product category, any number of products can belong to the
  80.             same category, a product can belong to any number of
  81.             categories
  82.         </description>
  83.         <map-to table="category" xml="category" />
  84.         <field name="id" type="integer">
  85.             <sql name="id" type="integer" />
  86.             <bind-xml />
  87.         </field>
  88.         <field name="name" type="string">
  89.             <sql name="name" type="char" />
  90.             <bind-xml node="element" />
  91.         </field>
  92.         <field name="products" type="myapp.Product" required="true"
  93.             collection="vector">
  94.             <sql name="prod_id" many-table="category_prod"
  95.                 many-key="category_id" />
  96.             <bind-xml name="product" node="element" />
  97.         </field>
  98.     </class>
  99. </mapping>

Category.java

  1. package myapp;
  2. import java.util.Vector;
  3. public class Category {
  4.     
  5.     private int _id;
  6.     private Vector<Product> _products = new Vector<Product>();
  7.     private String _name;
  8.     public int getId(){
  9.         return _id;
  10.     }
  11.     public void setId( int id ) {
  12.         _id = id;
  13.     }
  14.     
  15.     public String getName() {
  16.         return _name;
  17.     }
  18.     public void setName( String name ){
  19.         _name = name;
  20.     }
  21.     public Vector<Product> getProducts() {
  22.         return _products;
  23.     }
  24.     public void addProduct( Product product ) {
  25.         
  26.         if ( ! _products.contains( product ) ) {
  27.             System.out.println( "Adding product " + product + " to category " + this );
  28.             _products.addElement( product );
  29.             product.addCategories( this );
  30.         }
  31.     }
  32.     public String toString() {
  33.         return "<id: " + _id + " name: " + _name + ">";
  34.     }
  35. }

Computer.java

  1. package myapp;
  2. import org.exolab.castor.jdo.TimeStampable;
  3. public class Computer extends Product implements TimeStampable {
  4.     
  5.     private String _cpu;
  6.     private long _timeStamp;
  7.     
  8.     public String getCpu() {
  9.         return _cpu;
  10.     }
  11.     public void setCpu(String cpu) {
  12.         _cpu = cpu;
  13.     }
  14.     public void jdoSetTimeStamp(long timeStamp) {
  15.         _timeStamp = timeStamp;
  16.     }
  17.     public long jdoGetTimeStamp() {
  18.         return _timeStamp;
  19.     }
  20.     public String toString() {
  21.         return "<id: " + getId() + " name: " + getName() + " price: "
  22.                 + getPrice() + " cpu: " + _cpu + ">";
  23.     }
  24. }

Product.java

  1. package myapp;
  2. import java.util.Vector;
  3. import org.exolab.castor.jdo.Database;
  4. import org.exolab.castor.jdo.Persistent;
  5. import org.exolab.castor.jdo.TimeStampable;
  6. import org.exolab.castor.mapping.AccessMode;
  7. public class Product implements Persistent, TimeStampable {
  8.     
  9.     private int _id;
  10.     private String _name;
  11.     private float _price;
  12.     private ProductGroup _group;
  13.     private long _timeStamp;
  14.     private Vector<ProductDetail> _details = new Vector<ProductDetail>();
  15.     private Vector<Category> _categories = new Vector<Category>();
  16.     public int getId() {
  17.         return _id;
  18.     }
  19.     public void setId(final int id) {
  20.         _id = id;
  21.     }
  22.     public String getName() {
  23.         return _name;
  24.     }
  25.     public void setName(final String name) {
  26.         _name = name;
  27.     }
  28.     public float getPrice() {
  29.         return _price;
  30.     }
  31.     public void setPrice(final float price) {
  32.         _price = price;
  33.     }
  34.     public ProductGroup getGroup() {
  35.         return _group;
  36.     }
  37.     public void setGroup(final ProductGroup group) {
  38.         _group = group;
  39.     }
  40.     public ProductDetail createDetail() {
  41.         return new ProductDetail();
  42.     }
  43.     public Vector<ProductDetail> getDetails() {
  44.         return _details;
  45.     }
  46.     public void addDetail(final ProductDetail detail) {
  47.         _details.add(detail);
  48.         detail.setProduct(this);
  49.     }
  50.     public Vector<Category> getCategories() {
  51.         return _categories;
  52.     }
  53.     public void addCategories(final Category category) {
  54.         if (!_categories.contains(category)) {
  55.             _categories.addElement(category);
  56.             category.addProduct(this);
  57.         }
  58.     }
  59.     public void jdoPersistent(final Database db) {
  60.     }
  61.     public void jdoTransient() {
  62.     }
  63.     public Class<Computer> jdoLoad(final AccessMode accessMode) {
  64.         
  65.         if (_name.indexOf("PC") >= 0) {
  66.             return Computer.class;
  67.         }
  68.         return null;
  69.     }
  70.     public void jdoBeforeCreate(final Database db) {
  71.     }
  72.     public void jdoAfterCreate() {
  73.     }
  74.     public void jdoStore(final boolean modified) {
  75.     }
  76.     public void jdoBeforeRemove() {
  77.     }
  78.     public void jdoAfterRemove() {
  79.     }
  80.     public void jdoUpdate() {
  81.     }
  82.     public void jdoSetTimeStamp(final long timeStamp) {
  83.         _timeStamp = timeStamp;
  84.     }
  85.     public long jdoGetTimeStamp() {
  86.         return _timeStamp;
  87.     }
  88.     public String toString() {
  89.         return "<id: " + _id + " name: " + _name + " price: " + _price + ">";
  90.     }
  91. }

ProductDetail.java

  1. package myapp;
  2. public class ProductDetail {
  3.     private int _id;
  4.     private Product _product;
  5.     private String _name;
  6.     public int getId() {
  7.         return _id;
  8.     }
  9.     public void setId(int id) {
  10.         _id = id;
  11.     }
  12.     public String getName() {
  13.         return _name;
  14.     }
  15.     public void setName(String name) {
  16.         _name = name;
  17.     }
  18.     public Product getProduct() {
  19.         return _product;
  20.     }
  21.     public void setProduct(Product product) {
  22.         _product = product;
  23.     }
  24.     public String toString() {
  25.         return "<id: " + _id + " name: " + _name + ">";
  26.     }
  27. }
ProductGroup.java

 

  1. package myapp;
  2. public class ProductGroup {
  3.     private int _id;
  4.     private String _name;
  5.     public int getId() {
  6.         return _id;
  7.     }
  8.     public void setId(int id) {
  9.         _id = id;
  10.     }
  11.     public String getName() {
  12.         return _name;
  13.     }
  14.     public void setName(String name) {
  15.         _name = name;
  16.     }
  17.     public String toString() {
  18.         return "<id: " + _id + " name: " + _name + ">";
  19.     }
  20. }

测试代码:

CastorDemo.java

  1. package jdo;
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. import myapp.Category;
  5. import myapp.Computer;
  6. import myapp.Product;
  7. import myapp.ProductDetail;
  8. import myapp.ProductGroup;
  9. import org.exolab.castor.jdo.Database;
  10. import org.exolab.castor.jdo.JDOManager;
  11. import org.exolab.castor.jdo.OQLQuery;
  12. import org.exolab.castor.jdo.QueryResults;
  13. import org.exolab.castor.mapping.Mapping;
  14. public class CastorDemo {
  15.     private static final String DATABASE_NAME = "castordb";
  16.     public static final String JDO_CONFIG_FILE = "jdo-conf.xml";
  17.     public static final String MAPPING_FILE = "mapping.xml";
  18.     
  19.     public static void main(String[] args) throws Exception {
  20.         
  21.         Mapping mapping = new Mapping(CastorDemo.class.getClassLoader());
  22.         mapping.loadMapping(CastorDemo.class.getResource(MAPPING_FILE));
  23.         
  24.         String jdoConf = CastorDemo.class.getResource(JDO_CONFIG_FILE).toString();
  25.         JDOManager.loadConfiguration(jdoConf);
  26.         JDOManager manager = JDOManager.createInstance(DATABASE_NAME);
  27.         Database db = manager.getDatabase();
  28.         
  29.         createTable(manager);
  30.         
  31.         db.begin();
  32.         ProductGroup group = new ProductGroup();
  33.         group.setId( 3 );
  34.         group.setName( "a group" );
  35.         db.create( group );
  36.         Product product = new Product();
  37.         product.setId( 4 );
  38.         product.setName( "product4" );
  39.         product.setPrice( 200 );
  40.         product.setGroup( group );
  41.         ProductDetail detail = new ProductDetail();
  42.         detail.setId( 1 );
  43.         detail.setName( "keyboard" );
  44.         product.addDetail( detail );
  45.         detail = new ProductDetail();
  46.         detail.setId( 2 );
  47.         detail.setName( "mouse" );
  48.         product.addDetail( detail );
  49.         detail = new ProductDetail();
  50.         detail.setId( 3 );
  51.         detail.setName( "monitor" );
  52.         product.addDetail( detail );
  53.         db.create( product );
  54.         
  55.         Category category = new Category();
  56.         category.setId( 1 );
  57.         category.setName( "category" + category.getId() );
  58.         category.addProduct( product );
  59.         db.create( category );
  60.         
  61.         Computer computer = new Computer();
  62.         computer.setId( 7 );
  63.         computer.setCpu( "Pentium" );
  64.         computer.setName( "MyPC" );
  65.         computer.setPrice( 400 );
  66.         computer.setGroup( group );
  67.         detail = new ProductDetail();
  68.         detail.setId( 4 );
  69.         detail.setName( "network card" );
  70.         computer.addDetail( detail );
  71.         detail = new ProductDetail();
  72.         detail.setId( 5 );
  73.         detail.setName( "scsi card" );
  74.         computer.addDetail( detail );
  75.         db.create(computer);
  76.      
  77.         db.commit();
  78.         //======================================================
  79.         
  80.         db.begin();
  81.         OQLQuery productOql = db.getOQLQuery( "SELECT p FROM myapp.Product p WHERE p.id > $1" );
  82.         productOql.bind( 1 );
  83.         QueryResults results = productOql.execute();
  84.         while(results.hasMore()) {
  85.             product = ( Product ) results.next();
  86.             System.out.println(product.getName());
  87.             //db.remove( product );
  88.         }
  89.         db.commit();
  90.         
  91.         //======================================================
  92.         
  93.         db.begin();
  94.         product = ( Product ) db.load( Product.class7 );
  95.         System.out.println(product.getName() + " " + product.getPrice());
  96.         db.commit();
  97.         
  98.         db.begin();
  99.         product.setPrice(700);
  100.         db.update(product);
  101.         db.commit();
  102.         
  103.         //======================================================
  104. //      Marshaller marshaller = new Marshaller( new PrintWriter(System.out));
  105. //      marshaller.setMapping( mapping );
  106. //      marshaller.marshal(product);
  107. //              
  108. //      db.begin();
  109. //      OQLQuery computerOql = db.getOQLQuery( "SELECT c FROM myapp.Computer c WHERE c.id >= $1" );
  110. //      computerOql.bind( 7 );
  111. //      results = computerOql.execute();
  112. //      
  113. //      while( results.hasMore() )
  114. //           marshaller.marshal( results.next() );
  115. //      db.commit();
  116.         
  117.         db.close();
  118.       
  119. //        MappingRoot mappingRoot = mapping.getRoot();
  120. //        ClassMapping classMap;
  121. //        FieldMapping fieldMap;
  122. //        FieldMapping[] fieldMaps;
  123. //        int mappingCount = mappingRoot.getClassMappingCount();
  124. //        for ( int i = 0; i < mappingCount; ++i ){
  125. //            classMap = mappingRoot.getClassMapping( i );
  126. //            fieldMaps = classMap.getClassChoice().getFieldMapping();
  127. //            for ( int j = 0; j < fieldMaps.length; ++j ){
  128. //                fieldMap = fieldMaps[j];
  129. //                System.out.println("        Field name: " + fieldMap.getName());
  130. //                System.out.println("        Field type: " + fieldMap.getType());
  131. //            }
  132. //        }
  133.     }
  134.     
  135.     public static void createTable(JDOManager manager) throws Exception {
  136.         
  137.         Database db = manager.getDatabase();
  138.         db.begin();
  139.         Connection connection = db.getJdbcConnection();
  140.         Statement statement = connection.createStatement();
  141.         statement.executeUpdate("create table prod (id int not null, name varchar(200) not null, price numeric(18,2) not null,  group_id int not null)");
  142.         statement.executeUpdate("create table prod_group (id int not null, name varchar(200) not null)");
  143.         statement.executeUpdate("create table prod_detail (id int not null, prod_id int not null, name varchar(200) not null)");
  144.         statement.executeUpdate("create table computer (id int not null, cpu varchar(200) not null)");
  145.         statement.executeUpdate("create table category (id int not null, name varchar(200) not null)");
  146.         statement.executeUpdate("create table category_prod (prod_id int not null, category_id int not null)");
  147.         db.commit();
  148.         db.close();
  149.     }
  150. }
Castor和Spring集成: http://www.castor.org/spring-orm-integration.html

Castor介绍资料:

http://www.ibm.com/developerworks/cn/xml/x-xjavacastor1/

http://www.ibm.com/developerworks/cn/xml/x-xjavacastor2/

http://www.ibm.com/developerworks/cn/xml/x-xjavacastor3/

http://www.ibm.com/developerworks/cn/xml/x-xjavacastor4/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值