hiberntate4+spring mvc4报错: No Session found for current thread

Java小白进阶之路 2017-10-11 11:34:56
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">

<!-- Hibernate4 -->
<!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
<context:property-placeholder location="classpath:persistence-mysql.properties" />

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<!-- 可以加多个包 -->
<value>com.sfgk.www</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
</props>
</property>
</bean>

<!-- 数据库映射 -->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.user}" />

<property name="password" value="${jdbc.pass}" />

<property name="maxActive" value="100"></property>

<property name="maxIdle" value="30"></property>

<property name="maxWait" value="500"></property>

<property name="defaultAutoCommit" value="true"></property>

</bean>

<!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" />
<tx:method name="save" propagation="REQUIRED" />
<tx:method name="update" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="show*" propagation="REQUIRED" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>

<aop:config expose-proxy="true">
<!--只对业务逻辑层实施事务-->
<aop:pointcut id="txPointcut" expression="execution(* com.sfgk.www.*.*.*.*.*.*(..))" />
<!--Advisor定义,切入点和通知分别为txPointcut、txAdvice-->
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!--配置异步线程执行器-->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 设置核心池子的大小 -->
<property name="corePoolSize" value="10" />

<!-- 设置最大池子的大小 -->
<property name="maxPoolSize" value="30" />
</bean>

</beans>


web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>json_test</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 定义DispatcherServlet -->
<servlet>
<servlet-name>bean-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 默认/WEB-INF/[servlet名字]-servlet.xml加载上下文,
如果配置了contextConfigLocation参数,
将使用classpath:/bean-dispatcher-servlet.xml加载上下文
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/bean-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截匹配的请求,这里所有请求采用名字为bean-dispatcher的DispatcherServlet处理 -->
<servlet-mapping>
<servlet-name>bean-dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- 解决中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--hibernate4 开启session -->
<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>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!--伪静态页面-->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>


...全文
135 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
公共service:
package com.sfgk.www.util.service.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sfgk.www.util.dao.UtilDao;
import com.sfgk.www.util.pojo.Param;
import com.sfgk.www.util.service.UtilService;

@Transactional(readOnly = false, propagation = Propagation.REQUIRES) 
@SuppressWarnings("all")
@Service
public class UtilServiceImpl implements UtilService{
	@Autowired
	private UtilDao utilDao;         //公共Dao
	
	/**
	 * 根据ID查询对象get
	 * @param classes    对象.class
	 * @param id         int类型ID 
	 * @return
	 * @throws Exception
	 */
	public Object getObjectById(Class classes, int id) throws Exception {
		return this.utilDao.getObjectById(classes, id);
	}
	
	/**
	 * 根据ID查询对象get
	 * @param classes    对象.class
	 * @param id         String类型ID
	 * @return
	 * @throws Exception
	 */
	public Object getObjectById(Class classes, String id) throws Exception {
		return this.utilDao.getObjectById(classes, id);
	}
	
	/**
	 * 根据ID查询对象load
	 * @param classes     对象.class
	 * @param id          int类型ID 
	 * @return
	 * @throws Exception
	 */
	public Object loadObjectById(Class classes, int id) throws Exception {
		return this.utilDao.loadObjectById(classes, id);
	}
	
	/**
	 * 用hql语句查询对象
	 * @param hql         hql语句
	 * @return
	 */
	public Object getHqlObject(String hql) throws Exception {
		return this.utilDao.getHqlObject(hql);
	}
	
	/**
	 * 查询所有
	 * @param hql         hql语句
	 * @return
	 * @throws Exception
	 */
	public Collection getHqlAll(String hql) throws Exception {
		return this.utilDao.getHqlAll(hql);
	}
	
	/**
	 * 用sql 语句查询所有
	 * @param sql         sql语句
	 * @return
	 * @throws Exception
	 */
	public Collection getSqlAll(String sql) throws Exception {
		return this.utilDao.getSqlAll(sql);
	}
	
	/**
	 * 用sql语句分页查询
	 * @param sql         sql语句
	 * @param start 起始页码
	 * @param count 每页个数
	 * @return
	 */	
	public Collection getSqlQuery(String sql,int start,int count)throws Exception{
		return utilDao.getSqlQuery(sql, start, count);
	}

	/**
	 * 用hql语句分页查询
	 * @param hql
	 * @param start 起始页码
	 * @param count 每页个数
	 * @return
	 */		
	public Collection getHqlQuery(String hql,int start,int count)throws Exception{
		return utilDao.getHqlQuery(hql,start,count);
	}
	
	/**
	 * 查询集合大小
	 * @param hql          hql语句
	 * @return
	 */
	
	public Integer getHqlCount(String hql)throws Exception{
		return utilDao.getHqlCount(hql);
	}
	
	/**
	 * 查询集合大小
	 * @param sql          sql语句
	 * @return
	 */	
	public Integer getSqlCount(String sql)throws Exception{
		return utilDao.getSqlCount(sql);
	}
}
Propagation在myeclipse上报错:propagation cannot be resolved to a variable
  • 打赏
  • 举报
