java [30] NamedParameterJdbcTemplate使用带命名参数的JDBCtemplete

在经典的 JDBC 用法中, SQL 参数是用占位符 ? 表示,并且受到位置的限制. 定位参数的问题在于, 一旦参数的顺序发生变化, 就必须改变参数绑定. 在 Spring JDBC 框架中, 绑定 SQL 参数的另一种选择是使用具名参数(named parameter).
          那么什么是具名参数?
         具名参数: SQL 按名称(以冒号开头)而不是按位置进行指定. 具名参数更易于维护, 也提升了可读性. 具名参数由框架类在运行时用占位符取代具名参数只在 NamedParameterJdbcTemplate 中得到支持。NamedParameterJdbcTemplate可以使用全部jdbcTemplate方法,除此之外,我们来看看使用它的具名参数案例:

package com.us.spring;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.us.sqltable.userInfo;

import datasource.DBCPUtils;

public class NamedJdbcTemplete {
	static NamedParameterJdbcTemplate named = new NamedParameterJdbcTemplate(
			DBCPUtils.getDataSource());
	
	
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		/*List user = findUsers(5, 100f);
		System.out.println(user);*/
		userInfo user = new userInfo();
		user.setId(5);
		user.setMoney(500f);
		System.out.println(findUsers2(user));
		user.setBirthday(new Date(1028, 3, 15));
		user.setUsername("mo");
		user.setPasswd("rt");
		user.setType("ooxx");
		addUser(user);
		
	}
	// 插入数据时使用NamedParameterJdbcTemplate比JdbcTemplate简单点
	static void addUser(userInfo user) {
		String sql = "insert into userInfo(username,passwd,type,birthday,money) values (:username,:passwd,:type,:birthday,:money)";
		SqlParameterSource sps = new BeanPropertySqlParameterSource(user);
		KeyHolder keyHolder = new GeneratedKeyHolder();
		named.update(sql, sps, keyHolder);
		int id = keyHolder.getKey().intValue();
		user.setId(id);
		//如果是组合主键
		Map map = keyHolder.getKeys();   //获取key 列名
		System.out.println(map);
		
		
	}
	static List findUsers(int id,float money) {
		//一般方法
		//String sql = "select username,passwd,type,birthday,money from userInfo where id<? and ? money >?";
		//使用命名参数
		String sql = "select username,passwd,type,birthday,money from userInfo where id<:i and money >:m";
		//假如数据库中的名字跟java类中的名字对应不起来,可以使用别名同样实现
		//String sql = "select user_name,passwd,type,birthday,money from userInfo where username=?";
		Map params = new HashMap();
		params.put("i", id);
		params.put("m", money);
		//userInfo user=new userInfo();
		List users = named.query(sql, params, new BeanPropertyRowMapper<userInfo>(userInfo.class));
		return users;
	}
	static List findUsers2(userInfo user) {
		//这种方法比较简单,但是需要传入一个对象,并且:后面的名字有限制。
		String sql = "select username,passwd,type,birthday,money from userInfo where id<:id and money >:money";
		SqlParameterSource sps = new BeanPropertySqlParameterSource(user);
		List users = named.query(sql, sps, new BeanPropertyRowMapper<userInfo>(userInfo.class));
		return users;
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值