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 ...
随机推荐
- avg 的使用
select * from emp where sal>(select avg(sal) as ssalfrom emp);--要求查询出高于公司平均工资的全部雇员信息
- 软件测试自动化…python学习到什么程度?代码好不好学!
软件测试自动化…python学习到什么程度?代码好不好学! 如下:
- 项目Alpha冲刺(1/10)
1.项目燃尽图 2.今日进度描述 项目进展 熟悉Android Studio的基本使用,阅读代码规范 问题困难 学习中存在许多问题. 心得体会 应该选择一个自己熟悉的平台进行开发. 3.会议照片 4. ...
- Access denied for user ‘root’@‘localhost’(using password: YES)的解决方法
https://www.jb51.net/article/133836.htm 搭建服务器之后只能看见test与infomation_schema两个库 https://www.cnblogs.com ...
- appniu踩坑
1.pyCharm识别不到appnium-python-client 解决:新建项目注意选择环境,查看Project Interpreter中是否识别到了appnium-python-client 还 ...
- tar解压指定文件
import tarfileimport sys#tar = tarfile.open('/opt/platform-omp/omp.tar.gz','r')tar = tarfile.open(r' ...
- Idea中一些常用设置
idea展开和折叠方法的快捷键 Ctrl+”+/-”,当前方法展开.折叠Ctrl+Shift+”+/-”,全部展开.折叠 idea中也有自定代码块的功能 //region 描述.....业务代码//e ...
- 自定义QGraphicsItem
简述: QGraphicsItem 是场景中 item 的基类.图形视图提供了一些典型形状的标准 item,例如:矩形 ( QGraphicsRectItem ).椭圆 ( QGraphicsElli ...
- MySQL(三)
一.创建库 1.创建一个库: create database 数据库名;create database 库名 character set 编码; mysql> create database m ...
- vue 统一处理token失效问题
使用http response 拦截器 在main.js中添加 import axios from 'axios'; axios.interceptors.response.use(response ...