Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区 » Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 spring+hibernate例子!
e_water_boy





发贴: 3
积分: 0
于 2005-08-22 09:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
今天终于完成了hibernate+spring的例子,虽然只是简单的实现了对一个表中数据的

查询,但具体的过程还是很有意义的。现在把源代码贴到这里:
IDE工具:eclipse3.0(插件:hibernate synchronize2.3.1)
框架:hibernate2.1+spring1.2
jar包:cglib-full-2.0.2.jar、commons-collections-2.1.1.jar、commons-dbcp-

1.2.1.jar、commons-lang-1.0.1.jar、commons-logging-1.0.4.jar、commons-

pool-1.2.jar、dom4j-1.4.jar、ehcache-0.9.jar、hibernate2.jar、jta.jar、

odmg-3.0.jar、spring.jar、xalan-2.4.0.jar、xerces-2.4.0.jar、xml-apis.jar

、jconn2.jar(我用的sybase数据库,这个是sybase的驱动)
数据库建表sql(sybase数据库):create table CUSTOMERS (ID int not null,

NAME nvarchar(15) not null, primary key (ID)) ;
create table ORDERS (
ID bigint not null,
ORDER_NUMBER varchar(15),
CUSTOMER_ID bigint,
primary key (ID)
);
alter table ORDERS add constraint FK_CUSTOMER foreign key ( CUSTOMER_ID)

references CUSTOMERS ;
(针对不同的数据有不同的sql语句,请自己修改)
用hibernate synchronize插件可以生成hiberante.cfg.xml、Customer.hbm(用这个

插件直接生成的是hbm文件,不是hbm.xml文件)、Order.hbm、Customer.java、

Order.java、以及DAO类(注意:生成的DAO类不适合用spring,所以这里就不提及了

)。
因为这里我们要使用spring,所以直接用spring的配置文件即可(把

hiberante.cfg.xml的内容集成到spring配置文件中)。
以下是所有文件的代码:
1.spring配置文件hibernate_context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!-- <bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property

name="viewClass"><value>org.springframework.web.servlet.view.JstlView</val

ue></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean> -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName">
<value>com.sybase.jdbc2.jdbc.SybDriver</value>
</property>

<property name="url">
<value>jdbc:sybase:Tds:172.16.9.40:5000/quickstart</value>
</property>

<property name="username">
<value>sa</value>
</property>

<property name="password">
<value></value>
</property>
</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="dataSource">
<ref local="dataSource" />
</property>

<property name="mappingResources">
<list>
<value>com/jacky/hibernate/domainmodel/Customer.hbm</value>
<value>com/jacky/hibernate/domainmodel/Order.hbm</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop

key="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="transactionManager"

class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<!-- Spring的数据访问异常转换器(Data Access Exception Translator)定义 -

->
<bean id="jdbcExceptionTranslator"

class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator

">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

<!-- Hibernate Template定义 -->
<bean id="hibernateTemplate"

class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="jdbcExceptionTranslator">
<ref bean="jdbcExceptionTranslator" />
</property>
</bean>

<!--baseTransactionProxy -->
<bean abstract="true"

class="org.springframework.transaction.interceptor.TransactionProxyFactory

Bean" id="baseTransactionProxy">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<bean id="customerDao"

class="com.jacky.hibernate.domainmodel.dao.CustomerHibernateDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<bean id="customerManagerTarget"

class="com.jacky.hibernate.service.CustomerManagerImpl">
<property name="customerDao">
<ref local="customerDao" />
</property>
</bean>

<bean id="customerManager"

class="org.springframework.transaction.interceptor.TransactionProxyFactory

Bean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>

<property name="target">
<ref local="customerManagerTarget" />
</property>

<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

</beans>


2.Customer.hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.jacky.hibernate.domainmodel">
  <class name="Customer" table="CUSTOMERS" lazy="false">
    <id
      column="ID"
      name="Id"
      type="integer"
    >
      <generator class="vm" />
    </id>
    <property
      column="NAME"
      length="15"
      name="Name"
      not-null="true"
      type="string"
     />
    <set inverse="true" name="ORDERSSet" lazy="false" batch-

