Spring系列之 jdbcTemplate

啥是jdncTemplate?

t他是spring框架中提供的一个对象,是对原始的jdbcAPI对象的简单封装,spring框架为我们提供了很多操作,模板类,比如操作关系型数据库的jdbcTemplate,操作nosql数据库的Redis Template,操作消息队列的jmsTemplate等等

JdbcTemplate开发步骤

1.导入sprign-jdbc和spring-tx坐标

2.创建数据库表和实体

3.创建JdbcTemplate对象

4.执行数据库操作

1.导入sprign-jdbc和spring-tx坐标

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>

2.创建数据库表和实体

使用sqlyog创建一个表

语句

CREATE TABLE test1(
id INT,
NAME VARCHAR(10)
);

创建实体

package com.pjh;
public class user {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

JbdcTemplate快速入门*,不使用spring框架的时候

@Test
public void test1() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("1234");
//创建jdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(comboPooledDataSource);
//执行语句
jdbcTemplate.update("insert into test1 values(?,?)",10,"one");
}

结果



抽取配置文件

配置文件代码:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3309/one
jdbc.name=root
jdbc.password=1234

测试函数操作

@Test
public void test3() throws PropertyVetoException {
//读取配置文件
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
//获取连接池
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//设置参数
comboPooledDataSource.setDriverClass(jdbc.getString("jdbc.driver"));
comboPooledDataSource.setJdbcUrl(jdbc.getString("jdbc.url"));
comboPooledDataSource.setUser(jdbc.getString("jdbc.name"));
comboPooledDataSource.setPassword(jdbc.getString("jdbc.password"));
//创建jdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(comboPooledDataSource);
jdbcTemplate.update("insert into test1 values(?,?)",13,"three"); }

使用spring创建JdbcTemplate对象

将数据源DataSource与JdbcTemplate的创建权交给Spring并在Spring容器内进行依赖注入

配置代码:


<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3309/one"/>
<property name="user" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>

测试函数

@Test
public void test2(){
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate =(JdbcTemplate) classPathXmlApplicationContext.getBean("jdbcTemplate");
jdbcTemplate.update("insert into test1 values(?,?)",11,"two");
}

结果

成功插入

这个也可以使用读取配置文件的方式

我们首先要导入context的约束路径与命名空间

命名空间: xmlns:context="http://www.springframework.org/schema/context"

约束路径:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

配置文件修改

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.name}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
</beans>

测试代码

 @Test
public void test4(){
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate =(JdbcTemplate) classPathXmlApplicationContext.getBean("jdbcTemplate");
jdbcTemplate.update("insert into test1 values(?,?)",100,"pjh");
}

结果

成功插入

通过注解的方式来得到JdbcTemplate

使用框架

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test7(){
jdbcTemplate.update("insert into test1 values(?,?)",110,"GGB"); }

不使用框架

public void test1() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("1234");
//创建jdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(comboPooledDataSource);
//执行语句
jdbcTemplate.update("insert into test1 values(?,?)",10,"one");
}

由二者对比即可看出框架的巨大好处,上面那么长的代码现在只要几行即可解决

JDBCTemplate的常用操作

查询语句

查询数据库中的所有内容

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test8(){
String sql="select * from test1 where name=?";
List<user> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<user>(user.class));
for (user user : query) {
System.out.println(user);
}
}

结果

查询数据库中的某条内容

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
@Test
public void test9(){
String sql="select * from test1 where id=?";
List<user> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<user>(user.class), 10);
for (user user : query) {
System.out.println(user);
}
}
}

查询数据库记录的数量

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test90(){
String sql="select count(*) from test1";
Long aLong = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println("记录条数:"+aLong);
}
}

删除指定记录

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test11(){
String sql="delete from test1 where id=11";
jdbcTemplate.update(sql);
}
}

以上就是Spring jdbc操作的一些知识,我会不断的学习,也会不断更新我的学习文章,主要有java和数据结构两个方面,有想要一起学习的伙伴可以私信或则关注我,共勉

