Spring.xml 配置信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 启动注解 -->
<context:annotation-config />
<context:component-scan base-package="com" /> <!-- 自动扫描所有注解该路径 -->
<!-- 加载配置信息properties -->
<context:property-placeholder location="classpath:/jdbc.properties" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${jdbc.showsql}</prop>
<prop key="hibernate.format_sql">${jdbc.format.sql}</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl.auto}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.travelsky.**.model</value><!-- 扫描实体类,注解 -->
</list>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- AOP 对Dao添加事务 -->
<aop:config>
<aop:pointcut id="productServiceMethods" expression="execution(* com.travelsky.**.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置数据源 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="driverUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
<property name="minimumConnectionCount" value="${proxool.minConnCount}" />
<property name="statistics" value="${proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
<property name="trace" value="${proxool.trace}" />
<property name="verbose" value="${proxool.verbose}" />
</bean>
<!-- Dao模板,具体实现Dao继承此bean的配置,省去各个Dao的重复配置 -->
<bean id="daoTemplate" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 测试 bean start -->
<bean id="studentDao" class="com.travelsky.demo.dao.impl.StudentDaoImpl" parent="daoTemplate" />
<bean id="studentService" class="com.travelsky.demo.service.impl.StudentServiceImpl" >
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="loginAction" class="com.travelsky.demo.web.LoginAction" >
<property name="studentService" ref="studentService" />
</bean>
<!-- 测试 bean end -->
</beans>基类Dao
/**
* 保存
*/
public void save(T entity) {
System.out.println("GenericDao:" + entity);
// TODO Auto-generated method stub
// System.out.println(entity);
this.sessionFactory.getCurrentSession().save(entity);
}Hibernate4
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 启动注解 -->
<context:annotation-config />
<context:component-scan base-package="com" /> <!-- 自动扫描所有注解该路径 -->
<!-- 加载配置信息properties -->
<context:property-placeholder location="classpath:/jdbc.properties" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${jdbc.showsql}</prop>
<prop key="hibernate.format_sql">${jdbc.format.sql}</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl.auto}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.travelsky.**.model</value><!-- 扫描实体类,注解 -->
</list>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- AOP 对Dao添加事务 -->
<aop:config>
<aop:pointcut id="productServiceMethods" expression="execution(* com.travelsky.**.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置数据源 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="driverUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
<property name="minimumConnectionCount" value="${proxool.minConnCount}" />
<property name="statistics" value="${proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
<property name="trace" value="${proxool.trace}" />
<property name="verbose" value="${proxool.verbose}" />
</bean>
<!-- Dao模板,具体实现Dao继承此bean的配置,省去各个Dao的重复配置 -->
<bean id="daoTemplate" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 测试 bean start -->
<bean id="studentDao" class="com.travelsky.demo.dao.impl.StudentDaoImpl" parent="daoTemplate" />
<bean id="studentService" class="com.travelsky.demo.service.impl.StudentServiceImpl" >
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="loginAction" class="com.travelsky.demo.web.LoginAction" >
<property name="studentService" ref="studentService" />
</bean>
<!-- 测试 bean end -->
</beans>基类Dao
/**
* 保存
*/
public void save(T entity) {
System.out.println("GenericDao:" + entity);
// TODO Auto-generated method stub
// System.out.println(entity);
this.sessionFactory.getCurrentSession().save(entity);
}Hibernate4
还没解决的bug,尚无证据显示会影响运行
WARN: HHH000387: ResultSet's statement was not registered