《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 32

发布时间:2026/11/12 20:59:42
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 32 《SpringBoot 3入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 3212.2.2 JdbcTemplate 应用于 Dao 层虽然已经可以使用 JdbcTemplate 完成对数据的 CRUD 操作但是目前的代码中有一个非常大的问题与数据库操作相关的 API 应当集中到 Dao层的 API 中而不是直接注入 Controller 层。所以下面要做的是将 JdbcTemplate 的使用移入 Dao 层。模拟一个贴近真实开发环境的代码结构新建一个 dao 包并在其中创建一个接口 UserDao声明 3 个方法。注意这次只演示了保存操作和查列表、查单条数据的操作并未涉及全部内容。publicinterfaceUserDao{voidsave(Useruser);UserfindById(Integerid);ListUserfindAll();}紧接着是 UserDao 的实现类 UserDaoImpl注意这个实现类上需要使用 Repository 标注该注解同样是 Component 的派生注解可以被Spring Boot 扫描到并注入进 IOC 容器。packagecom.yangjunbo.springboot.jdbc.exampleb;importcom.yangjunbo.springboot.jdbc.examplea.User;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.BeanPropertyRowMapper;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.stereotype.Repository;importjava.util.List;RepositorypublicclassUserDaoImplimplementsUserDao{AutowiredprivateJdbcTemplatejdbcTemplate;Overridepublicvoidsave(Useruser){jdbcTemplate.update(insert into tbl_user (name, tel) values (?, ?),user.getName(),user.getTel());}OverridepublicUserfindById(Integerid){ListUseruserListjdbcTemplate.query(select * from tbl_user where id ?,newBeanPropertyRowMapper(User.class),id);returnuserList.size()0?userList.get(0):null;}OverridepublicListUserfindAll(){returnjdbcTemplate.query(select * from tbl_user,newBeanPropertyRowMapper(User.class));}}最后在需要操作 tbl_user 表的位置注入 UserDao 即可不需要再注入 JdbcTemplate 组件。12.2.3 查询策略1.queryForList顾名思义queryForList 方法的返回值一定是一个 List借助 IDE 可以发现 JdbcTemplate 中有 7 个重载的 queryForList 方法这些方法会根据是否传入 elementType 决定返回指定的类型还是 Map注意 “指定的类型” 这个概念只有当查询结果只有单列时才可以指定否则会出现列数过多的异常。在讲解 select 系列方法中已经介绍过指定类型的方法代码展示了一个返回 MapString, Object 的查询示例这种查询的方式通常在临时查询一批数据时使用。重新编译代码后访问 /test11 接口浏览器中得到了数据与预期相同。packagecom.yangjunbo.springboot.jdbc.examplea;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.BeanPropertyRowMapper;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;importjava.util.Map;RestControllerpublicclassJdbcTemplateController{AutowiredprivateJdbcTemplatejdbcTemplate;GetMapping(/test1)publicStringtest1(){System.out.println(jdbcTemplate);returnsuccess;}GetMapping(/test2)publicStringtest2(){UserusernewUser();user.setName(heihei);user.setTel(200);introwjdbcTemplate.update(insert into tbl_user (name, tel) values (?, ?),user.getName(),user.getTel());returnsuccess - row;}GetMapping(/test3)publicStringtest3(){UserusernewUser();user.setName(heihei);user.setTel(54321);introwjdbcTemplate.update(update tbl_user set tel ? where name ?,user.getTel(),user.getName());returnsuccess - row;}GetMapping(/test4)publicStringtest4(){introwjdbcTemplate.update(delete from tbl_user where name ?,heihei);returnsuccess - row;}GetMapping(/test5)publicListUsertest5(){ListUseruserListjdbcTemplate.query(select * from tbl_user,newBeanPropertyRowMapper(User.class));returnuserList;}GetMapping(/test6)publicListUsertest6(){ListUseruserListjdbcTemplate.query(select * from tbl_user where id ?,newBeanPropertyRowMapper(User.class),2);returnuserList;}GetMapping(/test7)publicUsertest7(intid){UseruserjdbcTemplate.queryForObject(select * from tbl_user where id ?,newBeanPropertyRowMapper(User.class),id);returnuser;}GetMapping(/test8)publicUsertest8(intid){ListUseruserListjdbcTemplate.query(select * from tbl_user where id ?,newBeanPropertyRowMapper(User.class),id);UseruseruserList.size()0?userList.get(0):null;returnuser;}GetMapping(/test9)publicUsertest9(intid){UseruserjdbcTemplate.queryForObject(select * from tbl_user where id ?,User.class,id);returnuser;}GetMapping(/test10)publicIntegertest10(){IntegercountjdbcTemplate.queryForObject(select count(*) from tbl_user,Integer.class);returncount;}GetMapping(/test11)publicListMapString,Objecttest11(){ListMapString,ObjectuserListjdbcTemplate.queryForList(select * from tbl_user where id 3);returnuserList;}}返回 MapString, Object 结构的 queryForList 方法通常在项目开发中使用不多但以笔者的工作经历来看这个方法很适合用来临时查询一批数据并对这些数据进行操作由于 JdbcTemplate 只需要依赖数据源因此使用 JdbcTemplate 处理临时的一次性工作非常顺手。2.queryForMapqueryForMap 方法也是一个非常好理解的方法这个系列的方法都会返回 MapString,Object。注意这个方法只会返回一条数据所以这个方法适用于查询单条数据的场景。代码提供了一个非常简单的 queryForMap 方法的使用。有关 JdbcTemplate 的常用方式就介绍这些在实际项目开发中 JdbcTemplate 使用频率不高但就其本身而言JdbcTemplate 不失为一个简单实用的 JDBC API读者可以在恰当的场景中合理利用。

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询