Spring Boot (7) JdbcTemplate访问数据库
使用jdbcTemplate操作数据库
spring framework对数据库的操作在jdbc上面做了深层次的封装,通过依赖注入功能,可以将datasource注册到jdbcTemplate中,学习成本低,毕竟是jdbc的基础知识。
在pom.xml中添加jdbc模块和mysql数据库驱动
<!--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>
application.yml
spring boot的jdbc模块会加载以下参数,并且根据url可以识别并自动加载mysql驱动,自动创建数据库实例,自动实现连接池。
server:
port: 8088
servlet:
context-path: /
spring:
datasource:
url: jdbc:mysql://localhost:3306/david2018_db?characterEncoding=utf8
username: root
password: 1234
创建t_user表
CREATE TABLE `david2018_db`.`t_user` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
t_user.java
package com.spring.boot.bean;
public class t_user {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
}
t_userDao.java
jdbc模块还会自动创建一个JdbcTemplate实例,可以在程序中直接注入使用,下面的dao实现了两个方法:
update方法:执行增删改操作
queryForList方法:执行查询操作
params:任意数量的数组,配置sql中的?占位符
package com.spring.boot.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List; @Repository
public class t_userDao {
@Autowired
JdbcTemplate jdbcTemplate; public int update(){
String sql = "update t_user set username = ? where id = ?";
Object[] params = new Object[]{"boot",1};
return jdbcTemplate.update(sql,params);
} public List queryForList(){
String sql = "select * from t_user";
return jdbcTemplate.queryForList(sql);
}
}
helloService.java
spring boot同样自动配置好了事务,在service上 直接加一个注解即可。
package com.spring.boot.service; import com.spring.boot.dao.t_userDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import java.util.List; @Transactional //开启事务
@Service
public class t_userService {
@Autowired
private t_userDao dao; public int update() {
return dao.update();
} public List queryForList() {
return dao.queryForList();
}
}
t_userController.java
package com.spring.boot.controller; import com.spring.boot.bean.t_user;
import com.spring.boot.service.t_userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RequestMapping("/t_user")
@RestController
public class t_userController {
@Autowired
private t_userService service; @GetMapping("/update")
public String update(){
service.update();
return "update";
} @GetMapping("/list")
public List<t_user> list(){
return service.queryForList();
}
}
输入 http://localhost:8088/t_user/list list 进行测试
Spring Boot (7) JdbcTemplate访问数据库的更多相关文章
- spring boot(6)-JdbcTemplate访问数据库
pom.xml 添加jdbc模块和mysql依赖 <dependency> <groupId>org.springframework.boot</groupId&g ...
- Spring boot未授权访问造成的数据库外联
一.spring boot 日常测试或攻防演练中像shiro,fastjson等漏洞已经越来越少了,但是随着spring boot框架的广泛使用,spring boot带来的安全问题也越来越多,本文仅 ...
- spring boot与jdbcTemplate的整合案例2
简单入门了spring boot后,接下来写写跟数据库打交道的案例.博文采用spring的jdbcTemplate工具类与数据库打交道. 下面是搭建的springbootJDBC的项目的总体架构图: ...
- 使用JdbcTemplate访问数据库
参考源端:https://blog.csdn.net/liaodehong/article/details/76974827 今天用Spring Boot访问一下数据库,并且把数据返回到页面中,进行增 ...
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...
- Spring Boot的数据访问:CrudRepository接口的使用
示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...
- Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...
- Spring Boot中使用PostgreSQL数据库
在如今的关系型数据库中,有两个开源产品是你必须知道的.其中一个是MySQL,相信关注我的小伙伴们一定都不陌生,因为之前的Spring Boot关于关系型数据库的所有例子都是对MySQL来介绍的.而今天 ...
- Spring MVC或Spring Boot配置默认访问页面不生效?
相信在开发项目过程中,设置默认访问页面应该都用过.但是有时候设置了却不起作用.你知道是什么原因吗?今天就来说说我遇到的问题. 首先说说配置默认访问页面有哪几种方式. 1.tomcat配置默认访问页面 ...
随机推荐
- 在Excel表格中打字为何会被覆盖掉
修改证件图片背景:https://www.cnblogs.com/liyanbin/p/9466746.html Insert键是插入和改写(覆盖)的开关如果当前处于改写(覆盖)模式,再按一下Inse ...
- Linux:安装CentOS 6.x和CentOS 7.x
1,准备镜像,这里到阿里云镜像网站下载 https://opsx.alibaba.com/mirror 2,安装CentOS6.10 打开vmware workstation--> 典型--&g ...
- 腾讯云,搭建nginx静态网站服务器
搭建Http静态服务器环境 任务时间:15min ~ 30min 搭建静态网站,首先需要部署环境.下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务. 安装 Nginx 在 ...
- sql 区分大小写查询
sql 区分大小写查询 select * FROM [Users] where userName collate Chinese_PRC_CS_AS='ADMIN'
- <embed> 标签
<embed> 标签定义嵌入的内容,比如插件. <embed quality="high" bgcolor="#FFF" wmode=&quo ...
- 【codeforces 514B】Han Solo and Lazer Gun
[题目链接]:http://codeforces.com/contest/514/problem/B [题意] 每次攻击可以把经过自己的一条直线上的所有点都毁掉; 然后给你n个目标物的坐标 问你最少要 ...
- 选择客栈(codevs 1135)
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- 洛谷—— P1077 摆花
https://www.luogu.org/problem/show?pid=1077 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客 ...
- 51Nod——T 1113 矩阵快速幂
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 ...
- Windows与VBox虚拟机共享目录的方法
前言 安装完虚拟机,设置共享目录的时候碰到问题,网上搜索了一下,按照相关教程操作还是有问题,可能是写的不够清楚,于是按照自己的理解重写了一份,力求简单明了,理解轻松. 具体步骤 1.关闭虚拟机(如果未 ...