size="4" outer-join="false">
      <key column="CUSTOMER_ID" />
      <one-to-many class="Order" />
    </set>
  </class>
</hibernate-mapping>


3.Order.hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.jacky.hibernate.domainmodel">
  <class name="Order" table="ORDERS" lazy="true">
    <id
      column="ID"
      name="Id"
      type="integer"
    >
      <generator class="vm" />
    </id>
    <property
      column="ORDER_NUMBER"
      length="15"
      name="OrderNumber"
      not-null="true"
      type="string"
     />
    <many-to-one
      class="Customer"
      name="Customer"
      not-null="true"
      outer-join="true"
      
    >
      <column name="CUSTOMER_ID" />
    </many-to-one>
  </class>
</hibernate-mapping>


4.Customer.java:

package com.jacky.hibernate.domainmodel;

import com.jacky.hibernate.domainmodel.base.BaseCustomers;
//import java.util.Set;
//import java.util.HashSet;
/**
* This is the object class that relates to the CUSTOMERS table.
* Any customizations belong here.
*/
public class Customer extends BaseCustomers {
  
/*[CONSTRUCTOR MARKER BEGIN]*/
  public Customer () {
    super();
  }

  /**
   * Constructor for primary key
   */
  public Customer (java.lang.Integer _id) {
    super(_id);
  }

  /**
   * Constructor for required fields
   */
  public Customer (
    java.lang.Integer _id,
    java.lang.String _name) {

    super (
      _id,
      _name);
  }

/*[CONSTRUCTOR MARKER END]*/
  
}


5.BaseCustomer.java:

package com.jacky.hibernate.domainmodel.base;

import java.io.Serializable;

/**
* This class has been automatically generated by Hibernate Synchronizer.


* For more information or documentation, visit The Hibernate Synchronizer

page
* at http://www.binamics.com/hibernatesync or contact Joe Hudson at

joe@binamics.com.
*
* This is an object that contains data related to

the CUSTOMERS table.
* Do not modify this class because it will be overwritten if the

configuration file
* related to this class is modified.
*
* @hibernate.class
* table="CUSTOMERS"
*/
public abstract class BaseCustomer implements Serializable {

  public static String PROP_NAME = "Name";
  public static String PROP_ID = "Id";

  private int hashCode = Integer.MIN_VALUE;

  // primary key
  private java.lang.Integer _id;

  // fields
  private java.lang.String _name;

  // collections
  private java.util.Set _oRDERSSet;

  // constructors
  public BaseCustomer () {
    initialize();
  }

  /**
   * Constructor for primary key
   */
  public BaseCustomer (java.lang.Integer _id) {
    this.setId(_id);
    initialize();
  }

  /**
   * Constructor for required fields
   */
  public BaseCustomer (
    java.lang.Integer _id,
    java.lang.String _name) {

    this.setId(_id);
    this.setName(_name);
    initialize();
  }

  protected void initialize () {}

  /**
   * Return the unique identifier of this class
* @hibernate.id
* generator-class="vm"
* column="ID"
*/
  public java.lang.Integer getId () {
    return _id;
  }

  /**
   * Set the unique identifier of this class
   * @param _id the new ID
   */
  public void setId (java.lang.Integer _id) {
    this._id = _id;
    this.hashCode = Integer.MIN_VALUE;
  }

  /**
   * Return the value associated with the column: NAME
   */
  public java.lang.String getName () {
    return _name;
  }

  /**
   * Set the value related to the column: NAME
   * @param _name the NAME value
   */
  public void setName (java.lang.String _name) {
    this._name = _name;
  }

  /**
   * Return the value associated with the column: ORDERSSet
  

*/
  public java.util.Set getORDERSSet () {
    return this._oRDERSSet;
  }

  /**
   * Set the value related to the column: ORDERSSet
   * @param _oRDERSSet the ORDERSSet value
   */
  public void setORDERSSet (java.util.Set _oRDERSSet) {
    this._oRDERSSet = _oRDERSSet;
  }
  
  public void addToORDERSSet (Object obj) {
    if (null == this._oRDERSSet) this._oRDERSSet = new

java.util.HashSet();
    this._oRDERSSet.add(obj);
  }

