当前位置: 代码迷 >> Java Web开发 >> 刚刚入门SpringMVC 有个地方不明白
  详细解决方案

刚刚入门SpringMVC 有个地方不明白

热度:202   发布时间:2016-04-16 21:45:45.0
刚入门SpringMVC 有个地方不明白

package com.sxt.dao;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sxt.po.User;

@Component
public class UserDao {
@Resource
private HibernateTemplate hibernateTemplate;

public void add(User u){
System.out.println("UserDao.add()");
hibernateTemplate.save(u);
}

public void delete(User u){
System.out.println("UserDao.delete()");
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery("delete from user where uname=?");
query.setString(0, u.getUname());
query.executeUpdate();
}

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

}


我用的是注解的方式
执行add的时候可以通过。

执行delete的时候我就是想自己写个hql 然后就报错了


exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:583)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)




不知道是什么情况  好像是Session 的问题  
这个怎么办?
------解决思路----------------------
没有配置事务,你这里没错,dao没错,是在引用dao的地方错了,在上一层方法上加上注解事务就对了
------解决思路----------------------
一种是没有配置事务

还有一种 是自己当初刚学的时候犯得错误

就是
srpingmvc 把Service层的类 也给扫描了,然后注册到容器
但是
在srping.xml里面配置的事务,是没法用到springmvc扫描的bena里,也会报这种错误,为自己的不熟悉,付出了一下午的debug
------解决思路----------------------
解决了就好 。哈哈


------解决思路----------------------


<aop:config> 
    <aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 

是做什么用的?

答:是spring提供的一套AOP模板,用来注入事务
为什么com.sxt.service.*.*
我改成com.sxt.dao.*.*
或改成com.*.*.*.*
程序一样正常?
答:com.sxt.service.*.*
是将事务注入到Service 的方法里,项目里一般这样配置,因为一个业务逻辑往往需要调用多个dao,为了保证数据的一致性,必须在一个事务里完成
com.sxt.dao.*.**
是将事务注入到dao 的方法里, 如果service调用多个dao,不在同一个事务,是不符合业务逻辑的
com.*.*.*.*
注入到com下


------解决思路----------------------
引用:
Quote: 引用:

一种是没有配置事务

还有一种 是自己当初刚学的时候犯得错误

就是
srpingmvc 把Service层的类 也给扫描了,然后注册到容器
但是
在srping.xml里面配置的事务,是没法用到springmvc扫描的bena里,也会报这种错误,为自己的不熟悉,付出了一下午的debug


引用:
解决了就好 。哈哈


我还是有问题


<aop:config> 
    <aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 

是做什么用的?
为什么com.sxt.service.*.*
我改成com.sxt.dao.*.*
或改成com.*.*.*.*
程序一样正常?

这个很简单,com.sxt.service.*.*可以操作多个表,com.sxt.dao.*.*一般操作一张表,例如汇款,如果配置后面那种,收款方账户不存在,但钱已经扣除了,那钱就回不来了,因为不会回滚操作成功的那张表
  相关解决方案