Spring学习-25:Spring中的JDBC Template(JDBC模板):设置参数到属性文件

来源:互联网 发布:href javascript void 编辑:程序博客网 时间:2024/05/17 08:16

如果只是一般情况下使用Spring 的JDBC模板,前面所说的配置方式就足够了,但是Spring也提供了将参数配置到属性文件的方式来避免到配置文件去修改参数。

1、在src目录下新建属性文件,文件名任意,我这里取jdbc.properties。

2、将参数写入属性文件中,属性名可以任意:

jdbc.driver = com.mysql.jdbc.Driverjdbc.url = jdbc:mysql:///spring3_day02jdbc.user = rootjdbc.password = root
3、需要在applicationContext.xml中使用属性文件配置的内容

这里有两种写法:

(1)

<?xml version="1.0" encoding="UTF-8"?><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"><!-- 配置属性文件 --><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"></property></bean><!-- 配置C3P0连接池 --><bean id="dataSource" 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.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 定义JDBC的模板类 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean></beans>

(2)引入context约束,然后解析属性文件。

<?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"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置C3P0连接池 --><bean id="dataSource" 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.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 定义JDBC的模板类 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean></beans>
4、编写测试类

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 JiangShuai * */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext4.xml")public class SpringTest5 {@Autowired@Qualifier("jdbcTemplate")private JdbcTemplate jdbcTemplate;@Testpublic void demo2(){jdbcTemplate.execute("create table user3333 (id int primary key auto_increment,name varchar(20))");}}
运行测试,表格成功创建。



0 0