开发者社区> 问答> 正文

springmvc(3.2)+hibernate(4.2)整合,报错:No Se?报错

网上百度了很多都无法解决。求助各位帮忙看看

错误代码:

org.hibernate.HibernateException: No Session found for current thread
	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:993)
	at com.miartech.moco.dao.impl.LoginDaoImpl.getSession(LoginDaoImpl.java:39)
	at com.miartech.moco.dao.impl.LoginDaoImpl.getLoginInfoByAccount(LoginDaoImpl.java:56)
	at com.miartech.moco.service.impl.LoginServiceImpl.getCheckLogin(LoginServiceImpl.java:48)
	at com.miartech.moco.controller.LoginController.login(LoginController.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
	at java.lang.Thread.run(Thread.java:619)



applicationContext.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  <!--加载 JDBC 配置文件 -->
	<context:property-placeholder order="1"
		location="classpath*:jdbc.properties" ignore-resource-not-found="true" />
	<context:property-placeholder order="2"
		location="classpath*:log4j.properties" ignore-resource-not-found="true" />

	<!-- c3p0 connection pool configuration -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 数据库驱动 -->
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<!-- 连接URL串 -->
		<property name="jdbcUrl" value="${jdbc.url}" />
		<!-- 连接用户名 -->
		<property name="user" value="${jdbc.username}" />
		<!-- 连接密码 -->
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接池时连接数量为5个 -->
		<property name="initialPoolSize" value="15" />
		<!-- 允许最小连接数量为5个 -->
		<property name="minPoolSize" value="50" />
		<!-- 允许最大连接数量为20个 -->
		<property name="maxPoolSize" value="100" />
		<!-- 允许连接池最大生成100个PreparedStatement对象 -->
		<property name="maxStatements" value="0" />
		<!-- 连接有效时间,连接超过3600秒未使用,则该连接丢弃 -->
		<property name="maxIdleTime" value="3600" />
		<!-- 连接用完时,一次产生的新连接步进值为5 -->
		<property name="acquireIncrement" value="5" />
		<!-- 获取连接失败后再尝试10次,再失败则返回DAOException异常 -->
		<property name="acquireRetryAttempts" value="10" />
		<!-- 获取下一次连接时最短间隔600毫秒,有助于提高性能 -->
		<property name="acquireRetryDelay" value="600" />
		<!-- 如果设为true那么在取得连接的同时将校验连接的有效性-->
		<property name="testConnectionOnCheckin" value="true" />
		<!-- 每个1200秒检查连接对象状态 -->
		<property name="idleConnectionTestPeriod" value="1200" />
		<!-- 获取新连接的超时时间为10000毫秒 -->
		<property name="checkoutTimeout" value="10000" />
		<!--
			定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:
			测试的表必须在初始数据源的时候就存在。Default: null
		-->
		<property name="preferredTestQuery"
			value="select t.f_userid from t_login t where t.f_userid=1"></property>
	</bean>

	<!-- 配置hibernate相关信息 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
				<!--<prop key="current_session_context_class">thread</prop>
			--></props>
		</property>

		<!-- 以下列表写入实体类 -->
		
			<property name="annotatedClasses"> 
			<list>
			<value>com.miartech.moco.entity.LoginInfo</value> 
			</list>
			</property>
		
		<!-- 自动扫描注解方式配置的hibernate类文件
		<property name="packagesToScan">
			<list>
				<value>com.miartech.moco.entity</value>
			</list>
		</property> -->
	</bean>
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 配置事务通知属性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 定义事务传播属性 -->
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="new*" propagation="REQUIRED" />
			<tx:method name="set*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="change*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="REQUIRED" read-only="true" />
			<tx:method name="find*" propagation="REQUIRED" read-only="true" />
			<tx:method name="load*" propagation="REQUIRED" read-only="true" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 配置事务切面 -->
	<aop:config proxy-target-class="true">
		<aop:pointcut id="serviceOperation" expression="execution(* com.miartech.moco.service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
	</aop:config>

	<!--
		<bean id="springUtils" class="com.miartech.moco.utils.SpringUtils"></bean> 
	-->
</beans>



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>Spring3MVC</display-name>
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:com/miartech/moco/config/spring/spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	<!-- Spring配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- openSessionInView配置 -->
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->
	<filter>
		<filter-name>springCharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>springCharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

spring-servlet.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!--<mvc:annotation-driven />  
       
     静态资源(js/image)的访问   
    <mvc:resources location="/js/" mapping="/js/**"/>  
    <mvc:resources location="/jsp/" mapping="/jsp/**"/>  
    <mvc:resources location="/error/" mapping="/error/**"/>  
	-->
	<context:component-scan base-package="com.miartech.moco" />
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>



dao层实现类

@Repository("loginDao")
public class LoginDaoImpl implements LoginDao {
	@Resource(name="sessionFactory")
	public SessionFactory sessionFactory;
    public Session getSession(){
    	return this.sessionFactory.getCurrentSession();
    }
    public void setSessionFactory(SessionFactory sessionFactory){
    	this.sessionFactory = sessionFactory;
    }
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}




展开
收起
爱吃鱼的程序员 2020-06-14 22:57:09 676 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

      <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>*.do</url-pattern>

      </filter-mapping>

    好像少了这个了。。。。。。

    传送门<atarget="_blank"rel="nofollow">http://www.oschina.net/question/659202_86641谢谢评论,他那个是事务没有配置好,我这边不一样,问题已解决,openSession没有配置好<atarget="_blank"rel="nofollow">http://www.oschina.net/question/659202_86641谢谢评论,他那个是事务没有配置好,我这边不一样,问题已解决,openSessionInViewFilter没有配置好<divclass="linenumber5index4alt2"> publicSessiongetSession(){谢谢评论,问题已解决,openSession没有配置好是不是<spanstyle="font-family:Verdana,sans-serif,宋体;font-size:14px;line-height:normal;background-color:#FFFFFF;">openSessionInViewFilter没有设置好?是啊,问题已解决

    2020-06-14 22:57:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
introduction to Apache Bean 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载