  public boolean equals (Object obj) {
    if (null == obj) return false;
    if (!(obj instanceof

com.jacky.hibernate.domainmodel.base.BaseCustomer)) return false;
    else {
      com.jacky.hibernate.domainmodel.base.BaseCustomer

mObj = (com.jacky.hibernate.domainmodel.base.BaseCustomer) obj;
      if (null == this.getId() || null == mObj.getId())

return false;
      else return (this.getId().equals(mObj.getId()));
    }
  }

  public int hashCode () {
    if (Integer.MIN_VALUE == this.hashCode) {
      if (null == this.getId()) return super.hashCode();
      else {
        String hashStr = this.getClass().getName()

+ ":" + this.getId().hashCode();
        this.hashCode = hashStr.hashCode();
      }
    }
    return this.hashCode;
  }

  public String toString () {
    return super.toString();
  

}

}


6.Order.java:

package com.jacky.hibernate.domainmodel;

import com.jacky.hibernate.domainmodel.base.BaseOrders;

/**
* This is the object class that relates to the ORDERS table.
* Any customizations belong here.
*/
public class Order extends BaseOrders {
  private Customer customers;
  
/*[CONSTRUCTOR MARKER BEGIN]*/
  public Order () {
    super();
  }

  /**
   * Constructor for primary key
   */
  public Order (java.lang.Integer _id) {
    super(_id);
  }

  /**
   * Constructor for required fields
   */
  public Order (
    java.lang.Integer _id,
    com.jacky.hibernate.domainmodel.Customer _customer,
    java.lang.String _orderNumber) {

    super (
      _id,
      _customer,
      _orderNumber);
  }

/*[CONSTRUCTOR MARKER END]*/

}


7.BaseOrder.java:

package com.jacky.hibernate.domainmodel.base;

import java.io.Serializable;

/**
* This class has been automatically generated by Hibernate Synchronizer.


* For more information or documentation, visit The Hibernate Synchronizer

page
* at http://www.binamics.com/hibernatesync or contact Joe Hudson at

joe@binamics.com.
*
* This is an object that contains data related to

the ORDERS table.
* Do not modify this class because it will be overwritten if the

configuration file
* related to this class is modified.
*
* @hibernate.class
* table="ORDERS"
*/
public abstract class BaseOrder implements Serializable {

  public static String PROP_ORDER_NUMBER = "OrderNumber";
  public static String PROP_CUSTOMER = "Customer";
  public static String PROP_ID = "Id";

  private int hashCode = Integer.MIN_VALUE;

  // primary key
  private java.lang.Integer _id;

  // fields
  private java.lang.String _orderNumber;

  // many to one
  private com.jacky.hibernate.domainmodel.Customer _customer;

  // constructors
  public BaseOrder () {
    initialize();
  }

  /**
   * Constructor for primary key
   */
  public BaseOrder (java.lang.Integer _id) {
    this.setId(_id);
    initialize();
  }

  /**
   * Constructor for required fields
   */
  public BaseOrder (
    java.lang.Integer _id,
    com.jacky.hibernate.domainmodel.Customer _customer,
    java.lang.String _orderNumber) {

    this.setId(_id);
    this.setCustomer(_customer);
    this.setOrderNumber(_orderNumber);
    initialize();
  }

  protected void initialize () {}

  /**
   * Return the unique identifier of this class
* @hibernate.id
* generator-class="vm"
* column="ID"
*/
  public java.lang.Integer getId () {
    return _id;
  }

  /**
   * Set the unique identifier of this class
   * @param _id the new ID
   */
  public void setId (java.lang.Integer _id) {
    this._id = _id;
    this.hashCode = Integer.MIN_VALUE;
  }

  /**
   * Return the value associated with the column: ORDER_NUMBER
   */
  public java.lang.String getOrderNumber () {
    return _orderNumber;
  }

  /**
   * Set the value related to the column: ORDER_NUMBER
   * @param _orderNumber the ORDER_NUMBER value
   */
  public void setOrderNumber (java.lang.String _orderNumber) {
    this._orderNumber = _orderNumber;
  }

