自建 springmvc+hibernate 项目
该死的报错: 请大神帮忙!
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
开发工具:
myEclipse 10.0
tomcat 6.0
测试service可以得到结果,使用session如下代码,可以正常从数据库增删改查.
Session session = sessionFactory.getCurrentSession(); //方法一
Session session = sessionFactory.openSession(); //方法二
session.close();
结果如下:
public static void main(String[] args) {
ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) con.getBean("studentServiceImpl");
List<ZtStudentLog> list = studentService.getTotalStudent();
for (ZtStudentLog z : list) {
System.out.println(z.getUsername()+" "+z.getAge()+" "+z.getId()+" "+z.getSex());
}
}
Hibernate: select this_.ID as ID0_0_, this_.AGE as AGE0_0_, this_.SEX as SEX0_0_, this_.USERNAME as USERNAME0_0_ from ZT_STUDENT_LOG this_
王1 66 1 男
王2 55 2 女
王4 33 4 女
码子 13 5 女
码2 45 3 女
但是在controller层调用的时候就会出问题,只能用方法二进行查询,增删改会产生相应的sql语句但不能对数据库进行操作,方法一就直接报错 No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 心都碎了 急死了,下面直接上代码,求大神们帮我看看.
web.xml部分代码:
applicationContext.xml部分代码:
<!-- 上下文配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<context:component-scan base-package="com.pb" />
<!-- 读取jdbc.properties文件信息,方便更改数据库 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<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.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- hibernate注释写法,不需要写*.hbm.xml了. -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${jdbc.hibernate.format_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.pb.model." />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config >
<aop:pointcut expression="execution(public * com.pb.service.impl.*.*(..))" id="pointCut"/>