首先晒晒我的项目包的层次结构:
–src—–com.dao.inter 接口包—–com.dao.base 父类包,实现接口—–com.dao 子类包,基础父类—–com.service.inter 接口包—–com.service.base 父类包,香港服务器租用,实现接口—–com.service子类包,网站空间,基础父类—–com.service.action父类包,实现接口—–com.action子类包,基础父类
接口,如下:
public interface IDAO { public List findByHql(String hql);}
public class BaseDao extends HibernateDaoSupport implements IDAO { public List findByHql(String hql) { return this.getHibernateTemplate().find(hql);}}
,如下:
public class UserDao extends BaseDao{
public User loginValidation(String loginName, String loginPassword) { return null; }
}
接口,如下:
public interface IService { public List findByHql(String hql);}
,服务器空间,如下:
public class BaseService implements IService { private IDAO dao;public IDAO getDao() { return dao;}public void setDao(IDAO dao) { this.dao = dao;}
public List findByHql(String hql) {return this.getDao().findByHql(hql);}}
,如下:
public class UserService extends BaseService { public User loginValidation(String loginName, String loginPassword) { return ((UserDao)this.getDao()).loginValidation(loginName, loginPassword);}}
配置文件中实现对象注入,如下:
<!– 注入sessionFactory到UserDao类 –><bean> <property ref=”sessionFactory”></property>
</bean><!– 注入UserDao到UserService类 –>
<bean>
<property ref=”userDao”></property>
</bean><!– 注入UserService到UserAction类 –>
<bean>
<property ref=”userService”></property>
</bean>
并在Spring中进行了事务配置,如下:
<!– 配置事务操作 –>
<bean>
<property ref=”sessionFactory” />
</bean>
<tx:advice transactionmanager=”transactionManager”>
<tx:attributes>
<tx:method propagation=”REQUIRED” />
<tx:method propagation=”REQUIRED” />
<tx:method propagation=”REQUIRED” />
<tx:method propagation=”REQUIRED” />
<tx:method read-only=”false” propagation=”SUPPORTS” />
<tx:method read-only=”false” propagation=”SUPPORTS” />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression=”execution(* com.dao.*.*(..))” />
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”point” />
</aop:config>
不要等待机会,而要创造机会。