Spring JDBCTemplate学习笔记

Title: Spring JDBCTemplate学习笔记
Date: 2016-8-26
Author: kagula

Env: Spring 3.1.2.RELEASE, Oracle 11GR2


这里记下示例代码片段,免得过一段时间又忘记了。

		String sql = "insert into cityCodeMap (idcitycodemap,ICBCCode,ChinaPetroCode,cityName)"
			+ "values(seq_citycodemap.nextval,?,?,?)";	
		this.getJdbcTemplate().update(sql,
				new Object[]{ICBCCode,chinaPetroCode,cityName});

		String sql = "delete from cashbox where idcashbox=?";
		this.getJdbcTemplate().update(sql,new Object[]{idcashbox});

		createTime += " 00:00:00";//expect format is 2016-08-26 00:00:00
		Timestamp ts = Timestamp.valueOf(createTime);
		sql= "update paymentnumber set idcashbox=? where createTime=? and idStationInfo=?";
		this.jdbcTemplate.update(sql,new Object[]{
				idcashbox,ts,idStationinfo});

单值返回

	public boolean IsExist(String createTime,int idStationinfo)
	{
		createTime += " 00:00:00";//CreateTime expect format is "2016-08-26 00:00:00"
		Timestamp ts = Timestamp.valueOf(createTime);
		
		int count = this.jdbcTemplate.queryForInt(
				"select count(0) from cashbox where createTime=? and idStationInfo=?",
				new Object[]{ts,idStationinfo});
		if(count>0)
		{
			return true;
		}
		return false;
	}

单值多条记录
		String stationid = this.getJdbcTemplate().queryForObject("select stationid from stationinfotable where idStationinfo=?", 
				new Object[]{idStationinfo},String.class);

单条记录
	public SessionUserInfo Query(String userName)
	{
		String sql = "select a.idCityCodeMap,b.stationname,b.idStationinfo,b.stationid,c.chinapetrocode from userinfotable a "+ 
                     "left join stationinfotable b on a.idStationinfo=b.idStationinfo "
                     + "left join citycodemap c on a.idcitycodemap=c.idcitycodemap where a.name=?";
		/*
		 * Spring中jdbcTemplate的用法实例(一)
		 * http://1358440610-qq-com.iteye.com/blog/1826816
		 */
		SessionUserInfo sui = null;
		try{			
			sui = this.getJdbcTemplate().queryForObject(sql,
					new Object[]{userName},
					  new RowMapper<SessionUserInfo>(){
				@Override  
                public SessionUserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
					SessionUserInfo sui  = new SessionUserInfo();
					sui.setStationid(rs.getString("stationid"));
					sui.setIdCityCodeMap(rs.getString("idCityCodeMap"));
					sui.setStationname(rs.getString("stationName"));
					sui.setIdStationinfo(rs.getString("idStationinfo"));
					sui.setChinaPetroCode(rs.getString("chinaPetroCode"));	
                    return sui; 
                }});			
		}catch(Exception e)
		{
			logger.debug(e.getMessage());
		}
		
		return sui;		
	}


多条记录

示例一:返回List<EscortManager>类型

			sql = "select a.*,b.cityname from escortManager a left join Citycodemap b on a.idcitycodemap=b.idcitycodemap where a.idCityCodeMap=? order by idEscortManager,a.idcitycodemap";
			return jdbcTemplate.query(sql, new Object[]{idCityCodeMap}, new RowMapper<EscortManager>(){
				@Override  
	            public EscortManager mapRow(ResultSet rs,int rowNum)throws SQLException {  
	                EscortManager em  = new EscortManager();
	                em.setIdEscortManager(rs.getString("idEscortManager"));
	                em.setLoginname(rs.getString("loginname"));
	                em.setIdCityCodeMap(rs.getString("idCityCodeMap"));
	                return em;  
	            }});


示例二:类属类

		String sql = "select a.idescortvehicle,a.platenumber from escortvehicle a "  ;
		return this.getJdbcTemplate().query(sql, new UserRowMapper());

类属类
	class UserRowMapper implements RowMapper<EscortVehicle> {
		@Override
		public EscortVehicle mapRow(ResultSet rs, int rowNum) throws SQLException {
			
			EscortVehicle ev = new EscortVehicle();
			ev.setIdEscortVehicle(rs.getString("idEscortVehicle"));
			ev.setPlateNumber(rs.getString("plateNumber"));
			//ev.setStationName(rs.getString("stationName"));
			return ev;
		}
	}


备注

[1]关于timestamp类型

列出createtime字段(timestamp类型)为今天的记录
SELECT * FROM cashbox where TO_CHAR(createtime,'YY/MM/DD') = TO_CHAR(SYSDATE,'YY/MM/DD')
其中TO_CHAR(SYSDATE,'YY/MM/DD')返回16/06/14.


timestamp类型比较示例
where = "and to_timestamp('"+beginDate+"','YYYY-MM-DD') <= a.createTime ";


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值