spring+jdbc开发

1.所需要的jar包

 spring基本jar包+ commons-dbcp.jar commons-pool.jar

数据库驱动mysql-connector-java-3.1.13-bin.jar 

如果采用注解方式管理组件要引入common-annotations.jar 

2.注解方式管理事务的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/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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:annotation-config/>
    <bean id="personDao" class="com.westsoft.daoimpl.PersonDaoBean"></bean>           
    <bean id="personServiec" class="com.westsoft.serviceimpl.PersonServiceBean">    
        <property name="personDao" ref="personDao"/>
    </bean>
    
-->
    <!-- 注解方式自动扫描包方式 -->
    <context:component-scan base-package="com.westsoft"/>
        
    
    <!-- Aop支持 -->
    <aop:aspectj-autoproxy/>
    
    <!-- AOP可以用注解的方式也可以用配置XML的方式 -->
    
    <!-- 数据源配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="soft"/>
         <!-- 连接池启动时的初始值 -->
         <property name="initialSize" value="1"/>
         <!-- 连接池的最大值 -->
         <property name="maxActive" value="500"/>
         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
         <property name="maxIdle" value="2"/>
         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
         <property name="minIdle" value="1"/>
     </bean>

    <!-- 事务管理 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>
    
</beans> 

3.示例代码(注解方式管理事务)

 package com.westsoft.serviceimpl;


import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.westsoft.bean.Person;
import com.westsoft.dao.PersonDao;
import com.westsoft.service.PersonService;

@Service @Transactional
public class PersonServiceBean implements PersonService {
    
    
    private JdbcTemplate jdbcTemplate;
    
    @Resource
    public void setJdbcTemplate(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
    
    @Resource
    private PersonDao personDao;
    
    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    
    
    @Override
    public void delete(Integer id) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public Person getPerson(Integer id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public List<Person> getPersons() {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * 新增
     * @Transactional(rollbackFor=Exception.class)
     * RuntimeException运行期例外,默认回滚
     * Exception 非运行期例外,不回滚
     * @Transactional(rollbackFor=Exception.class)
     * @Transactional(propagation=Propagation.NOT_SUPPORTED):不开启事务
     * @Transactional(propagation=Propagation.REQUIRED):默认事务行为,如果方法运行时已经处在一个事务中,则加入该事务,
     *                                                     否则为自己创建一个事务
     * @Transactional(propagation=Propagation.REQUIREDNEW):必须开启一个新事务,事务中嵌套事务
     
     *  @Transactional(propagation=Propagation.SUPPORTS):如果方法在已存在的事务中执行,则加入该事务,如果不在事务中执行,
     *                                                  则方法在没有事务的环境下执行.
     *  @Transactional(propagation=Propagation.MANDATORY):指定方法只能在已有的事务中执行,不会自动创建事务,如果没有在事务
     *                                                  中执行,则抛出异常.
     *  @Transactional(propagation=Propagation.NEVER):不能在事务中执行,否则抛出异常
     *  
     *  @Transactional(propagation=Propagation.NESTED):
     *  
     
*/
    @Override @Transactional(propagation=Propagation.REQUIRED)
    public void save(String name) throws Exception {
        // TODO Auto-generated method stub
        
        this.jdbcTemplate.update("insert into person(name) values(?)", new Object[]{name}, 
                    new int[]{java.sql.Types.VARCHAR});
        throw new RuntimeException("运行期例外");
        
    }

    @Override
    public void update(Person person) {
        // TODO Auto-generated method stub
        
    }
}

4.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/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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:annotation-config/>
    <bean id="personDao" class="com.westsoft.daoimpl.PersonDaoBean"></bean>           
    <bean id="personServiec" class="com.westsoft.serviceimpl.PersonServiceBean">    
        <property name="personDao" ref="personDao"/>
    </bean>
    
-->
    <!-- 注解方式自动扫描包方式 -->
    <context:component-scan base-package="com.westsoft"/>
        
    
    <!-- Aop支持 -->
    <aop:aspectj-autoproxy/>
    
    <!-- AOP可以用注解的方式也可以用配置XML的方式 -->
    
    <!-- 数据源配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="soft"/>
         <!-- 连接池启动时的初始值 -->
         <property name="initialSize" value="1"/>
         <!-- 连接池的最大值 -->
         <property name="maxActive" value="500"/>
         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
         <property name="maxIdle" value="2"/>
         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
         <property name="minIdle" value="1"/>
     </bean>

    <!-- 事务管理 -->        
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!--注解方式 -管理事务--类中需要注解
    <tx:annotation-driven transaction-manager="txManager"/>
     
-->
     
     <!-- 配置方式管理事务,可以出去类中的关于事务的注解 -->
     <aop:config>
          <aop:pointcut id="transactionPointcut" expression="execution(* com.westsoft.serviceimpl.*.*(..))"/>
          <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config> 
    <tx:advice id="txAdvice" transaction-manager="txManager">
          <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
            <tx:method name="*"/>
          </tx:attributes>
    </tx:advice>
     
     
    <!-- 
    <bean id="personServiceBean" class="com.westsoft.serviceimpl.PersonServiceBean">
        <property name="jdbcTemplate" ref="dataSource"/>
    </bean>
     
-->
     <!-- 
     <bean id="abc" class="com.westsoft.bean.testAbc"/>
      
-->
</beans> 

5.将(3)中的类中除去事务的注解 

原文地址:https://www.cnblogs.com/kuailewangzi1212/p/2408540.html