今天主要总结的是关于Spring中与jdbc的连接

例子还是昨天的相同例子

首先,我们要先开始配备bean3.xml中的配置文件

在配置过程当中,首先需要引入的jdbc的包

在这个配置中的具体思想为:

1、JdbcTemplate操作数据库


Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。同时,为了支持对properties文件的支持,spring提供了类似于EL表达式的方式,把dataSource.properties的文件参数引入到参数配置之中,<context:property-placeholder location="classpath:jdbc.properties" />。




2,Spring 为DAO的编写带来的好处:

1)优化了的异常类型体系:
      细化了数据访问异常,丰富了异常类型
      (都是 Unchecked Exception,这种异常不会变动,采用同一种异常,表示同一种现象,与使用的持久化技术无关)
 2)使用模板回调模式,开发者不再写模式化代码,简化编程:  
      不变:资源的获取,资源的释放,异常转化(Spring提供了模板类对此负责)
      变化:SQL,变量,结果集的提取

  
基于JDBC的DAO实现:

 使用 JdbcTemplate:(线程安全的)
 
  1)意义:简化对JDBC的操作
        模板负责:JDBC对象的获取释放,异常类型的转化
        开发者负责:提供SQL,设置SQL中的变量,提取ResultSet
  2)应用:

核心方法:
            query();
            update();
         核心回调接口:
            PreparedStatementCreator
            PreparedStatementSetter
            ResultSetExtractor
            RowMapper3)在DAO中获得JdbcTemplate的两种方式:
       A)给DAO注入JdbcTempate:
             Bean配置:DataSource->JdbcTemplate(需要bean工厂控制)->DAO       B)使DAO类继承 JdbcDaoSupport :
             继承的方法:getJdbcTemplate()
             Bean配置:DataSource->DAO


DAO需要一个接口和一个接口的实现类。因为Spring注入的时候只能用接口注入


dao是数据库接入层,这个是一个接口,implementsDao去实现这个接口,也就是按照接口中定义的方法取数据,Service也是一个接口,这个接口可以将不同的Dao放在同一个服务中,implementService实现Service。例如我们有个JavaBeanUser类,Course类,然后想在数据库中存取这个user相关的数据.对数据库的操作无非是增删改查,所以就对User类属性的增删改查,做一个接口,为什么要做接口呢,因为我们想与具体的实现脱离耦合关系。因为具体数据库的增删改查,我们既可以用Hibernate也可以ibitas这个dao中只是定义了我要增删改查,具体实现可以用不同的方法,对于user,和Courese他们分别有自己的增删改查,但是我们有的时候又想同时操作他们,所以也就有了Service接口。可以在这个接口中将User和Course的Dao当作成员变量。然后具体操作的时候传进来的是实现dao的imp就行了。然后在action中调用service

现在将bean3.xml文件配置进行上传
<?xml version="1.0" encoding="UTF-8"?>
 <beans default-autowire="byName"
default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 配置数据源 和连接池-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
           <property name="url" value="jdbc:mysql://localhost:3306/bigdata"></property>
           <property name="username" value="root"></property>
           <property name="password" value="yangfei"></property>
        </bean>
        
        <!-- spring提供的持久层框架(数据库操作层,Dao层) 1.事务支持,2,数据库连接管理,3.数据库操作的封装 4,原始的对象映射-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
           <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <bean id="userDao" class="jdbc.UserDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        
        <!-- 事务管理器,也是利用拦截器的原理,就是AOP -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 定义一个拦截器 AOP的另外一种写法 它要引用事务管理器-->
        <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager" ref="transactionManager"></property>
            <property name="transactionAttributes">
               <props>
                  <!-- 事务拦截四大类型 -->
                  <prop key="delete*">PROPAGATION_REQUIRED</prop>
                  <prop key="add*">PROPAGATION_REQUIRED</prop>
                  <prop key="update*">PROPAGATION_REQUIRED</prop>
                  <prop key="insert*">PROPAGATION_REQUIRED</prop>
                  <prop key="save*">PROPAGATION_REQUIRED</prop>
               </props>
            </property>
        </bean>
        
        <!--定义事务代理(事务管理的工作交给该类就可以了) 拦截器用来拦截哪些类 -->
        <bean id="txProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <!-- 根据拦截对象的名称去识别 -->
          <property name="beanNames">
             <list>
                <!-- 可以放入DAO层,可以放Service,可以放controller,可以放Action -->
                <value>*Dao*</value>
             </list>
          </property>
          <property name="interceptorNames">
            <list>
                <!-- 表明可以放入很多拦截器,有事务相关的拦截器,有日志相关的,有权限相关的等等 -->
                <value>transactionInterceptor</value>
            </list>
          </property>
        </bean>
 </beans>