在spring-boot-starter-jdbc这个依赖包中一共分成四个部分。

  • core,JdbcTemplate等相关核心接口和类
  • datasource,数据源相关的辅助类
  • object,将基本的jdbc操作封装成对象
  • support,错误码等其他辅助工具

这里对JdbcTemplate这个类进行描述。JdbcTemplate是Spring对JDBC的封装,目的是使JDBC更加易于使用。JdbcTemplate处理了资源的建立和释放。他帮助我们避免一些常见的错误,比如忘了总要关闭连接。他运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。

JdbcTemplate提供的方法

在JdbcTemplate中执行SQL语句的方法大致分为3类。

  • execute,可以执行所有的sql语句,一般用来执行ddl语句
  • update,用于执行insert、update、delete等dml语句
  • queryXXX,用于DQL数据查询语句

execute方法

    @GetMapping("/api/jdbc-oper/createTable")
public void createTable() {
String sql = "create table product (id int primary key auto_increment,name varchar(64),description varchar(128))";
jdbcTemplate.execute(sql);
}

update方法

  @GetMapping("/api/jdbc-oper/addData")
public void addData() {
String sql = "insert into product(name) values(?)";
Arrays.asList("Java编程思想", "Spring boot in action", "spark")
.forEach(p -> {
jdbcTemplate.update(sql, p);
});
} @GetMapping("/api/jdbc-oper/updateData")
public void updateData() {
String sql = "update product set description='这是Java编程思想' where name ='Java编程思想'";
jdbcTemplate.update(sql);
} @GetMapping("/api/jdbc-oper/deleteData")
public void deleteData() {
String sql = "";
jdbcTemplate.update(sql);
}

queryXXX方法

查询一个字段的场景

    @GetMapping("/api/jdbc-oper/queryForInt")
public void queryForInt() {
String sql = "select count(*) from product";
Integer totalNum = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(totalNum);
}

查询一个普通列表的场景

    @GetMapping("/api/jdbc-oper/queryName")
public void queryName() {
String sql = "select name from product";
List<String> names = jdbcTemplate.queryForList(sql, String.class);
System.out.println(names);
}

查询一个对象列表的场景

   @GetMapping("/api/jdbc-oper/queryProduct")
public void queryProduct() {
String sql = "select * from product";
List<Product> productList = jdbcTemplate.query(sql, new RowMapper<Product>() {
@Nullable
@Override
public Product mapRow(ResultSet resultSet, int i) throws SQLException {
return Product.builder()
.id(resultSet.getInt(1))
.name(resultSet.getString(2))
.build();
}
});
productList.forEach(product -> log.info("product:{}", product)); }

批量操作的方法

 @GetMapping("/api/jdbc-oper/batchInsert")
public void batchInsert() {
String sql = "insert into product (name) values (?)";
List<String> products = Arrays.asList("product1", "product2", "product3");
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
preparedStatement.setString(1, products.get(i));
}
@Override
public int getBatchSize() {
return products.size();
}
}); }

Springboot使用JdbcTemplate的使用的更多相关文章

  1. springboot之JdbcTemplate

    springboot可以使用JdbcTemplate进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boo ...

  2. SpringBoot使用JdbcTemplate

    前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...

  3. springboot 整合jdbcTemplate

    springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: ...

  4. springboot使用jdbcTemplate连接数据库

    springboot使用jdbcTemplate连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8" ...

  5. 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)

    Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...

  6. Springboot 使用JdbcTemplate

    Springboot 使用JdbcTemplate book package com.draymonder.book.jdbc; public class Book { private Integer ...

  7. 关于SpringBoot集成JDBCTemplate的RowMapper问题

    JdbcTemplate 是Spring提供的一套JDBC模板框架,利用AOP 技术来解决直接使用JDBC时大量重复代码的问题.JdbcTemplate虽然没有MyBatis 那么灵活,但是直接使用J ...

  8. SpringBoot整合JdbcTemplate连接Mysql

    import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProper ...

  9. springboot集成jdbcTemplate

    这里使用springboot自带的jdbcTemplate连接mysql数据库 1. 添加依赖包 <!-- jdbc --> <dependency> <groupId& ...

  10. SpringBoot(四) SpringBoot整合JdbcTemplate

    一.数据准备CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `username` varchar ...

随机推荐

  1. java 数据结构(七):Collection接口

    1.单列集合框架结构|----Collection接口:单列集合,用来存储一个一个的对象* |----List接口:存储序的.可重复的数据. -->“动态”数组* |----ArrayList. ...

  2. 数据可视化之PowerQuery篇(三)学会使用PowrQuery的自定义函数

    https://zhuanlan.zhihu.com/p/64415763 使用Power Query进行复杂一些的数据处理,离不开M函数,目前已经有超过700个函数了,基本上各式各样的数据处理需求都 ...

  3. 响应式布局rem、rem方法封装、移动端响应式布局

    相信大家在做移动端的时候都会做各个手机的适配这种适配就是响应式布局在之前做网站的响应式从pc到手机用的是媒体查询 @media screen and (max-width: 300px){} 最大宽度 ...

  4. bzoj2648SJY摆棋子&&bzoj2716[Violet 3]天使玩偶*

    bzoj2648SJY摆棋子 bzoj2716[Violet 3]天使玩偶 题意: 棋盘上有n个棋子,现在有m个操作,一种是加棋子,一种是查询离某个点最近的棋子.n,m≤500000. 题解: 先将已 ...

  5. J.U.C体系进阶(一):juc-executors 执行器框架

    Java - J.U.C体系进阶 作者:Kerwin 邮箱:806857264@qq.com 说到做到,就是我的忍道! 主要内容: juc-executors 执行器框架 juc-locks 锁框架 ...

  6. Python Ethical Hacking - BACKDOORS(6)

    File Upload: A file is a series of characters. Uploading a file is the opposite of downloading a fil ...

  7. echarts 实战 : 图表竖着或横着是怎样判定的?

    这个问题比较简单. echarts 的图表默认是竖着的. 只要 xAxis 和 yAxis 互换,竖着的图就变成了横着的图了. 所以我们可以可以写一个xy轴互换的方法. reverseXYAxis = ...

  8. 爆肝整理:Linux常用命令,建议收藏!

    目录管理命令:mkdir.rmdir mkdir命令 rmdir命令 文件管理命令:cp.mv.rm cp命令 mv命令 rm命令 文件查看命令:cat.tac.head.tail.more.less ...

  9. NameBeta - 多家比价以节省咱的域名注册成本

    共收录 1584 种顶级域名,汇集互联网上 29 家知名域名注册商,每日更新价格信息 有的域名还可以查出到期时间点我前往官网 NameSilo1美元优惠码:whatz

  10. JavaScript数组在指定某个元素前或后添加元素

    //原数组 var s = [['g','g'],['h','h'],['i','i']]; //要添加的元素 var s1 = ['a','b','c']; //要添加的元素 var s2 = [' ...