ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

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

《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读者可以在恰当的场景中合理利用。
返回列表