zoukankan      html  css  js  c++  java
  • day39-Spring 14-Spring的JDBC模板:DBCP连接池配置

    一般常用的连接池是DBCP和C3P0.


    package cn.itcast.spring3.demo1;
    
    import java.sql.DriverManager;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class SpringTest1 {
        
        @Autowired
        @Qualifier("jdbcTemplate")
        private JdbcTemplate jdbcTemplate;//注入Jdbc模板
        @Test
        public void demo2(){
            jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
        }
        
        
        @Test
        public void demo1(){
            // 创建连接池:
            DriverManagerDataSource dataSource = new DriverManagerDataSource();//Spring自带的连接池
            // 设置参数:
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql:///spring3_day02");
            dataSource.setUsername("root");
            dataSource.setPassword("");
            
            //使用JDBC的模板:
            //JdbcTemplate jdbcTemplate = new JdbcTemplate();
            //jdbcTemplate.setDataSource(dataSource);
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 引入beans的头 -->
    <beans xmlns="http://www.springframework.org/schema/beans"
           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.xsd">
        <!-- 配置Spring默认的连接池 -->
        <!-- 这个类由Spring来帮我们创建,它默认情况下只创建一次,因为是单例的. -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql:///spring3_day02"></property>
            <property name="username" value="root"></property>
            <property name="password" value=""></property>
            
        </bean>
        <!-- 配置DBCP连接池 -->
        <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql:///spring3_day02"></property>
            <property name="username" value="root"></property>
            <property name="password" value=""></property>
            
        </bean>
        
        <!-- 定义jdbctemplate -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
              <property name="dataSource" ref="dataSource1"></property><!-- 把上面定义好的连接池注入进来了 -->
        </bean>
    </beans>
  • 相关阅读:
    sort-list leetcode C++
    sum-root-to-leaf-numbers leetcode C++
    sum-root-to-leaf-numbers leetcode C++
    path-sum-ii leetcode C++
    path-sum-ii leetcode C++
    0139 函数的两种声明方式
    0138 函数可以调用另外一个函数
    0137 函数案例:数组翻转、冒泡排序、判断闰年
    0136 JavaScript中 arguments 的使用
    0135 函数的返回值:return 语句、终止函数 、只能返回一个值、没有 return 返回 undefined、break &continue&return 的区别
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6814194.html
Copyright © 2011-2022 走看看