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

db.properties

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

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
9
10 <context:property-placeholder location="classpath:db.properties"/>
11 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
12 <property name="user" value="${jdbc.user}"></property>
13 <property name="password" value="${jdbc.password}"></property>
14 <property name="driverClass" value="${jdbc.driverClass}"></property>
15 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
16 </bean>
17
18 <!-- NamedParameterJdbcTemplate有一个带有DataSource的构造器 -->
19 <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
20 <constructor-arg ref="dataSource"></constructor-arg>
21 </bean>
22 </beans>

Java代码

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

 

方式二:

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

Spring NamedParameterJdbcTemplate详解(10)的更多相关文章

  1. Spring NamedParameterJdbcTemplate 详解

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

  2. Spring NamedParameterJdbcTemplate详解

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

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

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

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

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

  5. spring事务详解(三)源码详解

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

  6. Spring Aop 详解二

    这是Spring Aop的第二篇,案例代码很详解,可以查看https://gitee.com/haimama/java-study/tree/master/spring-aop-demo. 阅读前,建 ...

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

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

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

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

  9. 小甲鱼PE详解之基址重定位详解(PE详解10)

    今天有一个朋友发短消息问我说“老师,为什么PE的格式要讲的这么这么细,这可不是一般的系哦”.其实之所以将PE结构放在解密系列继基础篇之后讲并且尽可能细致的讲,不是因为小甲鱼没事找事做,主要原因是因为P ...

随机推荐

  1. Dart编程布尔值

    Dart为布尔数据类型提供内置支持.Dart中的布尔数据类型仅支持两个值true和false.关键字bool用于表示DART中的布尔值. 在dart中声明布尔变量的语法如下所示 bool var_na ...

  2. 绘制窗体渐变背景的函数[delphi]

    绘制窗体渐变背景的函数,三个参数分别代表起始颜色,终止颜色,绘制方向procedure TForm1.Draw(StartColor:TColor;EndColor:TColor;Direction: ...

  3. 《DSP using MATLAB》Problem 8.45

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  4. 使用豆瓣源安装python包

    Python pip安装的原理就是从Python的官方源https://pypi.python.org/pypi下载到本地安装, 但是访问官方源网络不稳定,速度较慢,我经常会遇到一个问题,pip安装包 ...

  5. PAT L2-021. 点赞狂魔 /// sort+unique去重

    https://www.patest.cn/contests/gplt/L2-021 题目大意: 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每篇博文都有一些刻画其特性的标签,而你点赞 ...

  6. 【POJ】1251 Jungle Roads

    题目链接:http://poj.org/problem?id=1251 题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离.求构建所有村庄道路的最短距离. 题解:最小生成树裸题.注意 ...

  7. 源码编译安装nginx详细步骤

    1.下载nginx源码包并解压 可在http://nginx.org/en/download.html下载.tar.gz的源码包,如(nginx-1.4.7.tar.gz) 下载后通过tar -xvz ...

  8. 集群cluster概念

    集群是由两台或多台计算机(称为节点node或成员member)共同执行任务群集 集群方式: 存储集群  GFS共享存储 负载均衡  LB load balance 高可用     HA high av ...

  9. webgoat的构建

    如何打开webgoat 1.ctrl+r  打开命令 2.cmd 打开命令编辑器 3.我的webgoat保存在E:/安全软件/webgoat-container-7.0.1-war-exec下 4.在 ...

  10. leetcode-11-盛水最多的容器

    题目描述: 方法一:双指针 class Solution: def maxArea(self, height: List[int]) -> int: left = 0 right = len(h ...