zoukankan      html  css  js  c++  java
  • 一步一步深入spring(7)-- 整合spring和JDBC的环境

    1.配置数据源

    (1).添加支持数据源的jar包commons-dbcp.jar 、commons-pool.jar
    当然也要添加其他的spring用到的jar以及这里用到的数据库mysql的jar  mysql-connector-java-5.0.7-bin.jar
    (2).在bean.xml中添加配置文件代码
     1     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     2         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
     3         <property name="url" value="jdbc:mysql://127.0.0.1:3306/yangyang"/>
     4         <property name="username" value="root"/>
     5         <property name="password" value="123456"/>
     6          连接池启动时的初始值
     7          <property name="initialSize" value="1"/>
     8          连接池的最大值
     9          <property name="maxActive" value="500"/>
    10          最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止
    11          <property name="maxIdle" value="2"/>
    12           最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
    13          <property name="minIdle" value="1"/>
    14      </bean>
    2.配置事务
    配置事务时,需要在命名空间中添加引用事务声明的tx命名空间,配置事务有两种方式,一种是基于注解,一种是基于xml方式
    这里先来实现基于注解的方式。
     配置spring对事务的支持:
    1     <!-- spring的事务管理器 -->
    2     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    3          <property name="dataSource" ref="dataSource"/>
    4     </bean>
    5     
    6     <!-- 采用@Transactional使用注解方式处理事务 -->
    7     <tx:annotation-driven transaction-manager="txManager"/>

     3.编写业务bean,这里只给出实现部分的代码:

     1 package com.yangyang.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import javax.sql.DataSource;
     6 
     7 import org.springframework.jdbc.core.JdbcTemplate;
     8 import org.springframework.transaction.annotation.Transactional;
     9 
    10 import com.yangyang.model.Person;
    11 import com.yangyang.service.PersonService;
    12 
    13 //受spring的事务管理
    14 @Transactional
    15 public class PersonServiceImpl implements PersonService{
    16 
    17     private JdbcTemplate jdbcTemplate;//通过jdbcTemplate来操作数据源
    18     public void setDataSource(DataSource dataSource) {
    19         this.jdbcTemplate = new JdbcTemplate(dataSource);
    20     }
    21 
    22     @Override
    23     public void save(Person person) {
    24         jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()}, 
    25                 new int[]{java.sql.Types.VARCHAR});
    26     }
    27 
    28     @Override
    29     public void update(Person person) {
    30         jdbcTemplate.update("update person set name=? where id=?",new Object[]{person.getName(),person.getId()}, 
    31                 new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
    32     }
    33 
    34     @Override
    35     public void delete(Integer personId) {
    36         jdbcTemplate.update("delete from person where id=?",new Object[]{personId}, 
    37                 new int[]{java.sql.Types.INTEGER});
    38     }
    39 
    40     @Override
    41     public Person getPerson(Integer personId) {
    42         return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personId},new int[]{java.sql.Types.INTEGER}
    43         ,new PersonRowMapper());
    44     }
    45 
    46     @Override
    47     public List<Person> getPersons() {
    48         return (List<Person>)jdbcTemplate.query("select * from person",new PersonRowMapper());
    49     }
    50 
    51 }

    对查询支持的RowMapper 类

     1 package com.yangyang.service.impl;
     2 
     3 import java.sql.ResultSet;
     4 import java.sql.SQLException;
     5 
     6 import org.springframework.jdbc.core.RowMapper;
     7 
     8 import com.yangyang.model.Person;
     9 
    10 public class PersonRowMapper implements RowMapper{
    11 
    12     @Override
    13     public Object mapRow(ResultSet rs, int index) throws SQLException {
    14         Person person=new Person();
    15         person.setName(rs.getString("name"));
    16         person.setId(rs.getInt("id"));
    17         return person;
    18     }
    19 
    20 }

    当然我们也需要将业务bean配置到spring容器中,完整的配置文件如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:context="http://www.springframework.org/schema/context"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"    
     7         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     8             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
     9             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    10             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">     
    11   
    12     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    13         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    14         <property name="url" value="jdbc:mysql://127.0.0.1:3306/yangyang"/>
    15         <property name="username" value="root"/>
    16         <property name="password" value="123456"/>
    17          连接池启动时的初始值
    18          <property name="initialSize" value="1"/>
    19          连接池的最大值
    20          <property name="maxActive" value="500"/>
    21          最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止
    22          <property name="maxIdle" value="2"/>
    23           最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
    24          <property name="minIdle" value="1"/>
    25      </bean>
    26      
    27     <!-- spring的事务管理器 -->
    28     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    29          <property name="dataSource" ref="dataSource"/>
    30     </bean>
    31     
    32     <!-- 采用@Transactional使用注解方式处理事务 -->
    33     <tx:annotation-driven transaction-manager="txManager"/>
    34 
    35     <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
    36         <property name="dataSource" ref="dataSource"></property>
    37     </bean>
    38 </beans>

    接下来编写单元测试来测试我们的类,这里省略了,可以看到配置成功。

    当然我们会想把jdbc连接的部分以properties的形式抽出来,这样就有

    driverClassName=org.gjt.mm.mysql.Driver
    url=jdbc:mysql://127.0.0.1:3306/yangyang
    username=root
    password=123456
    initialSize=1
    maxActive=500
    maxIdle=2
    minIdle=1

    然后在bean中改为:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:context="http://www.springframework.org/schema/context"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"    
     7         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     8             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
     9             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    10             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    11  <!-- 加上aop的命名空间以及DTD验证 -->
    12      
    13      <!-- spring对外部文件的支持 -->
    14      <context:property-placeholder location="classpath:resources/jdbc.properties"/>
    15      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    16         <property name="driverClassName" value="${driverClassName}"/>
    17         <property name="url" value="${url}"/>
    18         <property name="username" value="${username}"/>
    19         <property name="password" value="${password}"/>
    20          <!-- 连接池启动时的初始值 -->
    21          <property name="initialSize" value="${initialSize}"/>
    22          <!-- 连接池的最大值 -->
    23          <property name="maxActive" value="${maxActive}"/>
    24          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
    25          <property name="maxIdle" value="${maxIdle}"/>
    26          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
    27          <property name="minIdle" value="${minIdle}"/>
    28      </bean>
    29      
    30     <!-- spring的事务管理器 -->
    31     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    32          <property name="dataSource" ref="dataSource"/>
    33     </bean>
    34     
    35     <!-- 采用@Transactional使用注解方式处理事务 -->
    36     <tx:annotation-driven transaction-manager="txManager"/>
    37 
    38     <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
    39         <property name="dataSource" ref="dataSource"></property>
    40     </bean>
    41 </beans>

    执行单元测试发现也是ok的

    下面再来利用xml配置的方式来实现事务,在xml中配置如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:context="http://www.springframework.org/schema/context"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"    
     7         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     8             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
     9             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    10             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    11  <!-- 加上aop的命名空间以及DTD验证 -->
    12      
    13      <!-- spring对外部文件的支持 -->
    14      <context:property-placeholder location="classpath:resources/jdbc.properties"/>
    15      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    16         <property name="driverClassName" value="${driverClassName}"/>
    17         <property name="url" value="${url}"/>
    18         <property name="username" value="${username}"/>
    19         <property name="password" value="${password}"/>
    20          <!-- 连接池启动时的初始值 -->
    21          <property name="initialSize" value="${initialSize}"/>
    22          <!-- 连接池的最大值 -->
    23          <property name="maxActive" value="${maxActive}"/>
    24          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
    25          <property name="maxIdle" value="${maxIdle}"/>
    26          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
    27          <property name="minIdle" value="${minIdle}"/>
    28      </bean>
    29      
    30     <!-- spring的事务管理器 -->
    31     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    32          <property name="dataSource" ref="dataSource"/>
    33     </bean>
    34 
    35     <!-- 使用配置的形式使用注解 -->
    36     <aop:config>
    37         <aop:pointcut  id="transactionPointcut" expression="exception(* com.yangyang.service..*.*(..))"/>
    38         <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
    39     </aop:config>
    40     
    41     <tx:advice id="txAdvice" transaction-manager="txManager">
    42         <tx:attributes>
    43                 <!--get开头的方法不添加事务  -->
    44             <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
    45             <tx:method name="*"/>
    46         </tx:attributes>
    47     </tx:advice>
    48     
    49     <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
    50         <property name="dataSource" ref="dataSource"></property>
    51     </bean>
    52 </beans>

    这样,我们不需要在业务类中加入任何注解,通过配置也能实现事务鼓管理

  • 相关阅读:
    欧拉公式求四面体的体积
    欧拉公式求四面体的体积
    I
    I
    闭包传递(floyed)
    闭包传递(floyed)
    Python hypot() 函数
    Python cos() 函数
    Python atan2() 函数
    Python atan() 函数
  • 原文地址:https://www.cnblogs.com/shunyang/p/3311098.html
Copyright © 2011-2022 走看看