日期:2014-05-16  浏览次数:20462 次

JdbcTemplate使用小结
spring配置:

<bean id="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<constructor-arg ref="dataSourceTransactionManager" />
</bean>

<bean id="jdbcTemplate"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource" />
</bean>	
//不需要返回值,增加数据时
public long addFamily(int regType,String mobile,String address,long areaId,String fmlCode){
		OracleSequenceMaxValueIncrementer incr = 
		    new OracleSequenceMaxValueIncrementer(jt.getDataSource(), "seq_family");
		long id = 0;
		try{
			id = incr.nextLongValue();//获得id
			if(!(jt.update(INSERT_FAMILY, new Object[] { id,regType,mobile,address,areaId,fmlCode })>=1)){
				id=0;
			}
		}catch(DataAccessException e){
			logger.error(e.getMessage());
			throw e;
		}
		return id;
	}

//返回单个对象
public Member getMember(String mobile,String relations){
		Member entity=null;
		try{
			ParameterizedRowMapper<Member> mapper = new ParameterizedRowMapper<Member>() {
				public Member mapRow(ResultSet rs, int rowNum)
				throws SQLException {
					Member m = new Member();
					m.setId(rs.getLong(1));
					m.setFamilyId(rs.getLong(2));
					m.setMobile(rs.getString(3));
					m.setRelations(rs.getString(4));
					m.setState(rs.getInt(5));
					return m;
				}
			};
			entity =  (Member) jt.queryForObject(QUERY_MEMBER_BY_RELA,new Object[] { mobile,relations }, mapper);
		}catch(DataAccessException ex){
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return entity;		
	}
//更新数据
public boolean updateFamily(Family f){
		boolean res = false;
		try{
			jt.update(UPDATE_FAMILY, new Object[] { f.getState(),f.getId() });	
			res=true;
		}catch(DataAccessException e){
			logger.error(e.getMessage());
			throw e;
		}
		return res;
	}

//返回集合
public List<Msg> getCancelMsgList(long orderId){
		ParameterizedRowMapper<Msg> mapper = new ParameterizedRowMapper<Msg>() {
			public  Msg mapRow(ResultSet rs, int rowNum)
			throws SQLException {
				Msg entity = new Msg();
				entity.setId(rs.getLong(1));
				entity.setLsh(rs.getString(2));
				entity.setEwmHdh(rs.getString(3));
				entity.setEwmSph(rs.getString(4));
				entity.setEwmJgm(rs.getString(5));
				return entity;
			}
		};
		return jt.query(QUERY_MM,new Object[] { orderId }, mapper);		
	}

//执行插入操作
public boolean buildProDayReport(final String strDate){
		boolean bRes = false;
		final Response tranRes = new Response();
		try{
			tt.execute(new TransactionCallbackWithoutResult(){
				@Override
				protected void doInTransactionWithoutResult(TransactionStatus ts){			
					jt.update(ProDayReport1,new Object[] { strDate });
					jt.update(ProDayReport2,new Object[]{ strDate });
					tranRes.setCode("0000");
				}
			});
			if("0000".equals(tranRes.getCode())){
				bRes = true;
			}
		}catch(Exception ex){
			logger.error(ex.getMessage());
		}
		return bRes;
	}
1 楼 还有也许 2010-06-12  
看的人太少了