qq52214373 2015-04-18 12:23 采纳率: 0%
浏览 2809

Spring 整合hibernate 事物不起作用

spring配置文件

 <context:component-scan base-package="com.yy.cms"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cms" />
        <property name="user" value="root" />
        <property name="password" value="accp" />

        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="3" />
        <!--连接池中保留的最小连接数。Default: 3 -->
        <property name="minPoolSize" value="3" />
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="5" />
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3" />
        <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
        <property name="maxStatements" value="8" />
        <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
        <property name="maxStatementsPerConnection" value="5" />
        <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800" />    
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

BaseDao代码

 package com.yy.cms.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.util.List;



import javax.inject.Inject;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.yy.cms.dao.BaseDao;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class BaseDaoImpl<T> implements BaseDao<T> {

    @Inject
    private SessionFactory sessionFactory;
    // 泛型的真正类型
    private Class clazz;

    // 通过反射获得泛型的真实类型
    public BaseDaoImpl() {
        ParameterizedType type = (ParameterizedType) this.getClass()
                .getGenericSuperclass();
        clazz = (Class) type.getActualTypeArguments()[0];
    }

    /** 得到Session */
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * 保存实体
     * 
     * @param t
     *            将被保存的实体
     * @return 被保存的实体
     */
    public T save(T t) {
        getSession().save(t);
        return t;
    }

测试类

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/beans.xml")
@Transactional
@Repository
public class BaseDaoImplTest {

    @Inject
    private UserDao userDao;

    @Test
    public void testSave(){
        userDao.save(new User("test"));
    }

    @Test
    public void testUpdate(){
        User user=new User(2,"update");
        userDao.update(user);
    }
  • 写回答

11条回答 默认 最新

  • devmiao 2015-04-18 15:03
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常