spring_jdbc(转)

时间:2014-05-26 21:00:36   收藏:0   阅读:291

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="oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="fengpeng"/>
   <property name="password" value="fengpeng"/>
  </bean-->
  
  <!-- 2.org.springframework.jdbc.datasource.SimpleDriverDataSource
    和DriverManagerDataSource一样
  -->
  <!--bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
   <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="fengpeng"/>
   <property name="password" value="fengpeng"/>
  </bean-->
  
  <!-- 3.org.springframework.jdbc.datasource.SingleConnectionDataSource
    无论怎么取,都返回同一个连接对象
  -->
  <!--bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
   <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="fengpeng"/>
   <property name="password" value="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="oracle.jdbc.driver.OracleDriver"/>
   <property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
   <property name="username" value="fengpeng"/>
   <property name="password" value="fengpeng"/>
   <property name="maxActive" value="10"/>
   <property name="maxIdle" value="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="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
    <property name="username" value="fengpeng"/>
    <property name="password" value="fengpeng"/>
    <property name="maxActive" value="10"/>
    <property name="maxIdle" value="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;
   }

spring_jdbc(转),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!