  /**
* @hibernate.property
* column=CUSTOMER_ID
   * not-null=true
   */
  public com.jacky.hibernate.domainmodel.Customer getCustomer () {
    return this._customer;
  }

  /**
   * Set the value related to the column: CUSTOMER_ID
   * @param _customer the CUSTOMER_ID value
   */
  public void setCustomer (com.jacky.hibernate.domainmodel.Customer

_customer) {
    this._customer = _customer;
  }

  public boolean equals (Object obj) {
    if (null == obj) return false;
    if (!(obj instanceof

com.jacky.hibernate.domainmodel.base.BaseOrder)) return false;
    else {
      com.jacky.hibernate.domainmodel.base.BaseOrder

mObj = (com.jacky.hibernate.domainmodel.base.BaseOrder) obj;
      if (null == this.getId() || null == mObj.getId())

return false;
      else return (this.getId().equals(mObj.getId()));
    }
  }

  public int hashCode () {
    if (Integer.MIN_VALUE == this.hashCode) {
      if (null == this.getId()) return super.hashCode();
      else {
        String hashStr = this.getClass().getName()

+ ":" + this.getId().hashCode();
        this.hashCode = hashStr.hashCode();
      }
    }
    return this.hashCode;
  }

  public String toString () {
    return super.toString();
  

}

}


8.IDAO.java:

/*
* 创建日期:2005-8-19
*
* 作者:<a href="mailto:jacky.jar@gmail.com">贾恺</a>
*/
package com.jacky.hibernate.domainmodel.dao;

/**
* @author 贾恺
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public interface IDAO {

}


9.ICustomerDao.java:

/*
* 创建日期:2005-8-19
*
* 作者:<a href="mailto:jacky.jar@gmail.com">贾恺</a>
*/
package com.jacky.hibernate.domainmodel.dao;

import java.util.List;

import com.jacky.hibernate.domainmodel.Customer;
/**
* @author 贾恺
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public interface ICustomerDao extends IDAO {
  public List getCustomer();
  public Customer getCustomer(Integer id);
  public List getCustomer(String name);
  public void saveCustomer(Customer customer);
  public void removeCustomer(String name);

}


10.CustomerHibernateDao.java:

/*
* 创建日期:2005-8-19
*
* 作者:<a href="mailto:jacky.jar@gmail.com">贾恺</a>
*/
package com.jacky.hibernate.domainmodel.dao;

import java.util.List;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.jacky.hibernate.domainmodel.Customer;

/**
* @author 贾恺
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

//import java.util.List;

import org.springframework.orm.ObjectRetrievalFailureException;
//import org.springframework.orm.hibernate.support.HibernateDaoSupport;

//import com.jacky.hibernate.domainmodel.Customer;
import com.jacky.hibernate.domainmodel.dao.ICustomerDao;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class CustomerHibernateDao extends HibernateDaoSupport implements
    ICustomerDao {
  private Log log = LogFactory.getLog(CustomerHibernateDao.class);

  /* (非 Javadoc)
   * @see

com.jacky.hibernate.domainmodel.dao.ICustomerDao#getCustomer()
   */
  public List getCustomer() {
    // TODO 自动生成方法存根
    return getHibernateTemplate().find("from Customer");
  }

  /* (非 Javadoc)
   * @see

com.jacky.hibernate.domainmodel.dao.ICustomerDao#getCustomer

(java.lang.String)
   */
  public Customer getCustomer(Integer id) {
    // TODO 自动生成方法存根
    //log.info("***="+name);
    Customer Customer = (Customer)getHibernateTemplate().load

(Customer.class,id);
    System.out.println(Customer.getId());
    if (Customer == null) {
    throw new ObjectRetrievalFailureException(Customer.class,

id);
    }
    return Customer;
  }

  public List getCustomer(String name){
    List list = getHibernateTemplate().find("from Customer

where NAME=?",name);
    if (list == null) {
      throw new ObjectRetrievalFailureException

(Customer.class, name);
      }
    return list;
  }
  /* (非 Javadoc)
   * @see com.jacky.hibernate.domainmodel.dao.ICustomerDao#saveUser

(com.jacky.hibernate.domainmodel.Customer)
   */
  public void saveCustomer(Customer Customer) {
    // TODO 自动生成方法存根
    log.info("saveCustomer(Customer Customer)");
    getHibernateTemplate().saveOrUpdate(Customer);
    if (log.isDebugEnabled()) {

      log.debug("Customername set to " +

Customer.getName());

      }
  }

  /* (非 Javadoc)
   * @see

com.jacky.hibernate.domainmodel.dao.ICustomerDao#removeUser

(java.lang.String)
   */
  public void removeCustomer(String name) {
    // TODO 自动生成方法存根
    Object Customer = getHibernateTemplate().load

(Customer.class, name);

    getHibernateTemplate().delete(Customer);

    if (log.isDebugEnabled()) {

    log.debug("del Customer " + name);

    }

  }

}


