ssh项目中经常出现的问题

来源:互联网 发布:仿苹果6splus淘宝店 编辑:程序博客网 时间:2024/05/18 02:08

在进行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 stubreturn null;}public List<Category> query() {// TODO Auto-generated method stubreturn null;} */}
BaseSweviceImpl.java

public class BaseServiceImpl<T> implements BaseService<T> {private Class clazz;   //存储了当前操作的类型,即泛型Tprivate 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 stubreturn (T) getSession().get(clazz,id);}public List<T> query() {String hql = "from " + clazz.getSimpleName();System.out.println(hql+"hql");// TODO Auto-generated method stubreturn 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" />


原创粉丝点击