NamedParameterJdbcTemplate和JdbcTemplate功能基本差不多。使用方法也类型。下面具体看下代码。

db.properties

 jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc\:mysql\:///test

applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean> <!-- NamedParameterJdbcTemplate有一个带有DataSource的构造器 -->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
</beans>

Java代码

 //启动IoC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); NamedParameterJdbcTemplate namedParameterJdbcTemplate=ctx.getBean(NamedParameterJdbcTemplate.class);
//为变量名称前面加上冒号
String sql="insert into user (name,deptid) values (:name,:deptid)";
//定义map集合,其参数名称为sql语句中变量的名称
Map<String,Object> paramMap=new HashMap<String,Object>();
paramMap.put("name", "caoyc");
paramMap.put("deptid", 2);
namedParameterJdbcTemplate.update(sql, paramMap);

 

方式二:

 //启动IoC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); NamedParameterJdbcTemplate namedParameterJdbcTemplate=ctx.getBean(NamedParameterJdbcTemplate.class);
//为变量名称前面加上冒号
String sql="insert into user (name,deptid) values (:name,:deptid)";
//定义个实体类
User user=new User();
user.setName("zhh");
user.setDeptid(3); SqlParameterSource paramSource=new BeanPropertySqlParameterSource(user);
namedParameterJdbcTemplate.update(sql, paramSource);

Spring NamedParameterJdbcTemplate详解的更多相关文章

  1. Spring NamedParameterJdbcTemplate 详解

    转自: https://zmx.iteye.com/blog/373736 NamedParameterJdbcTemplate类是基于JdbcTemplate类,并对它进行了封装从而支持命名参数特性 ...

  2. Spring NamedParameterJdbcTemplate详解(10)

    NamedParameterJdbcTemplate和JdbcTemplate功能基本差不多.使用方法也类型.下面具体看下代码. db.properties 1 jdbc.user=root 2 jd ...

  3. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  4. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  5. 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理

    Spring AOP详解 . JDK动态代理.CGLib动态代理  原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...

  6. J2EE进阶(四)Spring配置文件详解

    J2EE进阶(四)Spring配置文件详解 前言 Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程 ...

  7. Spring RestTemplate详解

    Spring RestTemplate详解   1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...

  8. spring事务详解(五)总结提高

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.概念 ...

  9. spring事务详解(四)测试验证

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...

随机推荐

  1. ROS知识(3)----功能包package编译的两种方式

    ROS的包编译有两种方法(我知道的),一种是用rosmake,这种方法简单:另一种是用catkin_make,这种方法更方便包的管理和开发.这两种方法都是先建立工作空间workspace(类似于vs下 ...

  2. Testcase的编写

    ‘ID’用于记录某一功能: ‘标题’用于表示某一Case:(一个功能有多个Case) ‘优先级’标记Case的重要等级,运行顺序 ‘测试步骤’记录测试流程 1.Given:条件 2.And:进行某些前 ...

  3. Okam(奥卡姆):小程序开发框架

    Okam(奥卡姆):小程序开发框架 Okam 是什么 `Okam` 一个面向小程序开发的开发框架,开发体验类 `Vue`.详情 Okam 对各小程序的支持情况 支持 百度小程序 支持 微信小程序 支持 ...

  4. Android之旅十四 android中的xml文件解析

    在我们做有关android项目的时候,肯定会涉及到对xml文件的解析操作.以下给大家介绍一下xml文件的解析.包括DOM.SAX.Pull以及曾经我们用到的DOM4J和JDOM: 要解析的XML文件: ...

  5. python笔记18-sort和sorted区别

    前言 python的排序有两个方法,一个是list对象的sort方法,另外一个是builtin函数里面sorted,主要区别: sort仅针对于list对象排序,无返回值, 会改变原来队列顺序 sor ...

  6. Mac通过wifi连接 Android设备

    公司用的全是mac开发,但是全是type-C接口,每次背电脑回家啊,还得带个数据线转换器…… 想着回来,直接通过Wi-Fi连接手机就好,发现完全忘了之前套路,现在赶紧记下一波,保证包教包会! STEP ...

  7. axure 6.5 汉化正式版软件及注册码

    Axure公司发布了Axure RP 6.5 正式版. 官方主页: http://www.axure.com/news 官方下载: http://www.axure.com/download 视频介绍 ...

  8. pidera安装node.js(树莓派)

    1. 下载node.js wget http://nodejs.org/dist/v0.10.5/node-v0.10.5-linux-arm-pi.tar.gz tar -xzvf node-v0. ...

  9. [Todo] Java并发编程学习

    有两个系列的博文,交替着可以看看: 1. Java并发编程与技术内幕 http://blog.csdn.net/Evankaka/article/details/51866242 2. [Java并发 ...

  10. Powerdesigner数据库建模--概念模型--ER图

    目标: 本文主要介绍PowerDesigner中概念数据模型 CDM的基本概念. 一.概念数据模型概述 数据模型是现实世界中数据特征的抽象.数据模型应该满足三个方面的要求: 1)能够比较真实地模拟现实 ...