Spring 系列之jdbcTemplate的使用的更多相关文章

  1. Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源

    多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...

  2. Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate

    在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...

  3. Spring系列

    Spring系列之访问数据库   阅读目录 一.概述 二.JDBC API的最佳实践 三.Spring对ORM的集成 回到顶部 一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结 ...

  4. Spring系列(六):Spring事务源码解析

    一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...

  5. Spring系列之初识Spring Spring概述

    初始Spring 啥是Spring? 下面这个就是Spring Spring当然不是上面那个Spring,Spring之所以命名为Spring是因为这个开源的轻量级的开源框架的出现给软件行业带来了春天 ...

  6. Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级

    Spring系列之事务的控制 注解实现+xml实现 在前面我写过一篇关于事务的文章,大家可以先去看看那一篇再看这一篇,学习起来会更加得心应手 链接:https://blog.csdn.net/pjh8 ...

  7. Spring系列之JDBC对不同数据库异常如何抽象的?

    前言 使用Spring-Jdbc的情况下,在有些场景中,我们需要根据数据库报的异常类型的不同,来编写我们的业务代码.比如说,我们有这样一段逻辑,如果我们新插入的记录,存在唯一约束冲突,就会返回给客户端 ...

  8. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  9. Spring 系列: Spring 框架简介

    Spring AOP 和 IOC 容器入门(转载) 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级的.强壮的 J2EE 应用程序.dev ...

随机推荐

  1. 看视频常见的 720p、1080p、4k,这些分辨率到底包含了什么

    从早期的420p,到后来的720p,到现在的非1080p不看.视频的清晰度飞快提升,但是在看到色彩越来越丰富清晰度越来越高的画面时,你有关注过他们的到底是怎么做到的么?我们一起来了解一下吧. 想必大家 ...

  2. CKA认证经验贴(认证日期:20200817)

    一.背景 由于年初疫情影响,身处传统IT行业且兼职出差全国各地“救火”的我有幸被领导选中调研私有云平台,这就给我后来的认证之路做下了铺垫.之前调研kubernetes的v1.17版本自带kubeadm ...

  3. 开源流数据公司 StreamNative 推出 Pulsar 云服务,推进企业“流优先”进程

    Apache 顶级项目 Pulsar 背后的开源流数据公司 StreamNative 宣布,推出基于 Apache Pulsar 的云端服务产品--StreamNative Cloud.该产品的发布, ...

  4. Google谷歌在根据流量统计分析当年的2008年汶川大地震

    这是一张2008年的老图,Google当时的博文说道:"当我们依照惯例整理和分析谷歌搜索引擎的流量数据时,一条从未见过的曲线出现在我们面前.当意识到发生了什么事情时,我们的眼睛湿润了.&qu ...

  5. Android Home

    unity处理Android的home键响应事件 http://blog.csdn.net/tianyongheng/article/details/38459531 Unity3D教程:处理Home ...

  6. 非IT行业大企程序员讲述MIS系统开发案例

      雪莉叹了一口气,调整了一下被汗水濡湿的刘海,然后向后靠在办公椅中,伸手在电脑键盘上输入了一些内容, 最后拿起印刷着房地产广告的扇子,边扇风边等待着.   她的工位在办公室的东侧角落,侧靠着窗.此时 ...

  7. 【问题】Java和Scala混合编译下无法正常使用lombok的问题

    工作中有java和scala和混合编译的工程,最近遇到一个问题,就是工程中有依赖java bean的scala文件,编译过程中发现编译器无法找到Java bean 中 lombok生成的getter, ...

  8. Nginx之https配置

    14.1. 对称加密 安全隐患:钥匙除我之外,还有多个人拥有.泄露风险较大,钥匙传递的过程风险较大 14.2. 非对称加密 优缺点:私钥很安全.但是非对称算法开销很大,大批量应用于业务,会导致性能成本 ...

  9. 出现jupyter notebook password or token提示需要token的处理方法

    很多朋友不知道下面的情况怎么处理,我给大家介绍一个方法! 出现这种情况很简单用下面这个地址就能进去了 (注意是你自己的 不是我这个)

  10. 【Flutter 实战】1.20版本更新及新增组件

    老孟导读:Flutter 1.20 更新了 Slider.RangeSlider.日期选择器组件.时间选择器组件的样式,新增了交换组件:InteractiveViewer,下面详细介绍其用法. 滑块 ...