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. 个人项目wordcount

    1.Github项目地址:https://github.com/inimble/wordcount.git 2.估算耗费的时间(如右表) PSP2.1 Personal Software Proces ...

  2. pandas - 异常值处理

    异常值概念:是指那些远离正常值的观测,即“不合群”观测.异常值的出现一般是人为的记录错误或者是设备的故障等,异常值的出现会对模型的创建和预测产生 严重的后果.当然异常值也不一定是坏事,有些情况下,通过 ...

  3. python字典的概念与基本操作

    字典是非常常用的一种数据结构,它与json格式的数据非常相似,核心就是以键值对的形式存储数据,关于Python中的字典做如下四点说明: 1.构造字典对象需要用大括号表示 {},每个字典元素都是以键值对 ...

  4. 【python系统学习17】python中的文件读写

    python中的文件读写 文件读写,是Python代码调用电脑文件的主要功能,能被用于读取和写入文本.音频片段.Excel文档.邮件以及任何保存在电脑上的东西. 可使用python批量的操作本地文件, ...

  5. better-scroll实现滚动

    通过better-scroll这个插件实现微信好友滚动列表 安装better-scroll npm i better-scroll 初始化better-scroll   //better-scroll ...

  6. js对象数组新增、修改时的验证是否重复的逻辑

    JS代码: // 定义数据集合 const persons = [ { id: 1, name: '张三' }, { id: 2, name: '李四' } ] console.log('') con ...

  7. TX-LCN 分布式事务框架

    第十章 TX-LCN 分布式事务框架 (Spring Cloud 高级) 一. 什么是分布式事务 分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位 于不同的分布式系统的不同 ...

  8. 谷歌分析(GA)新版的有哪些改变

    http://www.wocaoseo.com/thread-221-1-1.html 最近GA做了两次大规模改版,修改了GA使用率最高的traffic source.content面板以及最核心的a ...

  9. Python采集CSDN博客排行榜数据

    文章目录 前言 网络爬虫 搜索引擎 爬虫应用 谨防违法 爬虫实战 网页分析 编写代码 运行效果 反爬技术 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知 ...

  10. linux 下切换Python版本(某用户,共存,替换)

    当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Python 版本,因此系统中会存在多个 Python 的可执行二进制文件.你可以按照以下方法使用 ls 命令来查看你的系 ...