Spring JDBC包提供了JdbcTemplate和它的两个兄弟SimpleJdbcTemplate和NamedParameterJdbcTemplate。
NamedParameterJdbcTemplate类是基于JdbcTemplate类,并对它进行了封装从而支持命名参数特性。
NamedParameterJdbcTemplate主要提供以下三类方法:execute方法、query及queryForXXX方法、update及batchUpdate方法。
命名参数设值有两种方式:java.util.Map和SqlParameterSource:
1)Map:使用Map键数据来对于命名参数,而Map值数据用于设值;
2)SqlParameterSource:可以使用SqlParameterSource实现作为来实现为命名参数设值,默认有MapSqlParameterSource和BeanPropertySqlParameterSource实现;MapSqlParameterSource实现非常简单,只是封装了java.util.Map;而BeanPropertySqlParameterSource封装了一个JavaBean对象,通过JavaBean对象属性来决定命名参数的值。
其中有两个比较实用的两个类,分别是BeanPropertySqlParameterSource、BeanPropertyRowMapper。
BeanPropertySqlParameterSource:可以把实例转化成SqlParameterSource
例,new BeanPropertySqlParameterSource(new User);
BeanPropertyRowMapper:可以把返回的每一行转化成对应的对象
例,new BeanPropertyRowMapper<>(CustomSearchVo.class);
项目中的示例:
@Service("repeatContactsService") @Transactional public class RepeatContactsService { private static final Logger LOG = LoggerFactory.getLogger(RepeatContactsService.class); @Resource private NamedParameterJdbcTemplate namedParameterJdbcTemplate; public int getRepeatContactsCountByPhone(String userid, String phone){ String sql= "SELECT count(1) FROM tab_user WHERE userid != :userid AND number = :phone"; Map parmas = ImmutableMap.of("userid",userid,"phone",phone); Integer count = null; try { count = namedParameterJdbcTemplate.queryForObject(sql, parmas, Integer.class); } catch (DataAccessException e) { LOG.error("获取值时抛错:", e); } return count; } public List<RepeatContactsDetailVo> getAllRepeatContacts(String userid) { DateTime before = DateTime.now(); Set<String> phoneSet = new HashSet<>(); String sql = "SELECT contacts_phone FROM tab_contacts WHERE userid=:userid"; Map param = ImmutableMap.of("userid", userid); List<String> phoneList = null; try { phoneList = namedParameterJdbcTemplate.queryForList(sql , param, String.class); } catch (DataAccessException e) { LOG.error("获取用户的通讯录和运营商时抛错:", e); } //生成需要返回的结果 Map params = ImmutableMap.of("phones", phoneSet, "userid", userid); String sql = "SELECT a.name,a.phone,MAX(a.type),b.name userName,b.phone userPhone,b.idcard userIdcard FROM tab_contacts a where call_number in (:phones) AND userid != :userid GROUP BY a.userid,a.phone"; List<RepeatContactsDetailVo> contactsList = new ArrayList<>(); try { contactsList = namedParameterJdbcTemplate. query(sql, params, new BeanPropertyRowMapper<>(RepeatContactsDetailVo.class)); } catch (DataAccessException e) { LOG.error("获取时抛错:", e); } return contactsList ; } }
推荐看讲NamedParameterJdbcTemplate:
http://www.voidcn.com/blog/victor_cindy1/article/p-6153531.html
讲解JdbcTemplate接口比较好的
https://my.oschina.net/u/218421/blog/38598
相关推荐
我的使用Spring的NamedParameterJdbcTemplate完成DAO操作实例 博文链接:https://zmx.iteye.com/blog/373736
NULL 博文链接:https://shareisattitude.iteye.com/blog/2221135
Spring基础:Spring AOP简单使用
本资源里是使用IDEA中通过Maven创建一个web项目,结构采用了Spring的简单框架的整合。对 于初学Spring的伙伴们来说可以下载的看看。
使用spring-ldap-core测试链接ldap com.ldap.test.service包下为Junit测试类
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
《Spring3.0就这么简单》主要介绍了Spring3.0的核心内容,不仅讲解了Spring3.0的基础知识,还深入讨论了SpringIoC容器、SpringAOP、使用SpringJDBC访问数据库、集成Hibernate、Spring的事务管理、SpringMVC、单元...
Spring:JdbcTemplate使用指南 Spring:JdbcTemplate使用指南
RestTemplate是执行HTTP请求的同步阻塞式的客户端,它在HTTP客户端库(例如JDK HttpURLConnection,Apache HttpComponents,okHttp等)基础封装了更加简单易用的模板方法API。也就是说RestTemplate是一个封装,底层...
SpringCloud简单使用
SpringColud1简易分布式 ● cloud-config-server:配置服务器-(通过git获取配置) ● cloud-eureka-server:eureka注册服务器 ● cloud-simple-service:一个使用mybatis的数据库应用,服务端 ● cloud-simple-...
Spring 参考手册 Spring速查手册 spring初学者使用
这个项目提供了一个构建在 Spring 生态系统之上的 API 网关,包括:Spring 5,Spring ... Spring Cloud Gateway 旨在提供一种简单而有效的 API 路 由方式,并为其提供横切关注点,例如:安全,监控/指标和弹性。
一般使用mybatis的环境,大多都是别人已经配置...我文章中的方法不基于spring,一般很少会在真实项目中直接使用。我把我的搭建过程记录下来给有用的人吧。 https://blog.csdn.net/lxyoucan/article/details/126162493
SpringCloud开发使用手册~~SpringCloud开发使用手册~~SpringCloud开发使用手册~~SpringCloud开发使用手册~~SpringCloud开发使用手册~~
spring的quartz使用实例,spring的quartz使用实例
SpringCloud简单使用RestTemplate调用对方的服务
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。...简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用...
strut2+spring+springjdbctemplate做的简易登录系统