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配置默认访问页面 ...
随机推荐
- 安装hiredis后swoole扩展消失
php -m报错: PHP Warning: PHP Startup: Unable to load dynamic library 'swoole' (tried: /home/work/study ...
- Python os模块和time模块 day4
一.os模块 print(os.listdir(r'/Users/smh/Desktop/整理'))#os.listdir() 列出某个目录下面的文件夹/文件 print(os.path.isfile ...
- 51nod 1002 数塔取数问题【dp】
一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上. 5 8 4 3 6 9 7 2 9 5 例子 ...
- The Morning after Halloween uva1601
这道题思路还是比较清晰的,建图加bfs或双向bfs,其实后者比前者少了将近一半的时间.. 建图可以把某一点所拥有邻接点长度(数目)记录在数组0这个位置,因为这道题使用vector会超时. #inclu ...
- android 彩信附件添加删除
http://blog.csdn.net/yuzhu_guan2012/article/details/6679154
- Beetl学习总结(4)——Web集成
4.1. Web提供的全局变量 Web集成模块向模板提供web标准的变量,做如下说明 request 中的所有attribute.在模板中可以直接通过attribute name 来引用,如在cont ...
- 清北学堂模拟赛d2t1 一道图论神题(god)
题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只有点权. LYK想把这个图删干净,它的方法是这样的.每次选择一个点,将它删掉,但删这个点是需要代价的.假 ...
- eventlet学习笔记
eventlet学习笔记 标签(空格分隔): python eventlet eventlet是一个用来处理和网络相关的python库函数,且可以通过协程(coroutines)实现并发.在event ...
- windows 2013(codevs 1695)
题目描述 Description 话说adamyi编的Windows 2013超时了(- -!),所以他不得不在自己家门口亲眼见证这个电影般的场景.虽然他不想错过这个美妙的时刻,但是他的肚子一再抗议, ...
- Luogu P3740 [HAOI2014] 贴海报 线段树
线段树版的海报 实际上这个与普通的线段树相差不大,只是貌似数据太水,暴力都可以过啊 本来以为要离散的,结果没打就A了 #include<iostream> #include<cstd ...