11.ICustomerManager.java:

/*
* 创建日期:2005-8-19
*
* 作者:<a href="mailto:jacky.jar@gmail.com">贾恺</a>
*/
package com.jacky.hibernate.service;

/**
* @author 贾恺
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

import java.util.List;

import com.jacky.hibernate.domainmodel.Customer;
import com.jacky.hibernate.domainmodel.dao.ICustomerDao;

public interface ICustomerManager {
  public void setCustomerDAO(ICustomerDao dao);
  public Customer getCustomer(Integer id);
  public List getCustomer();
  public List getCustomer(String name);
  public Customer saveCustomer(Customer customer);
  public void removeCustomer(String name);
}


12.CustomerManagerImpl.java:

/*
* 创建日期:2005-8-19
*
* 作者:<a href="mailto:jacky.jar@gmail.com">贾恺</a>
*/
package com.jacky.hibernate.service;

import java.util.List;

import com.jacky.hibernate.domainmodel.Customer;
import com.jacky.hibernate.domainmodel.dao.ICustomerDao;
//import com.jacky.hibernate.domainmodel.dao.CustomersDAO;
/**
* @author 贾恺
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

//import java.util.List;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class CustomerManagerImpl implements ICustomerManager {
  private static Log log = LogFactory.getLog

(CustomerManagerImpl.class);

  public ICustomerDao customerDao;
  /* (非 Javadoc)
   * @see com.jacky.hibernate.service.ICustomersManager#setUserDAO

(com.jacky.hibernate.domainmodel.dao.ICustomersDao)
   */
  public void setCustomerDAO(ICustomerDao dao) {
    // TODO 自动生成方法存根
    this.customerDao = dao;
  }

  public ICustomerDao getCustomersDao(){
    return customerDao;
  }
  
  public void setCustomerDao(ICustomerDao customerDao){
    this.customerDao = customerDao;
  }
  /* (非 Javadoc)
   * @see

com.jacky.hibernate.service.ICustomersManager#getCustomers

(java.lang.String)
   */
  public Customer getCustomer(Integer id) {
    // TODO 自动生成方法存根
    return customerDao.getCustomer(id);

  }

  /* (非 Javadoc)
   * @see

com.jacky.hibernate.service.ICustomersManager#getCustomers()
   */
  public List getCustomer() {
    // TODO 自动生成方法存根
    return customerDao.getCustomer();
  }

  public List getCustomer(String name){
    return customerDao.getCustomer(name);
  }
  /* (非 Javadoc)
   * @see com.jacky.hibernate.service.ICustomersManager#saveUser

(com.jacky.hibernate.domainmodel.Customers)
   */
  public Customer saveCustomer(Customer customer) {
    // TODO 自动生成方法存根
    customerDao.saveCustomer(customer);
    return customer;
  }

  /* (非 Javadoc)
   * @see com.jacky.hibernate.service.ICustomersManager#removeUser

(java.lang.String)
   */
  public void removeCustomer(String name) {
    // TODO 自动生成方法存根
    customerDao.removeCustomer(name);
  }

}


13.CustomersManagerImplTest.java:

/*
* 创建日期:2005-8-19
*
* 作者:<a href="mailto:jacky.jar@gmail.com">贾恺</a>
*/
package com.jacky.test;

