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 = root3、需要在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
- Spring学习-25:Spring中的JDBC Template(JDBC模板):设置参数到属性文件
- Spring学习-23:Spring中的JDBC Template(JDBC模板)快速入门
- Spring学习-26:Spring中的JDBC Template(JDBC模板):完成增删改的操作
- Spring学习-27:Spring中的JDBC Template(JDBC模板):完成查询的操作
- Spring学习-24:Spring中的JDBC Template(JDBC模板):默认连接池、DBCP连接池、C3P0池的配置
- 学习经典 Spring jdbc template
- Spring对JDBC的支持 Jdbc Template模板类
- Spring JDBC template
- Spring JDBC Template
- Spring jdbc Template
- Spring JDBC Template
- Spring Jdbc Template详解
- Spring JDBC Template
- Spring AspectJ JDBC Template
- Spring之JDBC Template
- Spring 中的jdbc模板介绍
- Spring -- spring template Spring的JDBC(一)
- spring 的jdbc Template操作
- POJ1017 Packets(贪心算法训练)
- 嵌入式实验EX1:Kahn Process Networks and Synchronous Data Flows
- Opencv 中的结构体CvScalar
- nginx实现反向代理和负载均衡
- Java中的引用类型和垃圾回收
- Spring学习-25:Spring中的JDBC Template(JDBC模板):设置参数到属性文件
- 面试知识点6:MySQL中InnoDB的一级索引、二级索引
- An ffmpeg and SDL Tutorial 03
- Windows下安装Tensorflow教程
- TensorFlow实战11:实现Word2Vec
- Java数组练习(七)求用户输入数组中的奇数个数和偶数个数
- 简单实现二维码扫描
- 去除对话框边框 + 设置窗口可移动和透明+取消主窗口状态栏
- linux安装mysql