Spring学习笔记(六)

本教程对应视频课程:http://edu.51cto.com/course/14731.htmlhtml

一、SpringJDBC

1.一、Spring在jdbc中的做用

mark

1.二、SpringJDBC实践

一、添加jar包支持类java

spring-jdbc-4.3.14.RELEASE.jar、spring-tx-4.3.14.RELEASE.jarspring

package cn.org.kingdom.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.org.kingdom.dao.DeptDao;
import cn.org.kingdom.vo.Dept;
@Repository
public class DeptDaoImpl implements DeptDao {
    @Autowired
    private JdbcTemplate jt  ; 
    public JdbcTemplate getJt() {
        return jt;
    }
    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    @Override
    public int addUser(Dept vo) throws Exception {
        return jt.update("insert into dept(deptno,dname,loc) values(?,?,?)", vo.getDeptno(),vo.getDname(),vo.getLoc());
    }
}

二、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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
   <context:property-placeholder location="classpath:db.properties"/>
   <!-- 扫包 -->
   <context:component-scan base-package="cn.org.kingdom"/>
   <bean id="ds" class = "org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
   </bean>  
   <bean id = "jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"/>
   </bean>
</beans>

1.三、事务处理

1.3.一、spring对事务的支持

Spring对事务有很好的支持.提供了事务管理器. PlatformTransactionManager:平台事务管理器(顶层)express

配置事务管理交给Spring来作apache

<!—①配置事务管理器(AOP:作什么) --> 
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource"/>
    <!-- 2.配置加强(AOP:什么时机:本质是一个环绕加强) --> 
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="trans"/>
        </tx:attributes>     
    </tx:advice>
    <!-- 3.配置加强的地点(AOP:在哪里加强) --> 
    <aop:config>
        <aop:pointcut expression="execution(* cn.org.kingdom.service.*Service.*(..))" id="txPoint"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>

1.3.二、spring事务的属性说明

一、name:匹配到的方法模式(支持统配符);dom

二、read-only:查询的时候,设置为true,其余设置为false (也能够默认不设置) ide

三、isolation:表明数据库事务隔离级别(默认不用配置此项)DEFAULT:让spring使用数据库默认的事务隔离级别;其余:spring模拟;this

四、no-rollback-for: 若是遇到的异常是匹配的异常类型,就不回滚事务;(记录日志等)url

五、rollback-for:若是遇到的异常是指定匹配的异常类型,才回滚事务; 注意:Spring中默认只能回滚RuntimeException及其子类异常类型.(Exception.Throwabale就不能回滚) 、要求咱们:在Service层中全部的异常,使用RuntimeException类型、若原始异常不是RuntimeException,从新包装成RuntimeException.

六、propagation:事务的传播方式(当一个方法已经在一个开启的事务当中了,应该怎么处理自身的事务)

​ ①REQUIRED(默认的传播属性):若是当前方法运行在一个没有事务环境的状况下,则开启一个新的事务,若是当前方法运行在一个已经开启了的事务里面,把本身加入到开启的那个事务中 (通常默认选择此项)

​ ②REQUIRES_NEW:无论当前方法是否运行在一个事务空间以内,都要开启本身的事务保存数据建议使用这个

.(了解)

通用的事务管理器

<tx:advice id="crudAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRES_NEW" />
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="del*" />
            <tx:method name="get*" read-only="true"/>
            <tx:method name="list*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            //此配置必须必须放在最后
           <tx:method name="*"/>
        </tx:attributes>
</tx:advice>

1.3.三、事务的注解配置

@Service("accountService")
@Transactional:该类中全部方法都使用默认的属性加强 
public class AccountServiceImpl implements IAccountService {
    @Autowired
    @Qualifier("accountDAO")
    private IAccountDAO dao;
    @Transactional(propagation=Propagation.REQUIRES_NEW) 
    public void save(){}
    @Transactional(readOnly=true) 
    public void get(){}
    public void trans(Long outId, Long inId, Integer money) {
        System.out.println("11");
        dao.transIn(inId, money);
        System.out.println(1/0);//模拟停电
        dao.transOut(outId, money);
    }
}
@Repository("accountDAO")
public class AccountDAOImpl implements IAccountDAO {
    private JdbcTemplate jdbcTemplate;
    @Resource(name="dataSource")
    public void setDataSource(DataSource ds){
        jdbcTemplate = new JdbcTemplate(ds);
    }
}
相关文章
相关标签/搜索