Spring 链接数据库
一、前言
Spring 现在是我们在做 JavaWeb 开发中,用的最主流的框架。以后是不是我们暂时不知道,但现在是。废话不多我就介绍 Spring 中。链接数据库的三种方式: git源码地址 需要的自行下载。
二、Spring 默认链接数据库方式(java 代码)
导入的 JAR 有如下:

Spring 默认的链接数据库代码:
package com.springjdbc.service; import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; /**
* 默认 spring 链接数据库的方式
* @author TongZhou
*
*/
public class SpringJDBCService { /**
* 使用 Spring 默认的数据库方式
*/
@Test
public void JDBCTest(){ //创建数据库链接的数据源
DriverManagerDataSource dataSource=new DriverManagerDataSource(); //设置数据库的链接信息
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///adminmanger");
dataSource.setUsername("root");
dataSource.setPassword("123456"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table user(id int primary key auto_increment,name varchar(20))");
}
}
Test效果:

在数据库中,生成了表:

这是 Spring 在内部集成的 所以它应用的 dataSource 的包名是 import org.springframework.jdbc.datasource.DriverManagerDataSource。我们使用了 Spring 。就不用去在 实例化有关类了,配置就好。那么我们用 Spring 的方式去解决问题。
三、Spring 默认链接数据库方式(配置文件)
配置文件如下: 在项目中 src -->applicationContext.xml 内部代码如下:
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 事物的配置 -->
<!-- spring 中的数据持久层的代码 -->
<!-- spring 创建dataSource spring内置的连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///adminmanger"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean> </beans>
测试代码如下:
package com.springjdbc.service; 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 TongZhou
*
*/
// 使用 Spring 的 JUnit 的测试
//通过 ContextConfiguration 读取配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringJDBCService1 { @Autowired
//注入 从数据库中配置 id="jdbcTemplate"
@Qualifier("jdbcTemplate")
//数据库链接对象
private JdbcTemplate jdbcTemplate; /**
* 创建数据库
*/
@Test
public void dome(){ //执行 SQL
jdbcTemplate.execute("create table dashuju (id int primary key auto_increment,name varchar(20))");
}
}
结果如下:

四、Spring 链接数据库(dbcp连接池)
Spring 除了自己可以链接数据库以外,他还引入了第三方的插件如 dbcp 、c3p0 连接池。同时这个连接池也借助了 Spring 这个平台在被广泛使用。
dbcp 需要引入的 JAR 有:
1. com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
2. com.springsource.org.apache.commons.pool-1.5.3.jar
在 XML 中的配置如下:
<!-- dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///adminmanger"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
dbcp 和默认的 配置的比较如下:

五、Spring 链接数据库 (C3P0 连接池)
配置如下:
<!-- c3p0连接池的使用 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///adminmanger"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
效果截图:

六、总结
通过学习 Spring 这四种链接数据库做法,感觉受益匪浅。通过 Spring 的管理,我们可以省略了许多的代码。一个字就是 “爽”。git源码地址
Spring 链接数据库的更多相关文章
- Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- spring框架使用c3po链接数据库
编辑工具:idea 1.配置pom.xml文件(创建模板时软件自动创建) 导入spring的核心架包 全部架包官网:https://mvnrepository.com/ 1 <dependenc ...
- 4、原生jdbc链接数据库常用资源名
原生jdbc链接数据库要素:#MySql:String url="jdbc:mysql://localhost:3306/数据库名";String name="root& ...
- Druid链接池配置加密密码链接数据库
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...
- Spring的数据库编程浅入浅出——不吹牛逼不装逼
Spring的数据库编程浅入浅出——不吹牛逼不装逼 前言 上文书我写了Spring的核心部分控制反转和依赖注入,后来又衔接了注解,在这后面本来是应该写Spring AOP的,但我觉得对于初学者来说,这 ...
- PHP学习-链接数据库
链接数据库文件:conn.php <?php $conn = mysql_connect("localhost:3306","root","us ...
- PHP 链接数据库1(连接数据库&简单的登录注册)
对 解析变量的理解 数据库的名称和表的名称不能重复 从结果中取出的数据 都是以数组的形式取出的 1.PHP查询数据库中的某条信息 //PHP链接数据库 /*1.造链接对象 IP地址 用户名 密码 ...
- JDBC的使用(一):引用外部jar;代码链接数据库
一:引用外部jar 1.首先不jar文件放到项目下: 2.在Eclipse中,右键相应的项目--构建路径--配置构建路径--库--添加外部jar:选中-打开-应用-确定. 二:代码链接数据库 1.加载 ...
- Connect to Database Using Custom params链接数据库配置参数说明
使用RF的关键字Connect to Database Using Custom params链接数据库,对应的参数说明: a) 第一个参数我使用的是cx_Oracle,就写这个 b) ...
随机推荐
- mysql数据库第二弹
mysql数据库针对表的操作 表记录的增删改查 1.增加一张表 插入记录之前必须得先有表结构! CREATE TABLE score( id int PRIMARY KEY auto_incremen ...
- [转]DBCC (Transact-SQL)
http://msdn.microsoft.com/zh-cn/library/ms188796.aspx Transact-SQL 编程语言提供 DBCC 语句以作为 SQL Server 的数据库 ...
- 隐藏17年的Office远程代码执行漏洞(CVE-2017-11882)
Preface 这几天关于Office的一个远程代码执行漏洞很流行,昨天也有朋友发了相关信息,于是想复现一下看看,复现过程也比较简单,主要是简单记录下. 利用脚本Github传送地址 ,后面的参考链接 ...
- SSH框架基础
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...
- 你知道android的MessageQueue.IdleHandler吗?
WeTest 导读 干货!干货!或许可以是一种处理问题的新思路哟! 前言 我们知道android是基于Looper消息循环的系统,我们通过Handler向Looper包含的MessageQueue投递 ...
- 如何用while循环 输出一个九九乘法表
方法一 i = 1 while i < 10: k = 1 while k <= i: print('%d*%d=%2d '% (i,k,i*k),end='') #end='' 表示不换 ...
- lua API函数大全
Lua5.1中的API函数 lua_State* luaL_newstate()Lua脚本的编译执行是相互独立的,在不同的线程上执行.通过luaL_newstate()函数可以申请一个虚拟机,返回指针 ...
- 用python实现一个简单的词云
对于在windows(Pycharm工具)里实现一个简单的词云还是经过了几步小挫折,跟大家分享下,如果遇到类似问题可以参考: 1. 导入wordcloud包时候报错,当然很明显没有安装此包. 2. 安 ...
- 解决thymeleaf layout布局不生效
今天使用thymeleaf layout布局时总是不生效,特此把解决问题的步骤和几个关键点记录下来备忘. 一.检查依赖 1.thymeleaf必备maven依赖: <dependency> ...
- 人体姿态的相似性评价基于OpenCV实现最近邻分类KNN K-Nearest Neighbors
最近学习了人体姿态的相似性评价.需要用到KNN来统计与当前姿态相似的k个姿态信息. 假设我们已经有了矩阵W和给定的测试样本姿态Xi,需要寻找与Xi相似的几个姿态,来估计当前Xi的姿态标签. //knn ...