Hibernate3与spring的整合应用作者:TOM 来源:技术学习者 更新日期:2005-09-15
Spring为应用程序提供一个容器, 为应用程序的管理带来了方便. 它与hibernate的结合, 形成一个完整的后台体系,
也是当今应用开发流行的做法. 奋斗了一个晚上, 终于把hibernate3与spring整合了起来,
hibernate2.x和hibernate3与spring的结合稍有不同, 关键是引入的spring的包的不同,
下面我会标识出来.
Spring 的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "spring" "../../../lib/spring-beans.dtd" >
<beans default-autowire="no" default-dependency-check="none" default-lazy-init="false">

<!--
配置数据源
注意: 用org.apache.commons.dbcp.BasicDataSource, 要引入 apache commons
的commons-collections-3.1.jar, commons-dbcp-1.2.1.jar, commons-pool-1.2.jar三个包
-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/sparkcrm</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1111</value>
</property>
</bean>

<!-- 配置sessionFactory, 注意这里引入的包的不同 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/sparkcrm/schema/entities/Lead.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置transactionManager, 注意这里引入的包的不同 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<--事务代理在这里配置, 这里省略了 -->

<bean id="leadDAO" class="com.sparkcrm.schema.dao.LeadDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

</beans>



一个示例的hibernate的映射文件
<?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">
<hibernate-mapping package="com.sparkcrm.schema.entities"> <!-- 我在这里用了hibernate的动态模型(dynamic
models) , 没用pojo-->
<class entity-name="Lead" table="Lead">
<id name="id" column="id" type="string">
<generator class="uuid.hex"/>
</id>
<property name="companyName" type="string"/>
<property name="topic" type="string"/>
<property name="contactName" type="string"/>
</class>
</hibernate-mapping>

DAO代码:
import java.util.Map;
/** * DAO接口 */
 public interface IDAO {

String create(Map<String, Object> map);
void update(Map<String, Object> map);
Map<String, Object> delete(String id);
boolean share(String id, String userId, int rights);
boolean assign(String id, String userId);
}


import java.util.Map;

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

import com.sparkcrm.schema.IDAO;
import com.sparkcrm.schema.Schema;
import com.sparkcrm.schema.metadata.Lead;
/** *一个示例的DAO实现, 继承HibernateDaoSupport,
用spring带来的管理session等的便利 */
 public class LeadDao extends HibernateDaoSupport implements IDAO {

 public String create(Map<String, Object> map) {
getHibernateTemplate().saveOrUpdate(Schema.LEAD, map);
return (String) map.get(Lead.ID);
}

 public void update(Map<String, Object> map) {
}

 public Map<String, Object> delete(String id) {
return null;
}

 public boolean share(String id, String userId, int rights) {
return false;
}

 public boolean assign(String id, String userId) {
return false;
}

}
示意性的测试代码:
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sparkcrm.schema.IDAO;

 public class testLeadDAO extends TestCase {
ClassPathXmlApplicationContext ctx = null;
 public void setUp() {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
 public void testCreateLead() {
IDAO leadDao = (IDAO) ctx.getBean("leadDAO");

Map<String, Object> map = new HashMap<String, Object>();
map.put("companyName", "Spark Ltd.");
map.put("topic", "This is a Good Lead!");
map.put("contactName", "abcd");
String id = leadDao.create(map);
System.out.println(id);
}
}

|