hithyc 2017-10-24
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="3"/>
<!-- 最小连接数 -->
<property name="minPoolSize" value="3"/>
<!-- 最大连接时间 -->
<property name="maxConnectionAge" value="28800"/>
</bean><!-- 配置sessionFactory -->
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
<!-- 关联数据源和SessionFactory -->
<property name="dataSource" ref="dataSource"/>
<!-- hiberna的配置文件 -->
<property name="hibernateProperties" >
<props>
<!-- 数据库方言的设置 -->
<prop key="hibernate.dialect">${dialect}</prop>
<!--设置session上下文这里不能设置thread,设置thread就需要手动管理并不能让Spring管理,要么不指定要么指定下面的类 -->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<!-- 显示SQl和格式化SQl -->
<prop key="hibernate.show_sql">${showSql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.zhidi.entity">
</property>
</bean><!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean><!--配置通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置增删改的方法 这里设置的方法可以自动注入 这里的name的方法可以自动搜索 可以定义多个常用的方法名 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delect*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<!--配置查询的方法 REQUIRED表示没有逻辑事务的情况下会自己生产一个,如果有就自动加入
SUPPORTS是如果当前有逻辑事务存在就加入到事务中,如果没有就以非事务进行处理 -->
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>将配置好的数据库的AOP的具体实现通过切入点注入到对应的地方去引用。
<!-- aop配置 --> <aop:config> <!--将配置好的通知嵌入到切点相关联 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zhidi.service.impl..*.*(..))" /> </aop:config>
<!-- 注解模式的注入--> <tx:annotation-driven transaction-manager="txManager"/>
###注解配置的代码类的实现。。。。
@Transactional
@Repository
public class EmpDao implements EmpDaoImpl {
@Autowired
private DataSource dataSource;
@Autowired
private SessionFactory sessionFactory;
public Session getSession() {
return sessionFactory.getCurrentSession();
}
public DataSource getDataSource() {
return dataSource;
}
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public void get(){
Session session = getSession();
Transaction tx = session.beginTransaction();
Emp emp = (Emp) session.get(Emp.class, 2);
System.out.println(emp);
tx.commit();
}