Spring 系列之jdbcTemplate的使用
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的使用的更多相关文章
- Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...
- Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...
- Spring系列
Spring系列之访问数据库 阅读目录 一.概述 二.JDBC API的最佳实践 三.Spring对ORM的集成 回到顶部 一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结 ...
- Spring系列(六):Spring事务源码解析
一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...
- Spring系列之初识Spring Spring概述
初始Spring 啥是Spring? 下面这个就是Spring Spring当然不是上面那个Spring,Spring之所以命名为Spring是因为这个开源的轻量级的开源框架的出现给软件行业带来了春天 ...
- Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级
Spring系列之事务的控制 注解实现+xml实现 在前面我写过一篇关于事务的文章,大家可以先去看看那一篇再看这一篇,学习起来会更加得心应手 链接:https://blog.csdn.net/pjh8 ...
- Spring系列之JDBC对不同数据库异常如何抽象的?
前言 使用Spring-Jdbc的情况下,在有些场景中,我们需要根据数据库报的异常类型的不同,来编写我们的业务代码.比如说,我们有这样一段逻辑,如果我们新插入的记录,存在唯一约束冲突,就会返回给客户端 ...
- Spring 系列: Spring 框架简介 -7个部分
Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...
- Spring 系列: Spring 框架简介
Spring AOP 和 IOC 容器入门(转载) 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级的.强壮的 J2EE 应用程序.dev ...
随机推荐
- 团队作业3 需求改进&系统设计(银河超级无敌舰队)
目录 一.需求&原型改进 1. 需求改进 2. 修改说明书 3.功能分析 4. 调整WBS及计划 二.系统设计 1. 总体设计 2. 数据库设计 3.社团设计 三.Alpha任务分配计划 1. ...
- A Distributional Perspective on Reinforcement Learning
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! arXiv:1707.06887v1 [cs.LG] 21 Jul 2017 In International Conference on ...
- 牛客网PAT练兵场-打印沙漏
题目地址:https://www.nowcoder.com/pat/6/problem/4053 题意:模拟题 /** * Copyright(c) * All rights reserved. * ...
- flutter 制作一个用户登录页面
flutter 制作一个用户登录页面 用户登录效果图如下: 登录页面如下: import 'package:flutter/material.dart'; import 'package:flutte ...
- seo兼职顾问多少钱
http://www.wocaoseo.com/thread-199-1-1.html 随着近几年搜索引挚市场迅猛的发展,网络营销已成为企业销售的一大趋势,越来越多的企业开始投身于网络市 ...
- 关于华为服务器 双路E52620安装系统时遇到的问题
一 准备好u盘启动盘,centos7的,用UltraISO制作启动盘 双击图标 进入 点击启动下的刻录磁盘映像 几分钟之后制作完场 二 :按装centos7的系统 1 首先将 u盘插到服务器上 2 ...
- React技术实践(1)
随着系统越来越庞大,前端也变得越来越复杂,因此,构建一套组件化的前端变得很重要了. 之前一直在使用Asp.net来进行前端的组件化,Asp.net组件化有个很大的缺陷,就是和后台代码绑定太紧密了,不符 ...
- Canalv1.1.4版本搭建HA集群
前提 Canal上一个正式版是于2019-9-2发布的v1.1.4,笔者几个月前把这个版本的Canal推上了生产环境,部署了HA集群.过程中虽然遇到不少的坑,但是在不出问题的前提下,Canal的作用还 ...
- Currency Exchange(SPFA判负环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Kubernetes K8S之资源控制器Job和CronJob详解
Kubernetes的资源控制器Job和CronJob详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2 ...