边做边学小型封装--利用主流框架进行Dao层通用化,Spring3.0+Hibernate3.3.2通用Dao层整合(二)

     之前介绍了Spring3.0包是按功能进行一个一个进行分类的,并且把需要的依赖包都去掉了,只留下了实现功能的包,那么对于使用Spring2.5.6的人来讲有点不习惯,但是这些都是小问题,下面我将会从最基本的Spring+Hibernate的整合慢慢转变成通用的Dao层, ,从Hibernate官网上下载hibernate-distribution-3.3.2.GA-dist.zip,由于我采用的是JPA注解的形式直接对JavaBean进行表的关系跟定义,所以还需要下载hibernate-entitymanager-3.4.0.GA.zip,hibernate-annotations-3.4.0.GA ,然后到Spring官网网站下载spring-framework-3.0.0.RELEASE-with-docs.zip.

待下载完成后首先创建一个Project,我这里用的是Eclipse 3.5版本,创建的是Dynamic Web Project为了以后方便整合Struts2使用, 把从Hibernate官网中下载的Jar包分别解压到各个自己的目录后,分别在Hibernate3.3.2Ga中找到Hibernate3.jar,\lib\required\下的所有jar包,\lib\optional\ehcache下的ehcache-1.2.3.jar(用于Hibernate的2级缓存),在\lib\bytecode\cglib加入cglib-2.2(Hibernate中代理用的,实际和用途不是很清楚),在Hibernate-Annotation-3.4.0ga下加入让Hibernate支持JPA的核心包hibernate-annotations.jar、\lib下的ejb3-persistence.jar、hibernate-commons-annotations.jar,最后在hibernate-entitymanager-3.4.0.GA中加入hibernate-entitymanager.jar 分别放/WebContent/WEB-INF/lib 目录下,经测试之后发现Hibernate会报错Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder 或者Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/LogManager的错误。因为Hibernate3.3提供的jar包,缺少了slf4j-nop-1.5.8.jar这个包,解决方法可以到百度等其他地方下载或者从hibernate-annotations-3.4.0.GA 中的\lib\test加入slf4j-log4j12.jar和slf4j-api.jar,加入slf4j-log4j12.jar后如果没有加入log4j的话也会java.lang.NoClassDefFoundError: org/apache/log4j/LogManager的错误所以还需要加入log4j的jar包,当所有Hibernate需要的jar包加入后就正式开始我们的第一步:实体bean User:代码如下:

package org.lxh.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tb_user")
public class User implements Serializable{
	private Integer id ;
	private String username;
	private String password;
	/**  
	 * id  
	 *  
	 * @return  the id  
	 * @since   CodingExample Ver(编码范例查看) 1.0  
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	/**  
	 * @param id the id to set  
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	/**  
	 * username  
	 *  
	 * @return  the username  
	 * @since   CodingExample Ver(编码范例查看) 1.0  
	 */
	@Column(length=18,unique=true)
	public String getUsername() {
		return username;
	}
	/**  
	 * @param username the username to set  
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**  
	 * password  
	 *  
	 * @return  the password  
	 * @since   CodingExample Ver(编码范例查看) 1.0  
	 */
	@Column(length=18)
	public String getPassword() {
		return password;
	}
	/**  
	 * @param password the password to set  
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
}
创建完实体Bean后,使用Spring的aop模式注入Hibernate的SessionFactory和事务,这样可以不用再另外创建一个Hibernate.cfg.xml文件独立配置
Hibernate和创建一个SessionFactory工具类,直接让Spring进行对Hibernate的事务跟SessionFactory管理,在src目录下创建一个XML文件名字自定义
我这里把Spring的配置文件叫做applicationContext.xml,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/datatest">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="packagesToScan">
			<list>
				<value>org.lxh.bean</value>
			</list>
		</property>
	</bean>
</beans>
可以发现上面的XML配置中是有个名为DataSource的bean,因为Spring需要连接池来管理数据库,所以我们需要从Apache的官网上下载对应的Jar包分别是
Commons-dbcpCommons-pool,commons-pool-1.5.4.jarcommons-dbcp-1.2.2.jar放入/WebContent/WEB-INF/lib下,这样就不会报错找不到
该类了.这样我们的第一步配置Hibernate到Spring就基本完成
posted @ 2010-02-13 14:48  EdwardLau  阅读(2028)  评论(0编辑  收藏  举报