首页 > 代码库 > spring_jdbc(转)

spring_jdbc(转)

1.
 引入spring能力:
  需要三个Libraries:
   Spring 3.0 Persistence JDBC Libraries - <MyEclipse-Library>
   Spring 3.0 AOP Libraries - <MyEclipse-Library>
   Spring 3.0 Core Libraries - <MyEclipse-Library>
   Spring 3.0 Persistence Core Libraries - <MyEclipse-Library>
   
2.
 数据源(dataSource):
  <!-- 1.org.springframework.jdbc.datasource.DriverManagerDataSource
    经典jdbc链接的数据源,相当于每次使用DriverManager取得一个链接
  -->
  <!--bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="http://www.mamicode.com/fengpeng"/>
   <property name="password" value="http://www.mamicode.com/fengpeng"/>
  </bean-->
  
  <!-- 2.org.springframework.jdbc.datasource.SimpleDriverDataSource
    和DriverManagerDataSource一样
  -->
  <!--bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
   <property name="driverClass" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="http://www.mamicode.com/fengpeng"/>
   <property name="password" value="http://www.mamicode.com/fengpeng"/>
  </bean-->
  
  <!-- 3.org.springframework.jdbc.datasource.SingleConnectionDataSource
    无论怎么取,都返回同一个连接对象
  -->
  <!--bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
   <property name="driverClassName" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="http://www.mamicode.com/fengpeng"/>
   <property name="password" value="http://www.mamicode.com/fengpeng"/>
  </bean-->
  
  <!-- 4.用于实战的数据源:org.apache.tomcat.dbcp.dbcp.BasicDataSource
    需要引入Spring 3.0 Persistence JDBC Libraries(jar包)
    或者使用第三方jar包:tomcat-dbcp.jar <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  -->
  <!--bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
   <property name="driverClassName" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="http://www.mamicode.com/fengpeng"/>
   <property name="password" value="http://www.mamicode.com/fengpeng"/>
   <property name="maxActive" value="http://www.mamicode.com/10"/>
   <property name="maxIdle" value="http://www.mamicode.com/5"/>
  </bean-->
  
  <bean id="TestDao" class="dao.TestDaoImple">
   <property name="dataSource" ref="dataSource"/>
  </bean>
  
 3.JdbcTemplate对象的介绍
  在application.xml文件中的配置:
   <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
    <property name="driverClassName" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:moluo"/>
    <property name="username" value="http://www.mamicode.com/fengpeng"/>
    <property name="password" value="http://www.mamicode.com/fengpeng"/>
    <property name="maxActive" value="http://www.mamicode.com/10"/>
    <property name="maxIdle" value="http://www.mamicode.com/5"/>
   </bean>
   <!--bean id="connection" factory-bean="dataSource" factory-method="getConnection" scope="prototype"></bean-->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
   </bean>
   <bean id="TestDao" class="dao.TestDaoImple">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
   </bean>
  在dao.TestDaoImple类中定义JdbcTemplate
   private JdbcTemplate jdbcTemplate;
   public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
   }