Spring NamedParameterJdbcTemplate详解
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详解的更多相关文章
- Spring NamedParameterJdbcTemplate 详解
转自: https://zmx.iteye.com/blog/373736 NamedParameterJdbcTemplate类是基于JdbcTemplate类,并对它进行了封装从而支持命名参数特性 ...
- Spring NamedParameterJdbcTemplate详解(10)
NamedParameterJdbcTemplate和JdbcTemplate功能基本差不多.使用方法也类型.下面具体看下代码. db.properties 1 jdbc.user=root 2 jd ...
- Spring配置文件详解 – applicationContext.xml文件路径
Spring配置文件详解 – applicationContext.xml文件路径 Java编程 spring的配置文件applicationContext.xml的默 ...
- spring配置文件详解--真的蛮详细
spring配置文件详解--真的蛮详细 转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...
- 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理
Spring AOP详解 . JDK动态代理.CGLib动态代理 原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...
- J2EE进阶(四)Spring配置文件详解
J2EE进阶(四)Spring配置文件详解 前言 Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程 ...
- Spring RestTemplate详解
Spring RestTemplate详解 1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...
- spring事务详解(五)总结提高
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.概念 ...
- spring事务详解(四)测试验证
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
随机推荐
- bzoj1004 purfer 序列
之前没不知道这个数列. 一个purfer序列与一棵树一一对应. 长度为n的purfer的集合: A = { s | s is a sequence and a∈[1,len(s)+2] 一个直接的结论 ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- asp.net调用存储过程1
1,传入参数,传出参数 public int GetTeam1Id(string userId) { int team1ID = -1; st ...
- UESTC 2015dp专题 E 菲波拉契数制 dp
菲波拉契数制 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descr ...
- div中内容超出自动换行
下面以table中td的内容超出为例说明: 首先: td { display: block; } 然后:给td设置css样式: 1. td { word-wrap: break-word; } 2. ...
- Spring依赖检查
在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...
- gdb逆向调试
http://blog.csdn.net/yiling2012/article/details/35988361
- C#各种结束进程的方法详细介绍
Process类的CloseMainWindow, Kill, Close Process.CloseMainWindow是GUI程序的最友好结束方式,从名字上就可以看出来它是通过结束主窗体,相当于用 ...
- Oracle初始安装内存设置参考
预备知识 shared memory:共享内存段: 一个内存区域,可以被不同的进程读取.oracle使用它来构成sga.oracle使用以下三种方法来创建一个sga区: 1. 使用单个共享内存段. ...
- 为11gR2 Grid Infrastructure增加新的public网络
在某些环境下,运行11.2版本的RAC数据库的服务器上,连接了多个public网络,那么就会有如下的需求: 给其他的,或者说是新的public网络增加新的VIP地址. 在新的public网络上增加SC ...