zoukankan      html  css  js  c++  java
  • 解决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);

    }
    }

  • 相关阅读:
    Node爬虫之初体验
    Angular中ui-router实现路由嵌套案例
    Angular路由与多视图综合案例
    面试一周10多家公司面试问题总结 ,react相关的并没多少
    单页面应用和多页面应用
    dangerouslySetInnerHTML 正常解析后台返回的html
    文件上传服务器跨域问题
    回流的触发方式
    antd 树的递归 循环展示部门组织架构
    日常杂项记录:(jquery弹出层插件、js判断是pc还是移动端)
  • 原文地址:https://www.cnblogs.com/daxiong225/p/4726997.html
Copyright © 2011-2022 走看看