需要导入的jar包:(将这些jar包导入lib里面去)
web.xml配置文件
applicationContext-config.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"
xmlns:tool="http://www.springframework.org/schema/tool"xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tool
http://www.springframework.org/schema/tool/spring-tool.xsd"
default-lazy-init="true"default->
<context:component-scanbase-package="cn.cdi.test">
<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置DBCP数据源-->
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName" value="com.mysql.jdbc.Driver"/>
<propertyname="url"
value="jdbc:mysql://192.168.6.171:3306/importtask"/>
<propertyname="username" value="root" />
<propertyname="password" value="111111" />
</bean>
<!--配置SessionFactory,由Spring容器来管理Hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<propertyname="dataSource" ref="dataSource" />
</bean>
<bean id="txManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>
<!-- 需要引入tx的命名空间-->
<!-- 这是事务通知操作(传播特性),使用的事务管理器引用自transactionManager -->
<tx:advice id="txAdvice"transaction-manager="txManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法-->
<tx:method name="save*"propagation="REQUIRED"rollback-for="OperationException"/> <!-- 根据每个(类)方法配置事务的传播特性 -->
<tx:method name="modify*"propagation="REQUIRED"rollback-for="OperationException"/>
<tx:methodname="delete*" propagation="REQUIRED"rollback-for="OperationException"/>
<tx:methodname="get*" propagation="REQUIRED"read-only="true"/>
<tx:method name="query*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 需要引入aop的命名空间(配置事物的切入点)-->
<aop:configproxy-target-class="true">
<!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作-->
<aop:pointcut id="serviceMethods"expression="execution(* cn.cdi.*.business.*Business.*(..))"/>
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice-->
<aop:advisor advice-ref="txAdvice"pointcut-ref="serviceMethods"/>
</aop:config>
</beans>
applicationContext-hibernate.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<propertyname="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
</beans>
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- beans 的配置文件-->
<importresource="applicationContext-beans.xml"/>
<importresource="applicationContext-config.xml"/>
<importresource="applicationContext-hibernate.xml"/>
</beans>
hibernate.cfg.xml配置文件配置如下
<?xml version='1.0'encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- hibernatedialect -->
<!-- JDBCconnection properties (begin) -->
<!-- MySQL-->
<propertyname="hibernate.connection.username">root</property>
<propertyname="hibernate.connection.password">111111</property>
<propertyname="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="hibernate.connection.url">jdbc:mysql://192.168.6.171:3306/importtask</property>
<propertyname="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<propertyname="hibernate.use_outer_join">true</property>
<propertyname="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<propertyname="hibernate.hbm2ddl.auto">update</property>
<propertyname="hibernate.show_sql">false</property>
<propertyname="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
<!--数据库连接池
<propertyname="c3p0.min_size">5</property>
<propertyname="c3p0.max_size">30</property>
<propertyname="c3p0.time_out">1800</property>
<propertyname="c3p0.max_statement">50</property>
-->
<mappingresource="hbm/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
applicationContext-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注入 -->
<bean id="userDAO"class="cn.cdi.util.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="testDao"class="cn.cdi.test.testdao.TestDao"parent="userDAO"/>
<bean id="testBusiness"class="cn.cdi.test.business.TestBusiness">
<propertyname="testDao" ref="testDao" />
</bean>
</beans>
取得sessionfactory对象的类
package cn.cdi.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class BasicHibernateDao {
private SessionFactorysessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public voidsetSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory =sessionFactory;
}
public Session getSession()
{
returnsessionFactory.getCurrentSession();
}