首页 技术 正文
技术 2022年11月9日
0 收藏 958 点赞 4,858 浏览 1811 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860