zoukankan      html  css  js  c++  java
  • Spring的JDBC Template

    Spring的JDBC Template(JDBC模板)简化JDBC API开发,使用上和Apache公司的DBUtils框架非常类似)

    快速入门实例

    1、创建项目后,导入Spring基础核心开发包、数据库驱动包以及日志记录相关包

    导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar以及mySql的驱动

    2、创建applicationContext.xml

    3、编写一个测试类

    package com.js.demo1;
    
    
    import org.junit.Test;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
     
    public class SpringJDBCTemplateTest {
        
        @Test
        public void demo1(){
            
            //创建连接池,这里使用的是Spring自带的连接池
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            //设置参数
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/springjdbc");
            dataSource.setUsername("root");
            dataSource.setPassword("123456");
            
            //使用JDBC的模板,传入DataSource,带参构造器或者setter方法均可
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
            System.out.println("创建 table user...");
        }
    }

    默认连接池、DBCP连接池、C3P0池的配置

    1、在src目录下新建属性文件,文件名任意,我这里取jdbc.properties,文件前后不能有多余空格。

    2、将参数写入属性文件中,属性名可以任意,但是不能和连接池的默认属性冲突,比如C3P0连接池赋值为user的不能是${jdbc.username}:

    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://127.0.0.1:3306/springjdbc?characterEncoding=utf-8
    jdbc.name = root
    jdbc.password = 123456

    设置参数到属性文件的两种方式

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 第1种,配置属性文件:配置bean,暂时注释 -->

    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location" value="classpath:jdbc.properties"></property>

    </bean> -->
    <!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 配置Spring默认连接池 -->
    <bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.name}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 定义JDBC的模板类 -->
    <bean id="defaultJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="defaultDataSource"></property>
    </bean>
    </beans>

    1、默认连接池,实现类 DriverManagerDataSource

    1、导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar以及mySql的驱动

    2、配置Spring默认连接池以及JDBC模板类

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 第1种,配置属性文件:配置bean,暂时注释 -->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
    </bean> -->

    <!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置Spring默认连接池 -->
    <bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.name}"></property>
        <property name="username" value="${jdbc.name}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 定义JDBC的模板类 -->
    <bean id="defaultJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="defaultDataSource"></property>
    </bean>
    </beans>

    3、编写测试类

    package com.js.demo1;
    
    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.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    /**
     * 默认连接池配置测试
     *  * @author hdb
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class DefaultDataSourceTest {
        
        @Autowired
        @Qualifier("defaultJdbcTemplate")
        private JdbcTemplate jdbcTemplate;
        
        @Test
        public void demo2(){
            jdbcTemplate.execute("create table user1 (id int primary key auto_increment,name varchar(20))");
            System.out.println("创建 table user1...");
        }
    }

    2、DBCP数据源 BasicDataSource 

    1、导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar、commons-dbcp-1.4.jar、commons-pool-1.6.jar以及mySql的驱动

    2、配置DBCP连接池

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">
    

    <!-- 第1种,配置属性文件:配置bean,暂时注释 -->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
    </bean> -->

    <!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置DBCP连接池 -->
    <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.name}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 定义JDBC的模板类 -->
    <bean id="dbcpJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dbcpDataSource"></property>
    </bean>
    </beans>

    3、编写测试类

    package com.js.demo1;
    
    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.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    /**
     * DBCP连接池配置测试
     * @author hdb
     *
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class DbcpDataSourceTest {
        
        @Autowired
        @Qualifier("dbcpJdbcTemplate")
        private JdbcTemplate jdbcTemplate;
        
        @Test
        public void demo2(){
            jdbcTemplate.execute("create table user2 (id int primary key auto_increment,name varchar(20))");
            System.out.println("创建 table user2...");
        }
    }

    3、C3P0数据源 ComboPooledDataSource(重点掌握)

    1、导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar、com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar以及mySql的驱动

    2、配置C3P0连接池

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">
    

    <!-- 第1种,配置属性文件:配置bean,暂时注释 -->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
    </bean> -->

    <!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置C3P0连接池 -->
    <bean id="C3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.name}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 定义JDBC的模板类 -->
    <bean id="C3p0JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="C3p0DataSource"></property>
    </bean>
    </beans>

    3、编写测试类

    package com.js.demo1;
    
    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.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    /**
     * C3P0连接池配置测试
     * @author hdb
     *
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class C3p0DataSourceTest {
        
        @Autowired
        @Qualifier("C3p0JdbcTemplate")
        private JdbcTemplate jdbcTemplate;
        
        @Test
        public void demo2(){
            jdbcTemplate.execute("create table user3 (id int primary key auto_increment,name varchar(20))");
            System.out.println("创建 table user3...");
        }
    }
  • 相关阅读:
    HDU 1301 Jungle Roads (最小生成树)
    POJ 1733 Parity game (并查集)
    HDU 3038 How Many Answers Are Wrong (并查集)
    CentOS用yum安装搭建LAMP
    Linux下php安装Redis扩展
    PHPExcel用法
    利用phpmailer类邮件发送
    Vim编辑器配置
    vhost文件设置
    ThinkPHP验证码类
  • 原文地址:https://www.cnblogs.com/huangdabing/p/9484954.html
Copyright © 2011-2022 走看看