ssh项目中经常出现的问题

本文总结了SSH项目开发中常见的两个问题:一是Service层公共方法抽取导致的事务管理问题;二是Action层抽取过程中遇到的NoSuchMethodException异常及其解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在进行ssh项目的开发过程中,往往会出现很多问题,我就把我所遇到的问题一一罗列在此,供大家参考


1、在对service层进行公共方法抽取的过程中,将CategoryService中的所有方法抽取到BaseService中,此时也应该将BaseService进行事务管理,否则就会获取不到session。

CategoryServiceImpl.java

public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService{

	
	/*
	 * (non-Javadoc)
	 * @see com.shop.base.BaseServiceImpl#save(java.lang.Object)
	 * 
	 * 只需实现CategoryService接口中新增的方法即可,公共方法已经在BaseServiceImpl中实现了
	 * 
	 public void save(Category category) {
		Session session = HibernateSessionFactory.getSession();
		try {
			//手动事务
			session.getTransaction().begin();
			//执行业务逻辑
			session.save(category);
			//事务提交
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			throw new RuntimeException(e);
			// TODO: handle exception
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
	
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	protected Session getSession(){
		return sessionFactory.getCurrentSession();
	}

	public void update(Category category) {
		getSession().update(category);
		
	}

	public void delete(Category category) {
		// TODO Auto-generated method stub
		
	}

	public Category get(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Category> query() {
		// TODO Auto-generated method stub
		return null;
	}
	 */
	
}
BaseSweviceImpl.java

public class BaseServiceImpl<T> implements BaseService<T> {
	
	private Class clazz;   //存储了当前操作的类型,即泛型T
	
	private SessionFactory sessionFactory;
	
	public BaseServiceImpl(){
		System.out.println("this代表的是当前调用构造方法的对象" + this);
		System.out.println("获取当前this对象的父类信息" + this.getClass().getSuperclass());
		System.out.println("获取当前对象的父类信心(包括泛型信息)" + this.getClass().getGenericSuperclass());
		
		//拿到泛型参数类型
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		clazz = (Class)type.getActualTypeArguments()[0];
	}
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	
	public SessionFactory getSessionFactory() {
		
		return sessionFactory;
	}
	
	protected Session getSession(){
		System.out.println("为啥得不到session?因为抽取出来的类也要加上事务管理");
		System.out.println(sessionFactory);
		return sessionFactory.getCurrentSession();
		//return sessionFactory.getCurrentSession();
	}
	
	public void save(T t) {
		getSession().save(t);
		// TODO Auto-generated method stub
		
	}

	public void update(T t) {
		getSession().update(t);
		// TODO Auto-generated method stub
		
	}

	public void delete(int id) {
		System.out.println(clazz.getSimpleName());
		String hql = "delete" + clazz.getSimpleName() + "as c where c.id=:id";
		getSession().createQuery(hql).setInteger("id", id).executeUpdate();
		// TODO Auto-generated method stub
		
	}

	public T get(int id) {
		// TODO Auto-generated method stub
		return (T) getSession().get(clazz,id);
	}
	
	
	public List<T> query() {
		String hql = "from " + clazz.getSimpleName();
		System.out.println(hql+"hql");
		// TODO Auto-generated method stub
		return getSession().createQuery(hql).list();
	}

}

spring的事务管理

<aop:config>
       		<!--被切入事务的包和类  -->  
       		<aop:pointcut expression="execution(* com.shop.serviceimpl.*.*(..))" id="pointcut" />
       		 <!--切入的事务   -->
       		<aop:advisor advice-ref="advice" pointcut-ref="pointcut" />
       </aop:config>
       <!--当将CategoryServiceImpl的公共方法抽取出来后,抽取出来的基础类也要进行事务管理,否则就会报错  -->
       <aop:config>
       		<aop:pointcut expression="execution(* com.shop.base.*.*(..))" id="pointcut1" />
       		<aop:advisor advice-ref="advice" pointcut-ref="pointcut1" />
       </aop:config>
       
      
       
       
       <!-- 配置categoryservice 抽取后被干掉
        <bean id="categoryService" class="com.shop.serviceimpl.CategoryServiceImpl">
       		<property name="sessionFactory" ref="sessionFactory" />
       </bean>
        -->
      
       
       <!-- 配置抽取出来的baseService 泛型类是不能实例化的,所以加lazy-init属性-->
       <bean id="baseService" class="com.shop.base.BaseServiceImpl" lazy-init="true">
       		<property name="sessionFactory" ref="sessionFactory"/>
       </bean>
       <!-- 配置抽取公共方法的categoryService 将poperty干掉,指明继承baseService -->
       <bean id="categoryService" class="com.shop.serviceimpl.CategoryServiceImpl" parent="baseService" />

2、下面是抽取action过程中遇到的异常
java.lang.NoSuchMethodException: com.sun.proxy.$Proxy18.query()
遇到这个异常通常加下面一行配置就好了
<aop:aspectj-autoproxy proxy-target-class="true" />


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值