解决No Hibernate Session bound to thread, and configuration does not allow creat。。。

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: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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:component-scan base-package="com.donghua"></context:component-scan>
<!-- 加载外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 事物管理,配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- Connection properties -->
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<!-- Pool properties-->
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />

</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--
<property name="mappingResources">
<list>
<value>com/donghua/bean/Article.hbm.xml</value>
<value>com/donghua/bean/BlogInfo.hbm.xml</value>
<value>com/donghua/bean/ClickNum.hbm.xml</value>
<value>com/donghua/bean/Critique.hbm.xml</value>
<value>com/donghua/bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>

</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>-->
<!-- 剩余的配置到。cfg.xml中找 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

</bean>

<!-- 配置声明式事物管理器  -->
<bean id="transctionManger"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- (基于注解的方式) -->
<tx:annotation-driven transaction-manager="transctionManger" />
</beans>

dao配置:注解配置

@Service
public class Dao {
@Resource
private SessionFactory sessionfactory;

@Transactional
public void addUser(User user){
Session session = sessionfactory.getCurrentSession();
session.save(user);

}
}

测试

public class BlogTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

@Test
public void testUser(){

User user = new User("赵六","sdfs","asf","fdd","dd");
Dao dao = (Dao) ac.getBean("dao");
dao.addUser(user);

}
}

上一篇:nginx安装,配置,并可以放静态文件教程


下一篇:Nginx 安装 配置 使用