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);
}
<!-- 配置事务通知规则 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>