• Spring管理连接池的几种方式


    第一种方式:.Spring常规的数据库连接方法:

     1 @RunWith(SpringJUnit4ClassRunner.class)
     2 @ContextConfiguration(locations="classpath:applicationContext.xml")
     3 public class jdbcTemplateTest1 {
     4 /*    @Test
     5     public void test1(){
     6     //1.创建数据库连接池(Spring)
     7         DriverManagerDataSource dataSource = new DriverManagerDataSource();
     8     //2.设置参数
     9         //获取驱动
    10         dataSource.setDriverClass("com.mysql.jdbc.Driver");
    11         //连接数据库
    12         dataSource.setJdbcUrl("jdbc:mysql:///springtest");
    13         dataSource.setUser("root");
    14         dataSource.setPassword("123");
    15     //3.创建jdbcTemplate
    16         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    17         jdbcTemplate.execute("update t_user set name='tom' where id=3");
    18     } */   
    19     @Autowired
    20     private JdbcTemplate jdbcTemplate;
    21     @Test
    22     public void test2(){
    23     jdbcTemplate.execute("update t_user set name='tom' where id=1");
    24 }
    25 }
    常规方案

    第二种方式:注入Spring,由Spring内部管理

    1.测试类

    1  测试类 
    @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(locations="classpath:applicationContext.xml") 3 public class jdbcTemplateTest1 { 4 @Autowired 5 private JdbcTemplate jdbcTemplate; 6 @Test 7 public void test2(){ 8 jdbcTemplate.execute("update t_user set name='tom' where id=1");}}

    2.1applicationContext.xml配置文件-----Spring内置的连接池
     1 <!-- Spring内置的连接池 -->
     2 <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     3 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
     4 <property name="url" value="jdbc:mysql:///springtest" />
     5 <property name="username" value="root"/>
     6 <property name="password" value="123"></property>
     7 </bean>
     8 
     9 <!-- ref声明数据源,交由Spring管理 -->
    10 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    11 <property name="dataSource" ref="driverManagerDataSource"></property>
    12 </bean>

    2.2  applicationContext.xml配置文件-----c3p0连接池

     1 <!-- 创建c3p0连接池 -->
     2     <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     3         <property name="driverClass" value="com.mysql.jdbc.Driver" />
     4         <property name="jdbcUrl" value="jdbc:mysql:///springtest" />
     5         <property name="user" value="root" />
     6         <property name="password" value="123" />
     7     </bean>
     8     <!-- ref声明c3p0的数据源,交由Spring管理 -->
     9     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    10         <property name="dataSource" ref="c3p0DataSource"></property>
    11 </bean>

    2.3在c3p0基础上引用外部属性--为了切换oracle等数据库

     1<!-- 引入外部的properties文件 -->

    <context:property-placeholder location="classpath:db.properties"/>
        <!-- 创建c3p0连接池 -->
     2     <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     3         <property name="driverClass" value="${jdbc.driverClass}" />
     4         <property name="jdbcUrl" value="${jdbc.url}" />
     5         <property name="user" value="${jdbc.username}" />
     6         <property name="password" value="${jdbc.password}" />
     7     </bean>
     8     <!-- ref声明c3p0的数据源,交由Spring管理 -->
     9     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    10         <property name="dataSource" ref="c3p0DataSource"></property>
    11     </bean> 
    ----------------------------------------------------------------------
    db.properties

    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.url= jdbc:mysql:///springtest
    jdbc.username=root
    jdbc.password=123



  • 相关阅读:
    Visual Studio 2005 Starter Kits
    怎样去做才是朋友?
    C#读写日志文本文件
    [文摘20080707]马云追加投资20亿 淘宝首定10年超沃尔玛目标
    [转]在WinForm应用程序中实现自动升级
    [转]官方Flash CS3简体中文帮助文档下载,AS3.0简体中文帮助文档下载
    [引]MySQL INNODB类型表的外键关联设置
    [转]winfrom让弹出的MessageBox在指定时间内自动销毁
    生活开心一笑 之 "我家半"与"QQ病毒"
    [English20080721]疯狂英语365句
  • 原文地址:https://www.cnblogs.com/wwwzzz/p/7909894.html
Copyright © 2020-2023  润新知