17.Spring_使用NamedParameterJdbcTemplate

本文介绍如何在Spring中使用具名参数代替SQL语句中的占位符,并提供了配置文件及测试代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用具名参数来代替sql语句中的?。

spring配置文件:

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="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.0.xsd">
<!-- 导入资源文件 -->
	<context:property-placeholder  location="classpath:db.properties" />

<!-- 配置c3p0 -->
	<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="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		
		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	<!-- 配置JdbcTemplate  -->
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<!-- 配置 NamedParameterJdbcTemplate 对象,该对象可以使用具名参数,其没有无参构造器,所以必须传递参数-->
	<bean id="NamedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
		<constructor-arg ref="dataSource"></constructor-arg>
	 </bean>
</beans>


测试:


public class JDBCTest {
	
	private ApplicationContext ctx= null;
	private JdbcTemplate jdbcTemplate= null;
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	{
		ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
		jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
		namedParameterJdbcTemplate=(NamedParameterJdbcTemplate) ctx.getBean(NamedParameterJdbcTemplate.class);
	}
	/**
	 * 使用具名参数时,可以使用update(String sql, SqlParameterSource paramSource)方法进行操作
	 * 1.sql与旧中的参数名和类的属性一致
	 * 2.使用SqlParameterSource的BeanPropertySqlParameterSource实现类作为参数。
	 */
	@Test
	public void testNamePatameterJDBCTempate2() {
		String sqlString = "insert  into t_admin(admin_number,admin_password) values(:adminNumber,:adminPassword)";
		Admin admin =new Admin();
		admin.setAdminNumber("hcx");
		admin.setAdminPassword("111111111111111");
		SqlParameterSource sqlParameterSource = null;
		sqlParameterSource =new  BeanPropertySqlParameterSource(admin);
		namedParameterJdbcTemplate.update(sqlString, sqlParameterSource);
	}
	
	
	/**
	 * 命名参数代替sql中的问号
	 */
	@Test
	public void testNamedParameterJdbcTemplate() {
		String sqlString = "insert  into t_admin(admin_number,admin_password) values(:num,:pwd)";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("num", "hcx");
		paramMap.put("pwd", "122");
		namedParameterJdbcTemplate.update(sqlString, paramMap);
	}
	
	
	@Test
	public void testDataSource() throws SQLException {
		DataSource dataSource =ctx.getBean(DataSource.class);
		System.out.println(dataSource.getConnection());
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值