/**
* @author 贾恺
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jacky.hibernate.domainmodel.Customer;
import com.jacky.hibernate.domainmodel.Order;
//import com.jacky.hibernate.service.CustomersManagerImpl;
import com.jacky.hibernate.service.ICustomerManager;

public class CustomersManagerImplTest {
  private static final Log log = LogFactory.getLog

(CustomersManagerImplTest.class);
  private ApplicationContext ac;
  private static ICustomerManager cm;
  public CustomersManagerImplTest(){
     ac = new ClassPathXmlApplicationContext

("hibernate_context.xml");
     cm = (ICustomerManager)ac.getBean

("customerManagerTarget");
    log.info("welcome!");
  }
  public static void main(String[] args){
    new CustomersManagerImplTest();
    test(cm);
  }
  
  private static void test(ICustomerManager cm){
    //getCustomer(cm);
    getCustomer(new Integer(1));
    //getCustomer("Tom");
  }
  private static void getCustomer(ICustomerManager cm){
    Iterator it = cm.getCustomer().iterator();
    while(it.hasNext()){
      log.info("name="+((Customer)it.next()).getName());
    }
  }
  private static void getCustomer(Integer id){
    Customer customer = cm.getCustomer(id);
    Set set = customer.getORDERSSet();
    log.info("name="+customer.getName());
    Iterator it = set.iterator();
    while(it.hasNext()){
      log.info(customer.getName()+":"+((Order)it.next

()).getOrderNumber());
    }
    
  }
  private static void getCustomer(String name){
    Iterator it = cm.getCustomer(name).iterator();
    while(it.hasNext())log.info("id="+((Customer)it.next

()).getId());
  }
}


14.build.xml:

<?xml version="1.0"?>
<project name="Learning Hibernate" default="prepare" basedir=".">

<!-- Set up properties containing important project directories -->
<property name="source.root" value="src"/>
<property name="class.root" value="WEB-INF/classes"/>
<property name="lib.dir" value="WEB-INF/lib"/>

<!-- Set up the class path for compilation and execution -->
<path id="project.class.path">
<!-- Include our own classes, of course -->
<pathelement location="${class.root}" />
<!-- Include jars in the project library directory -->
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>


<!-- Create our runtime subdirectories and copy resources into them -->
<target name="prepare" description="Sets up build structures">
<delete dir="${class.root}"/>
<mkdir dir="${class.root}"/>

<!-- Copy our property files and O/R mappings for use at runtime -->
<copy todir="${class.root}" >
<fileset dir="${source.root}" >
<include name="**/*.properties"/>
<include name="**/*.hbm.xml"/>
<include name="**/*.xml"/>
<include name="**/*.gif"/>
  <include name="**/*.hbm"/>
</fileset>
</copy>
  
  <delete dir="${lib.dir}"/>
  <mkdir dir="${lib.dir}"/>
  <!-- Copy our property files and O/R mappings for use at runtime

-->
  <copy todir="${basedir}/WEB-INF" >
     <fileset dir="${basedir}" >
   <include name="lib/*.jar"/>
   <include name="lib/*.tdt"/>
   <include name="lib/*.xml"/>
  
   </fileset>
  </copy>
</target>

<!-- Compile the java source of the project -->
<target name="compile" depends="prepare"
description="Compiles all Java classes">
<javac srcdir="${source.root}"
destdir="${class.root}"
debug="on"
optimize="off"
deprecation="on">
<classpath refid="project.class.path"/>
</javac>
</target>

<target name="run" description="Run a Hibernate+spring sample"
depends="compile">
<java classname="com.jacky.test.CustomersManagerImplTest" fork="true">
<arg value="NativeTester" />
<classpath refid="project.class.path"/>
</java>
</target>

</project>


以上就是所有程序的代码了,简单搭建了spring+hibernate的环境,下一步打算把

struts也加入其中,搭建起一个struts+spring+hibernate的J2EE框架!




话题树型展开
人气 标题 作者 字数 发贴时间
6638 spring+hibernate例子! e_water_boy 26095 2005-08-22 09:28
5751 Re:spring+hibernate例子! e_water_boy 9 2005-08-31 14:06

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923