SpringBoot整合使用JdbcTemplate
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的更多相关文章
- springboot 整合jdbcTemplate
springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: ...
- SpringBoot整合持久层技术--(一)JdbcTemplate
简介: JdbcTemplate是Spring提供的一套JDBC模板框架,利用AOP技术解决直接使用JDBC带来的重复代码问题.它没有MyBatis使用那么灵活,但是却比直接使用JDBC方便得多.Sp ...
- SpringBoot 整合 Mybatis + Mysql——XML配置方式
一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...
- Java学习之SpringBoot整合SSM Demo
背景:在Java Web中Spring家族有着很重要的地位,之前JAVA开发需要做很多的配置,一堆的配置文件和部署调试一直是JavaWeb开发中的一大诟病,但现在Spring推出了SpringBoot ...
- 三、SpringBoot 整合mybatis 多数据源以及分库分表
前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程.但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现.我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接 ...
- springboot整合spring Data JPA
今天敲代码,一连串的错误,我也是服气~果然,我们不是在出bug,就是在找bug的路上…… 今天完成的是springboot整合spring data JPA ,出了一连串的错,真是头大 java.sq ...
- springBoot 整合 mybatis 项目实战
二.springBoot 整合 mybatis 项目实战 前言 上一篇文章开始了我们的springboot序篇,我们配置了mysql数据库,但是我们sql语句直接写在controller中并且使用 ...
- SpringBoot整合开发
1.SpringBoot分模块 分模块就是将一个项目分成多个模块,即maven项目. 1)首先创建一个springboot的项目: 第一步:选择springboot的项目 第二步:填写项目的相关信息, ...
- 3、SpringBoot整合之SpringBoot整合JDBC
SpringBoot整合JDBC 一.创建SpringBoot项目 选择Spring Web.JDBC API.MySQL Driver 二.在pom配置文件中修改JDBC版本,导入lombok &l ...
随机推荐
- [Reinforcement Learning] Cross-entropy Method
Cross-entropy Method(简称CEM)虽然是一种基于交叉熵的算法,但并不是我们熟知的监督学习中的交叉熵方法,与其说它是一种基于交叉熵的算法,倒不如说是一种基于蒙特卡洛和进化策略的算法. ...
- jquery弹出窗口选择回写值
$(document).ready(function(){ $('.sel').dblclick(function(){ var nowid=$(this).attr('id'); window.op ...
- 微信小程序中使用 <web-view> 内嵌 H5 时,登录问题的处理方法
在微信小程序的开发中,经常遇到需要使用 <web-view></web-view> 内嵌 H5 的需求.在这种需求中比较棘手的问题应该就是登录状态的判断了,小程序中的登录状态怎 ...
- 关于HashSet集合add元素
HashSet集合add元素底层实现使用的是HashMap. 简单记忆:无论HashMap put元素还是HashSet add元素,都先调用hashCode()方法,若hashCode方法返回值不同 ...
- web前端效率提升-nginx+nodejs搭建本地生态
1.起因 编写的项目是一个偏向于后台管理的web系统,使用了angular框架,在绑定数据的时候就依赖于后台的接口格式. 以前是后台写好接口后,我在绑定,在这之前一些逻辑是没法做的,有时候后台接口给的 ...
- FATAL ERROR: please install the following Perl modules before executing
运行安装mysql 报错 [root@localhost mysql-mult]# ./scripts/mysql_install_db --defaults-file=conf/3306my.cn ...
- git知识总结-2.git基本操作之操作汇总
0.前言 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 上图分别为: Workspace:工作区 Index / Stage:暂存区 Reposito ...
- QT学习之解决QT中QIcon图标不显示的问题
第一种:图标存放目录问题 :/文件夹名称/文件名 如:文件夹为:/img 文件名为:a.png 路径: :/img/a.png 这里注意前面的":". 第二种:编译生成 ...
- Xshell 连接虚拟机出现 "The remote SSH server rejected X11 forwarding request"
1. 描述 虚拟机:VirtualBox Linux: centOS7 解决了 centOS7在VirtualBox中装好后的网络连接问题 后,用 Xshell 连接服务器时出现下面情况: 2. ss ...
- 小程序如何传数组数据到vs后台中
首先小程序要跟vs运行的状态打通,首先要修改配置,也就是说调试的时候,小程序一使用Post请求后台的方法时就能让vs进入调试状态. 1.修改vs中的配置,注意这个.vs文件,如图: 找到这个文件 然后 ...