回复
公共dao:
package com.sfgk.www.util.dao.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sfgk.www.util.dao.UtilDao;

@SuppressWarnings("all")
@Repository
public class UtilDaoImpl<T> implements UtilDao<T>{
	@Autowired
	private SessionFactory sessionFactory;
	
	public Session getCurrentSession(){
		return sessionFactory.getCurrentSession();
	}
	
	/**
	 * 保存
	 * @param t 实体类对象
	 * @throws Exception
	 */
	public void save(T t){
		getCurrentSession().save(t);
    }
	
	/**
	 * 修改
	 * @param t 实体类对象
	 * @throws Exception
	 */
	public void update(T t){
        getCurrentSession().update(t);
    }
	
	/**
	 * 保存/修改
	 * @param t 实体类对象
	 */
	public void merge(T t){
        getCurrentSession().merge(t);
    }
	
	/**
	 * 按条件更新
	 * @param hql hql语句
	 * @throws Exception
	 */
	public void updateAll(String hql) throws Exception{
		getCurrentSession().createQuery(hql).executeUpdate();
	}
	
	/**
	 * 彻底删除
	 * @param t 实体类对象
	 * @throws Exception
	 */
	public void delete(T t) throws Exception{
		getCurrentSession().delete(t);
	}
	
	/**
	 * 按条件删除
	 * @param hql hql语句
	 * @throws Exception
	 */
	public void deleteAll(String hql) throws Exception{
		getCurrentSession().createQuery(hql).executeUpdate();
	}
	
	/**
	 * 根据ID查询对象get
	 * @param classes          实体类.class
	 * @param id               int类型ID
	 * @return
	 * @throws Exception
	 */
	public Object getObjectById(Class classes,int id) throws Exception{
		return getCurrentSession().get(classes, id);
	}
	
	/**
	 * 根据ID查询对象get
	 * @param classes          实体类.class
	 * @param id               string类型ID
	 * @return
	 * @throws Exception
	 */
	public Object getObjectById(Class classes,String id) throws Exception{
		return getCurrentSession().get(classes, id);
	}
	
	/**
	 * 根据ID查询对象load
	 * @param classes          实体类.class
	 * @param id               int类型ID
	 * @return
	 * @throws Exception
	 */
	public Object loadObjectById(Class classes,int id) throws Exception{
		return getCurrentSession().load(classes, id);
	}
	
	/**
	 * 用hql语句查询对象
	 * @param hql        hql语句
	 * @return
	 */
	public Object getHqlObject(String hql)throws Exception{
		try {
			List list = (List) getCurrentSession().createQuery(hql);
			if(list != null && list.size()>0){
				Object obj = list.get(0);
				return obj;
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询所有
	 * @param hql          hql语句
	 * @return
	 * @throws Exception
	 */
	public Collection getHqlAll(String hql) throws Exception{
		try{
			Query query =  getCurrentSession().createQuery(hql);
			return query.list();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 用sql语句查询全部
	 * @param sql          sql语句
	 * @return
	 * @throws Exception
	 */
	public Collection getSqlAll(String sql) throws Exception{
		try{
			Query query = getCurrentSession().createSQLQuery(sql);
			return query.list();
		}catch(RuntimeException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 用sql 语句分页查询
	 * @param sql   sql语句
	 * @param start 起始页码
	 * @param count 每页的个数
	 * @return
	 * @throws Exception
	 */
	public Collection getSqlQuery(String sql, int start,int count)throws Exception{
		Session session = null ;
		try{
			Query query = getCurrentSession().createSQLQuery(sql);
			query.setFirstResult(start);
			query.setMaxResults(count);
			return query.list();
		}catch (RuntimeException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 用hql语句分页查询
	 * @param hql   hql语句
	 * @param start 起始页码
	 * @param count 每页个数
	 * @return
	 */		
	public Collection getHqlQuery(String hql,int start,int count)throws Exception{
		Session session = null;
		try {
			Query query = getCurrentSession().createQuery(hql);
			query.setFirstResult(start);
			query.setMaxResults(count);
			return query.list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询总记录数
	 * @param hql    hql语句
	 * @return 
	 * @throws Exception
	 */
	public int getHqlCount(String hql) throws Exception {
		List list =  (List<T>) getCurrentSession().createQuery(hql);
		if (null != list && list.size() > 0) {
			if(null != list.get(0)){
				return Integer.parseInt(String.valueOf(list.get(0)));
			}
		}
		return 0;
	}
	
	/**
	 * 用sql 语句查询 记录总数
	 * @param sql     sql语句
	 * @return 
	 * @throws Exception
	 */
	public int getSqlCount(String sql) throws Exception{
		Session session = null;
		try{
			Query query = getCurrentSession().createSQLQuery(sql);
			List<Object> list = query.list();
			if(null != list && list.size()>0){
				if(list.get(0)==null){
					return 0;
				}else{
					return new Integer(list.get(0).toString());
				}
			}
		}catch (RuntimeException e) {
			e.printStackTrace();
		}
		return 0;
	}
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