SpringBoot怎么使用JdbcTemplate操作数据库
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要介绍了SpringBoot怎么使用JdbcTemplate操作数据库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot怎么使用JdbcTemp
千家信息网最后更新 2024年09月22日SpringBoot怎么使用JdbcTemplate操作数据库
这篇文章主要介绍了SpringBoot怎么使用JdbcTemplate操作数据库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot怎么使用JdbcTemplate操作数据库文章都会有所收获,下面我们一起来看看吧。
JdbcTemplate 是 Spring 提供的一套 JDBC 模版框架,利用 AOP 技术来解决直接使用 JDBC 时大量重复代码的问题。虽然没有 MyBatis 那么灵活,但是比直接使用 JDBC 要方便很多。
一、创建表
CREATE TABLE `t_demo` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(120) NOT NULL, `num` int(11) NOT NULL, `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='demo表';
二、添加依赖、配置
1、首先编辑 pom.xml 文件,添加相关依赖。
org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java com.alibaba druid 1.1.9
2、编写配置
spring.datasource.type = com.alibaba.druid.pool.DruidDataSourcespring.datasource.url = jdbc:mysql://localhost:3306/PiaoDB?useUnicode=swater&characterEncoding=UTF-8spring.datasource.username = rootspring.datasource.password = rootspring.datasource.driver-class-name = com.mysql.jdbc.Driver
三、编写代码
1、编写实体类
@Data@Accessors(chain = true)public class Demo { private Integer id; private String name; private Integer num; private Date createTime;}
2、编写Dao代码
@Repositorypublic class DemoDao { @Autowired private JdbcTemplate jdbcTemplate; // 新增数据 public int addDemo(Demo demo) { return jdbcTemplate.update("INSERT INTO t_demo(name, num) VALUE (?, ?)", demo.getName(), demo.getNum()); } // 修改数据 public int updateDemo(Demo demo) { return jdbcTemplate.update("UPDATE t_demo SET name=?, num=? WHERE id=?", demo.getName(), demo.getNum(), demo.getId()); } // 删除数据 public int deleteDemoById(Integer id) { return jdbcTemplate.update("DELETE FROM t_demo WHERE id=?", id); } // 获取单条数据 public Demo getDemoById(Integer id) { return jdbcTemplate.queryForObject("SELECT * FROM t_demo WHERE id=?", new BeanPropertyRowMapper<>(Demo.class), id); } // 获取多条数据 public ListgetAllDemos() { return jdbcTemplate.query("SELECT * FROM t_demo", new BeanPropertyRowMapper<>(Demo.class)); }}
3、编写Controller代码
@RestController@RequestMapping("/demo")public class DemoController { @Autowired private DemoDao demoDao; @RequestMapping("") public void test(){ // 新增数据 int num = demoDao.addDemo(new Demo().setName("piao").setNum(20)); System.out.println("插入一条数据:" + num); // 修改数据 int num2 = demoDao.updateDemo(new Demo().setId(15).setName("piao").setNum(22)); System.out.println("更新一条数据:" + num2); // 删除数据 int num3 = demoDao.deleteDemoById(13); System.out.println("删除一条数据:" + num3); // 查询单条数据 Demo demo = demoDao.getDemoById(15); System.out.println("查询1条数据:" + demo.toString()); // 查询多条数据 Listdemos = demoDao.getAllDemos(); System.out.println("查询多条数据:" + demos); }}
四、验证结果
关于"SpringBoot怎么使用JdbcTemplate操作数据库"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"SpringBoot怎么使用JdbcTemplate操作数据库"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
数据
数据库
代码
查询
多条
知识
内容
篇文章
配置
价值
实体
技术
操作简单
文件
文章
易懂
更多
框架
模版
看吧
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
游戏软件开发项目解决方案
小米6装不了魅族服务器
网络安全汇编
网络安全维护多项选择
佳明的服务器在哪里
办公室网络安全存在问题
四个字的软件开发品牌
steam 黑魂3 服务器
陕西医保动态数据库
字节创科网络技术有限公司
税务网络安全体系建设
广西2u工控存储服务器机箱生产
数据库终端应用者是什么意思
对数据库的感想
联云服务器
服务器安全防护方案
互联网网络安全都包括什么
软件开发的新技术有哪些
服务器的串口重定向什么意思
打游戏缺少数据库什么意思
深圳小学的网络安全课在哪看
荆州网络安全维护哪家好
服务器管理php
怎么查询角色转移服务器
泰拉瑞亚tmod启动服务器慢
iis安装服务器
广东网络技术开发案例
山西正规软件开发服务检测中心
软件开发需求镀金
服务器登录管理软件