JdbcTemplate是Spring框架自带的对JDBC操作的封装,目的是提供统一的模板方法使对数据库的操作更加方便、友好,效率也不错。

整合使用JdbcTemplate实现对图书的添加功能小案例

采用springboot2.0.0版本

1.导入所需依赖jar包

<!--web应用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--单测-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

2.application.properties中的配置

1 spring.datasource.url=jdbc:mysql://localhost:3306/bookshop
2 spring.datasource.username=root
3 spring.datasource.password=123
4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.entity层

 @Entity(name = "book") public class Book {
@Id
@GeneratedValue
private Integer bookid;
@Column
private String bookname;
@Column
private Integer bookprice; get set方法省略。。
}

4.service层

 @Service
public class BookService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void createUser(Integer booid,String bookname,Integer bookprice){
System.out.println("createUser");
jdbcTemplate.update("insert into book values(?,?,?);",booid,bookname,bookprice);
System.out.println("图书添加成功!!");
} }

5.controller层

 @Controller
public class BookController {
@Autowired
private BookService userService; @RequestMapping("/createUser")
public String createUser(Integer booid,String bookname,Integer bookprice){
userService.createUser(booid,bookname,bookprice);
return "success";
}
}

6.success.ftl

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>success</h1>
</body>
</html>

7.启动项目

控制台打印

SpringBoot整合使用JdbcTemplate的更多相关文章

  1. springboot 整合jdbcTemplate

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

  2. SpringBoot整合持久层技术--(一)JdbcTemplate

    简介: JdbcTemplate是Spring提供的一套JDBC模板框架,利用AOP技术解决直接使用JDBC带来的重复代码问题.它没有MyBatis使用那么灵活,但是却比直接使用JDBC方便得多.Sp ...

  3. SpringBoot 整合 Mybatis + Mysql——XML配置方式

    一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...

  4. Java学习之SpringBoot整合SSM Demo

    背景:在Java Web中Spring家族有着很重要的地位,之前JAVA开发需要做很多的配置,一堆的配置文件和部署调试一直是JavaWeb开发中的一大诟病,但现在Spring推出了SpringBoot ...

  5. 三、SpringBoot 整合mybatis 多数据源以及分库分表

    前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程.但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现.我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接 ...

  6. springboot整合spring Data JPA

    今天敲代码,一连串的错误,我也是服气~果然,我们不是在出bug,就是在找bug的路上…… 今天完成的是springboot整合spring data JPA ,出了一连串的错,真是头大 java.sq ...

  7. springBoot 整合 mybatis 项目实战

    二.springBoot 整合 mybatis 项目实战   前言 上一篇文章开始了我们的springboot序篇,我们配置了mysql数据库,但是我们sql语句直接写在controller中并且使用 ...

  8. SpringBoot整合开发

    1.SpringBoot分模块 分模块就是将一个项目分成多个模块,即maven项目. 1)首先创建一个springboot的项目: 第一步:选择springboot的项目 第二步:填写项目的相关信息, ...

  9. 3、SpringBoot整合之SpringBoot整合JDBC

    SpringBoot整合JDBC 一.创建SpringBoot项目 选择Spring Web.JDBC API.MySQL Driver 二.在pom配置文件中修改JDBC版本,导入lombok &l ...

随机推荐

  1. Microsoft Internet Explorer v11 / XML External EntityInjection 0day

    [+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org[+] Source:  http://hyp3 ...

  2. JS判断页面是在浏览器还是微信打开

    一.Navigator对象 1.获取用户的浏览器信息. let ua = navigator.userAgent.toLowerCase(); 打印一下ua的结果: Mozilla/5.0 (Maci ...

  3. 为什么ArrayList、LinkedList线程不安全,Vector线程安全

    ArrayList源码 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! ele ...

  4. 2018-2019-2 20165325 《网络对抗技术》 Exp5:MSF基础应用

    2018-2019-2 20165325 <网络对抗技术> Exp5:MSF基础应用 实验内容(概要) 1.1 一个主动攻击实践,本实验选择 ms17_010_eternalblue(成功 ...

  5. expect 批量自动部署ssh 免密登陆 之 三

    #!/bin/expect -- ########################################## zhichao.hu #Push the id.pas.pub public k ...

  6. Jenkins构建自动化脚本执行无界面解决方法

    场景: jenkins构建selenium自动化用例的时候,会有jenkins自带服务后台运行自动化脚本,可无界面运行IE.Chrome.Firefox. 然而运行IE浏览器时候(IE比较特殊),Je ...

  7. Linux中Too many open files

    1.ulimit –a open files一项就是默认的句柄数,最大为 65536 2.修改最大open files /etc/security/limits.conf文件中,加入以下配置: * s ...

  8. Lesson 1-2

    1.5 模块 模块可视为扩展,通过将其导入可以扩展python的功能.python中自带有一组模块,也称为“标准库”. 1.5.1 模块的导入:import + 模块名称 • 使用关键字import导 ...

  9. Ajax原理一篇就够了

    Ajax原理一篇就够了 一.什么是Ajax Ajax(Asynchronous JavaScript and XML的缩写)是一种异步请求数据的web开发技术,对于改善用户的体验和页面性能很有帮助.简 ...

  10. python复习基本知识

    print(x or y) """x or y ; x 为 true ,则返回x ,否则返回flase“”“” and 则相反 优先级比较:not > and &g ...