码迷,mamicode.com
首页 > 数据库 > 详细

spring_jdbc(转)

时间:2014-05-26 21:00:36      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   a   get   com   

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

spring_jdbc(转)

标签:c   class   blog   a   get   com   

原文地址:http://www.cnblogs.com/aijindi/p/3745908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!