Hibernate是一款ORM关系映射框架+SPRing是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升。
整合开发步骤如下:
第一步:导入架包:
1、Hibernate基础包+Spring基础包(AOP代理包和cglib...)
第二步:在spring配置文件中配置datasource(数据库连接信息要么写在hibernate.cfg.xml中;要么写在datasource中)
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!--设置类扫描器;自动装配Bean--><context:component-scanbase-package="com.msit.ssh.sh"/><!--引入外部属性文件--><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="location"value="classpath:jdbc.properties"></property></bean><!--配置数据源--><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><!--配置数据源信息--><propertyname="url"><value>${connection.url}</value></property><propertyname="driverClassName"><value>${connection.driver_class}</value></property><propertyname="username"><value>${connection.username}</value></property><propertyname="passWord"><value>${connection.password}</value></property><!--最大连接数--><propertyname="maxActive"><value>${jdbc.maxactive}</value></property><!--最大空闲数--><propertyname="maxIdle"><value>${jdbc.maxidle}</value></property><!--最小空闲数--><propertyname="minIdle"><value>${jdbc.minidle}</value></property></bean><!--sessionFactory(管理hibernate sessionfactory)--><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!--注入数据源--><propertyname="dataSource"ref="dataSource"/><!--第一种方式:引入hibernate.cfg.xml--><propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property><!--第二种方式:所有的hibernate配置配置在spring中--><!--配置映射文件--><!--<property name="mappingDirectoryLocations">
<list>
<value>com/msit/ssh/sh/entity/User.hbm.xml</value>
</list>
</property>--><!--配置其他选项--><!--<property name="hibernateProperties">
<props>
<prop key=""></prop>
<prop key="show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>--></bean><!--配置hibernate事务管理器--><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><!--注入sessionFactory--><propertyname="sessionFactory"ref="sessionFactory"/></bean><!--事务通知--><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><!--配置哪些方法需要用到事务;哪些方法不需要事务--><tx:methodname="*"/><tx:methodname="get*"propagation="NOT_SUPPORTED"/></tx:attributes></tx:advice><!--配置aop--><aop:config><!--配置切入点--><aop:pointcutid="txPointcut"expression="execution(* com.msit.ssh.sh.service.impl.*.*(..))"/><!--配置事务通知--><aop:advisoradvice-ref="txAdvice"pointcut-ref="txPointcut"/></aop:config><!--<bean class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property></bean>--><!--<bean id="hibernatedaosuppert"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl"
parent="hibernatedaosuppert">
</bean>--></beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!--数据库连接信息--><!--<property name="connection.url">jdbc:Oracle:thin:@localhost:1521:orcl</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">db2</property>
<property name="connection.password">db2</property>--><propertyname="hibernate.dialect">org.hibernate.dialect.OracleDialect</property><!--其他配置--><propertyname="show_sql">true</property><propertyname="hbm2ddl.auto">update</property><mappingresource="com/msit/ssh/sh/entity/User.hbm.xml"/><!--<class-cache
class="org.hibernate.test.legacy.Simple"
region="Simple"
usage="read-write"/>--></session-factory></hibernate-configuration>
3、配置sessionfactory(hibernate交给sprig管理)
里边注入数据源(datasource);再把hibernate配置文件引入进来
或者hibernate所有配置都写在sessionfactory中(舍弃了hibernate配置文件)<!--sessionFactory(管理hibernate sessionfactory)--><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!--注入数据源--><propertyname="dataSource"ref="dataSource"/><!--引入hibernate.cfg.xml--><propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property></bean>
4、dao层继承HibernateDaoSuppert(抽象类)(不能用注解);必须要标明abstract="true"
如果使用此方式:<bean id="hibernatedaosuppert"class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"abstract="true">//将sessionFactory注入给HibernateDaoSuppert<property name="sessionFactory" ref="sessionFactory"></property>
</bean>//dao层Bean必须配置parent="hibernatedaosuppert"<bean id="userdao"class="com.msit.ssh.sh.dao.impl.UserDaoImpl"parent="hibernatedaosuppert">
</bean>还想用注解怎么办?//编写一个超类继承HibernateDaoSuppertpublicclassBaseHibernateDaoSuppertextendsHibernateDaoSupport{
@Resource//注入sessionFactorypublicvoidsetMySessionFactory(SessionFactory sessionFactory){this.setSessionFactory(sessionFactory);
}
}//dao层就继承超类@Repository("userdao")publicclassUserDaoImplextendsBaseHibernateDaoSuppertimplementsIUserDao
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态。
如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦
如果您对文章内容有任何疑问, 可以通过评论或发邮件的方式联系我: 2276292708@QQ.com
如果需要转载,请注明出处,谢谢!!