感觉使用spring注解相当方便,就对原来的项目做了下改变,主要是对spring配置文件的改变
1: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: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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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"> <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="defaultAutoCommit" value="false" /> <property name="maxActive" value="150" /> <property name="maxIdle" value="100" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="5" /> <property name="testOnBorrow" value="true"/> <property name="testWhileIdle" value="true"/> <property name="validationQuery" value="select 1 from dual" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> --> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.query.factory_class"> org.hibernate.hql.classic.ClassicQueryTranslatorFactory </prop> </props> </property> <property name="mappingResources"> <list> <value>com/yeshun/bean/User.hbm.xml</value> </list> </property> </bean> <!--jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!--transactionManager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置hibernateTemplate模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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"> <!-- Hibernate configuration --> <import resource="classpath:applicationContext-hibernate.xml" /> <!-- 采用注释的方式配置bean --> <context:annotation-config /> <context:component-scan base-package="com.yeshun" /> <!-- 采用注释的方式配置 aop --> <aop:aspectj-autoproxy /> <!-- 采用annotation的方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!--采用配置文件的方式配置事务 --> <!-- 配置一个事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.yeshun.service.impl.*Impl.*(..))" id="txManager" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager" /> </aop:config> <!-- 配置事务的具体使用方式 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <!-- 其他方法使用默认的事务设置 --> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
package com.yeshun.dao.impl; import java.io.Serializable; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.yeshun.dao.BaseDao; /** * @author yeshun */ public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao { protected @Resource(name = "jdbcTemplate") JdbcTemplate jdbcTemplate; @Resource(name = "hibernateTemplate") public void setSupperHibernateTemplate(HibernateTemplate hibernateTemplate) { super.setHibernateTemplate(hibernateTemplate); } public Object getObject(Class clazz, Serializable id) { return getHibernateTemplate().load(clazz.getName(), id); } public Object saveObject(Object obj) throws RuntimeException, SQLException{ return getHibernateTemplate().save(obj); } public void updateObject(Object obj) throws RuntimeException, SQLException{ getHibernateTemplate().update(obj); } public void saveOrUpdate(Object obj) throws RuntimeException, SQLException{ getHibernateTemplate().saveOrUpdate(obj); } public void deleteObject(Object obj) throws RuntimeException, SQLException{ this.getHibernateTemplate().delete(obj); } public Object removeObject(Class clazz, Serializable id) throws RuntimeException, SQLException{ // TODO Auto-generated method stub Object obj = this.getObject(clazz, id); if(obj!=null)deleteObject(obj); return obj; } public java.util.List getAll(Class clazz) { List list=getHibernateTemplate().loadAll(clazz); return list; } }
package com.yeshun.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.yeshun.bean.User; import com.yeshun.dao.UserDao; import com.yeshun.service.UserService; @Transactional @Service("UserServiceImpl") public class UserServiceImpl implements UserService{ private @Resource(name = "UserDaoImpl") UserDao userDao; public List<User> findAllUser() { // TODO Auto-generated method stub return userDao.findAllUser(); } }
也附上源码的下载地址点击打开链接