Spring Jdbc 框架整合的第一天
Spring Jdbc的概述
它是Spring框架的持久层子框架。用于对数据库的操作
什么是数据库的操作?
答:对数据库的增删改查
在使用Spring Jdbc框架,要用到一个类---->JdbcTemplate,他是spring jdbc 子框架中提供的一个操作类,用于对原始jabc API的简单封装
那么如何创建Template呢?
首先看一下它的源码:
public JdbcTemplate() {
}
/**
* Construct a new JdbcTemplate, given a DataSource to obtain connections from.
* <p>Note: This will not trigger initialization of the exception translator.
* @param dataSource the JDBC DataSource to obtain connections from
*/
public JdbcTemplate(DataSource dataSource) {
setDataSource(dataSource);
afterPropertiesSet();
}
/**
* Construct a new JdbcTemplate, given a DataSource to obtain connections from.
* <p>Note: Depending on the "lazyInit" flag, initialization of the exception translator
* will be triggered.
* @param dataSource the JDBC DataSource to obtain connections from
* @param lazyInit whether to lazily initialize the SQLExceptionTranslator
*/
public JdbcTemplate(DataSource dataSource, boolean lazyInit) {
setDataSource(dataSource);
setLazyInit(lazyInit);
afterPropertiesSet();
}
在此段代码中我们可以看出要创建一个jdbcTemplate 对象,需要获得一个数据库连接的数据源
如何获得一个数据库连接的数据源呢?

org.springframework.jdbc.datasource.DriverManagerDataSource,这个路径位于jdbc驱动包中
再配置springjdbc的四要素:

实现代码:
配置文件代码:
<?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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!--配置一个数据源
由于jjabc模块提供的DriverManagerDataSource数据源,是一个直连数据源,
所以每次访问数据库,都要去打开连接才能访问,效率不高!
所以引入了连接池的概念。。。
连接池中的四个重要属性
1.最大超时毫秒数
2.最大连接数
3.最小连接数
4.最大空闲时间 destroy-method:在连接池对象注销之前,先关闭数据库(关闭连接池) -->
<!-- 数据源的作用:获得数据库的链接,从而引出springJDBC的四要素 -->
<bean name="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<!-- 配置springJDBC四要素 -->
<!-- 驱动 -->
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"></property>
<!-- 连接字符串 -->
<property name="url" value="jdbc:mysql://localhost:3306/sms"></property>
<!-- 用户名 -->
<property name="username" value="root"></property>
<!-- 密码 -->
<property name="password" value="1234"></property>
<!-- 最大超时毫秒数 -->
<property name="maxWaitMillis" value="30000"></property>
<!-- 最大连接数 -->
<property name="maxTotal" value="100"></property>
<!-- 最小连接数 -->
<property name="initialSize" value="50"></property>
<!-- 最大空闲连接 -->
<property name="maxIdle" value="55"></property>
</bean> <!-- 获得JDBCTemplate操作对象 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 设置 -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置dao -->
<bean name="studentDAO" class="cn.gzsxt.dao.StudentDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> </beans>
dao层:
package cn.gzsxt.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import cn.gzsxt.pojo.Student;
public class StudentDAO implements DAO<Student>{
private JdbcTemplate JdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
JdbcTemplate = jdbcTemplate;
}
@Override
public int insert(Student entity) {
// TODO Auto-generated method stub
//事物的操作(增删改)都可以是update方法
Object [] objects = {entity.getStuName(),entity.getStuPwd()};
int update = JdbcTemplate.update("INSERT INTO t_student(stuName,stuPwd) VALUES(?,?)", objects);
return update;
}
}
pojo层
package cn.gzsxt.pojo;
import java.io.Serializable;
public class Student implements Serializable{
/**什么是序列化?
* 将对象通过流的形式写出去
* 反序列化
* 读取流形式的数据
*
*/
private static final long serialVersionUID = 114525114257637298L;
private int stuId;
private String stuName;
private String stuPwd;
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuPwd() {
return stuPwd;
}
public void setStuPwd(String stuPwd) {
this.stuPwd = stuPwd;
}
public Student(int stuId, String stuName, String stuPwd) {
super();
this.stuId = stuId;
this.stuName = stuName;
this.stuPwd = stuPwd;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
测试代码:
package cn.gzsxt.test; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.gzsxt.dao.StudentDAO;
import cn.gzsxt.pojo.Student; public class ApplicationContextTest {
private StudentDAO studentDAO;
private ClassPathXmlApplicationContext conn; @Before
public void Before(){
conn = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获得数据源对象
studentDAO = conn.getBean(StudentDAO.class);
} @After
public void after(){
conn.close();
} /**
* junit代替main方法,使得类能同时有多个main方法的功能
* 使用时要注意,返回值必须为空,为void
*/
// @Test
// public void dataSource(){
// //通过配置文件创建容器
// ClassPathXmlApplicationContext conn = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//
// //获取数据源对象
// DataSource dataSource = conn.getBean(DataSource.class);
//
// try {
// //打印数据库链接地址,测试连接是否成功
// System.out.println(dataSource.getConnection());
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
@Test
public void insert(){
Student entity = new Student();
entity.setStuName("王八");
studentDAO.insert(entity); }
}
spring jdbc 的优化!
由于jdbc模块提供的DriverManagerDataSource数据源,是一个直连数据源,所以每次访问数据库,都要去打开连接才能访问,效率不高,所以引入了连接池的概念,连接池的四个重要属性:
1.最大超时毫秒数
2.最大连接数
3.最小连接数
4.最大空闲时间

如何关闭连接池呢?
destroy-method:在连接池对象注销之前,先关闭数据库(关闭连接池)
Spring Jdbc 框架整合的第一天的更多相关文章
- RabbitMQ与Spring的框架整合之Spring Boot实战
1.RabbitMQ与Spring的框架整合之Spring Boot实战. 首先创建maven项目的RabbitMQ的消息生产者rabbitmq-springboot-provider项目,配置pom ...
- Spring JDBC 框架使用JdbcTemplate 类的一个实例
JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...
- struts2 + spring + mybatis 框架整合详细介绍
struts2 + spring + mybatis 框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...
- ref:Spring JDBC框架
ref:https://blog.csdn.net/u011054333/article/details/54772491 Spring JDBC简介 先来看看一个JDBC的例子.我们可以看到为了执行 ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- 11.Spring——JDBC框架
1.DBC 框架概述 2.Spring JDBC 示例 3.Spring 中 SQL 的存储过程 1.DBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关 ...
- Spring JDBC 框架 简介
在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等. 但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常 ...
- spring三大框架整合
Spring概述 Spring介绍 Spring它是一个一站式的分层轻量级框架. Spring体系结构 1. core container a) beans与core ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
随机推荐
- mac 上安装 openJDK11
紧接上篇,mac现在基本上作为开发者的主力机,当然也要安装jdk的 首先需要卸载原来的jdk8,如下: ls /Library/Java/JavaVirtualMachines/ sudo rm -r ...
- iOS中UITextField输入判断小数点后两位
在输入金额的UITextField中,要给予三个规则的判断 1. 只能输入数字(可以通过设置键盘类型为Decimal Pad) 2. 小数点只能有一个 3. 小数点后最多有两位数字 (可以通过正则表达 ...
- .net core 2.0 虚拟目录下载 Android Apk 等文件
当Android 文件 Apk 放在Asp.net core wwwroot 虚拟目录下面.访问是 404,设置Content-Type类型 app.UseStaticFiles(); //设置实际目 ...
- Win10连接远程桌面的时候提示您的凭证不工作该怎么办?
Win10连接远程桌面的时候提示您的凭证不工作该怎么办?Win10连接远程桌面的时候,提示“您的凭证不工作”.原有保存的远程帐号密码无法使用,导致远程登录系统失败.我这里总结下自己解决的方法,分享给大 ...
- python 搭建一个http服务的小例子
一.创建Server 1.Dos 命令 python -m BaseHTTPServer [port] 默认端口是8000, 2.Python 脚本启动 #coding:utf-8 ''' Creat ...
- CreateWindowEx failed (当前程序已使用了 Window 管理器对象的系统允许的所有句柄。)
我在QT图形场景视图中通过QGraphicsProxyWidget添加代理Widget(实现添加基本的QT Widget,如按钮.复选框.日期时间控件等),当数量超过3500左右的时候,QT应用程序直 ...
- linux环境,通过rpm删除mysql包,报错:error reading information on service mysqld: Invalid argument
问题描述: 今天在做saltstack的练习,想要通过sls的方式,在远程进行mysql数据库的安装,发现无法通过service的方式启动数据库,然后就想给删除了重新进行安装,在通过rpm -e进行删 ...
- 1开放封闭原则OCP
一.什么是开放封闭原则 开放封闭原则(Open-Closed Principle):一个软件实体 应当对扩展开放,则修改关闭. 在设计一个模块时,应当使得这个模块可以在不被修 改的前提下被扩展.也就是 ...
- Visual Studio 2015编译wxWidgets
宫指导说,换帅如换刀 程序员的编译器一换,基本套路必须都重练几次 使用wxWidgets并不难,但不能使用现有的库和工程配置文件,细节就必须理清楚 获取wxWidgets 官方的下载页面,下7z或zi ...
- C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
报错信息: 最近打开下载的 Excel,会报如下错误.(xls 格式不受影响) 解决方案: 下载代码(红色为新添代码) public void download() { string fileName ...