21Spring_JdbcTemplatem模板工具类的使用——配置文件(连接三种数据库连接池)

上一篇文章提到过DriverManagerDataSource只是Spring内置的数据库连接池,我们可选的方案还有c3p0数据库连接池以及DBCP数据库连接池。

所以这篇文章讲一下上面三种数据库连接池的配置文件的形式。

第一种:DriverManagerDataSource:Spring内置的数据库连接池。

第一步:编写配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置spring 本身自带的数据库连接池 

这一步相当于
//1.创建数据库连接池,使用spring内置的连接池
    DriverManagerDataSource dataSource=new DriverManagerDataSource();
    //连接数据库驱动
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql:///spring3_day2");
    dataSource.setUsername("root");
    dataSource.setPassword("root");

-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 初始化dataSource里面的参数 -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql:///spring3_day2"></property>
 <property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<!-- 构建一个jdbcTemplate的模板对象 ,这个模板对象里面有dataSource这个变量,这里给他赋予初始值
这一步相当于: //2.通过连接池构造模板对象
             JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);

-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>

</bean>

</beans>

第二步:编写Junit测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")

public class Jdbctest {
    //为了注解(根据类型注解的),不加这个注释的话 Spring怎么给jdbcTemplate赋值
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void jdbcTemplatetest()
    {
        jdbcTemplate.execute("create table person2(id int primary key,name varchar(20))");

    }

}

结果:

创建了一个person2的表。

第二种:

时间: 2024-08-03 09:08:02

21Spring_JdbcTemplatem模板工具类的使用——配置文件(连接三种数据库连接池)的相关文章

Beanutils工具类,封装数据的三种方式,单例模式

org.apache.commons.beanutils.Beanutils; Beanutils setProperty(Object obj,String name,Object value) Object obj:被赋值的对象 String name:属性名 Object value:属性值 getProperty(Object obj,String name) 获取指定属性的属性值 Object obj: 对象 String name: 属性名 populate(Object bean,

20Spring_JdbcTemplatem模板工具类

JdbcTemplate 是Spring提供简化Jdbc开发模板工具类.为了更好的了解整个JdbcTemplate配置数据库连接池的过程,这篇文章不采用配置文件的方式,而是采用最基本的代码 的方式来写.后一篇文章会讲配置文件的方式. 1.Spring 对一下的持久层技术支持 2.jdbcTemplate快速入门 第一步:在项目中导入jar包 其中Spring核心包有四个,日志包有两个,测试包一个.如下 因为要用到jdbcTemplate所以还要导入以下两个jar包 spring-jdbc-3.2

Javascript定义类(class)的三种方法

注:本文转自阮一峰,觉得此篇文章对我对大家有帮助,因此转过来. 将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越庞大的项目.Javascript代码的复杂度也直线上升.单个网页包含10000行Javascript代码,早就司空见惯.2010年,一个工程师透露,Gmail的代码长度是443000行! 编写和维护如此复杂的代码,必须使用模块化策略.目

hadoop学习;自定义Input/OutputFormat;类引用mapreduce.mapper;三种模式

hadoop分割与读取输入文件的方式被定义在InputFormat接口的一个实现中,TextInputFormat是默认的实现,当你想要一次获取一行内容作为输入数据时又没有确定的键,从TextInputFormat返回的键为每行的字节偏移量,但目前没看到用过 以前在mapper中曾使用LongWritable(键)和Text(值),在TextInputFormat中,因为键是字节偏移量,可以是LongWritable类型,而当使用KeyValueTextInputFormat时,第一个分隔符前后

JDBC java数据连接 读取properties 数据库连接池 预编译

1.创建连接,下载MySQL驱动(JDBC接口的实现类,就是一个jar包) public class Demo01 { public static void main(String[] args) throws ClassNotFoundException, SQLException { //1. 注册驱动 告诉虚拟机使用的是哪个数据库软件 Class.forName("com.mysql.jdbc.Driver"); //2. 获取连接对象 Connection conn = Dri

hadoop学习;自己定义Input/OutputFormat;类引用mapreduce.mapper;三种模式

hadoop切割与读取输入文件的方式被定义在InputFormat接口的一个实现中.TextInputFormat是默认的实现,当你想要一次获取一行内容作为输入数据时又没有确定的键.从TextInputFormat返回的键为每行的字节偏移量,但眼下没看到用过 曾经在mapper中曾使用LongWritable(键)和Text(值),在TextInputFormat中,由于键是字节偏移量.能够是LongWritable类型,而当使用KeyValueTextInputFormat时,第一个分隔符前后

谈谈对于vmware Workstation 网络连接三种模式的认识.

大家好,我是黄药师,从今天开始坚持写博客. 简介: VM(VMware Workstation简称VM,后面都将用VM代替阐述)是一款功能强大的虚拟化软件.VM支持在 单一的桌面上同时运行多款不同的操作系统,能够模拟完整的网络环境,支持pxe功能.实时快照,等等多种强 大的功能.能够测试软件和部署新的业务测试.同时节约了很多资源.节约了企业成本.所以对于广大的互联 网从业者是不可多得的好工具. 本文中作者使用的软件以及操作系统版本: VM 11.1.2版本 win10 专业版 由于本文只讨论VM

【SSM整合】spring配置文件中读取配置文件的三种方式

目录 依赖 jdbc.properties applicationContext.xml(spring配置文件) 依赖 <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <

mfc 在VC的两个对话框类中传递参数的三种方法

弄了好久,今天终于把在VC中的对话框类之间传递参数的问题解决了,很开心,记录如下: 1. 我所建立的工程是一个基于MFC对话框的应用程序,一共有三个对话框,第一个对话框为主对话框,所对应的类为CTMDDDlg类.在主对话框上我放置了一个标签页(Tab Control)控件,其实现的功能是当单击标签提示A时进入页面A,即对话框A(所对应的类为CDialogChild1),单击B时进入对话框B(CDialogChild2). 整个工程的框架已经设计好了,在对话框A和对话框B上放置了许多控件,现在我想