ibatis中任意sql取值

DAO >>>>>
String destValue = "";
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			con = this.getSqlMapClientTemplate().getDataSource().getConnection();
			st = con.createStatement();
			rs = st.executeQuery(isql);
			while(rs.next()){
				log.info(rs.getObject(1));
				Object rsobj =rs.getObject(1);
				if(rsobj instanceof Integer){
					log.info("111");
					Integer integer = (Integer)rsobj;
					destValue = integer.toString();
				}else if(rsobj instanceof Long){
					log.info("222");
					Long l = (Long)rsobj;
					destValue = l.toString();
				}else if(rsobj instanceof java.math.BigDecimal){
					log.info("333");
					java.math.BigDecimal b = (java.math.BigDecimal) rsobj;
					destValue = b.toString();
				}else if(rsobj instanceof java.sql.Date){
					log.info("444");
					destValue = rsobj.toString();
				}else if(rsobj instanceof java.sql.Timestamp){
					log.info("555");
					Date date = (Date) rsobj;
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					destValue = sdf.format(date).toString();
				}else if(rsobj instanceof java.sql.Clob){
					log.info("666");
					String reString = "";
					java.sql.Clob clob = (java.sql.Clob)rsobj;
					if (clob != null){
						//try {
							Reader is = clob.getCharacterStream();// 得到流
							BufferedReader br = new BufferedReader(is);
							String s = br.readLine();
							StringBuffer sb = new StringBuffer();
							while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
								sb.append(s);
								s = br.readLine();
							}
							reString = sb.toString();
						//} catch (SQLException e) {
						//	logger.error("ClobToString SQLException:", e);
						//} catch (IOException e) {
						//	logger.error("ClobToString IOException:", e);
						//}
					}
					destValue = reString;
				}else{
					log.info("777");
					destValue = (String) rsobj;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				st.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
/*		if(isql.indexOf("l.accountflag = '1' or l.accountflag = '2'")!=-1){
			isql = isql.replace("( l.accountflag = '1' or l.accountflag = '2' )", 
					"l.accountflag in ( '1','2')");
		}*/
		
/*		log.warn("isql2:" + isql);
		
		Map condition = new HashMap();
		if (isql != null && !"".equals(isql)) {
			condition.put("sql", isql);
		} else {
			condition.put("sql", null);
		}
		
		List iList = this.getSqlMapClientTemplate().queryForList("findDestValueBySql",condition);
		
		String destValue = "";
		for(Object rsobj : iList){
			if(rsobj != null){
				HashMap hashMap = (HashMap) rsobj;
				Set set = hashMap.entrySet();
				Iterator iterator = set.iterator();
				while (iterator.hasNext()) {
					Map.Entry mapentry = (Map.Entry) iterator.next();
					System.out.println(mapentry.getKey() + "/" + mapentry.getValue());
					rsobj = mapentry.getValue();
				
					if(rsobj instanceof Integer){
						log.info("111");
						Integer integer = (Integer)rsobj;
						destValue = integer.toString();
					}else if(rsobj instanceof Long){
						log.info("222");
						Long l = (Long)rsobj;
						destValue = l.toString();
					}else if(rsobj instanceof java.math.BigDecimal){
						log.info("333");
						java.math.BigDecimal b = (java.math.BigDecimal) rsobj;
						destValue = b.toString();
					}else if(rsobj instanceof java.sql.Date){
						log.info("444");
						destValue = rsobj.toString();
					}else if(rsobj instanceof java.sql.Clob){
						String reString = "";
						java.sql.Clob clob = (java.sql.Clob)rsobj;
						if (clob != null){
							//try {
								Reader is = clob.getCharacterStream();// 得到流
								BufferedReader br = new BufferedReader(is);
								String s = br.readLine();
								StringBuffer sb = new StringBuffer();
								while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
									sb.append(s);
									s = br.readLine();
								}
								reString = sb.toString();
							//} catch (SQLException e) {
							//	logger.error("ClobToString SQLException:", e);
							//} catch (IOException e) {
							//	logger.error("ClobToString IOException:", e);
							//}
						}
						destValue = reString;
					}else{
						log.info("555");
						destValue = (String) rsobj;
					}
				}
			}
		}*/
			
		/*
		Set set = hashMap.entrySet();
		Iterator iterator = set.iterator();
		String destValue = "";
		while (iterator.hasNext()) {
			Map.Entry mapentry = (Map.Entry) iterator.next();
			System.out.println(mapentry.getKey() + "/" + mapentry.getValue());
			Object rsobj = mapentry.getValue();
			if(rsobj != null){
				if(rsobj instanceof Integer){
					log.info("111");
					Integer integer = (Integer)rsobj;
					destValue = integer.toString();
				}else if(rsobj instanceof Long){
					log.info("222");
					Long l = (Long)rsobj;
					destValue = l.toString();
				}else if(rsobj instanceof java.math.BigDecimal){
					log.info("333");
					java.math.BigDecimal b = (java.math.BigDecimal) rsobj;
					destValue = b.toString();
				}else if(rsobj instanceof java.sql.Date){
					log.info("444");
					destValue = rsobj.toString();
				}else if(rsobj instanceof java.sql.Clob){
					String reString = "";
					java.sql.Clob clob = (java.sql.Clob)rsobj;
					if (clob != null){
						//try {
							Reader is = clob.getCharacterStream();// 得到流
							BufferedReader br = new BufferedReader(is);
							String s = br.readLine();
							StringBuffer sb = new StringBuffer();
							while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
								sb.append(s);
								s = br.readLine();
							}
							reString = sb.toString();
						//} catch (SQLException e) {
						//	logger.error("ClobToString SQLException:", e);
						//} catch (IOException e) {
						//	logger.error("ClobToString IOException:", e);
						//}
					}
					destValue = reString;
				}else{
					log.info("555");
					destValue = (String) rsobj;
				}
			}
		}*/
		return destValue;


 

搞了好久,,,

开始想配sqlmap

       <select id="findDestValueBySql" parameterClass="java.util.HashMap" remapResults="true" 
       		resultClass="java.util.HashMap">
       		$sql$
       </select>

 

普通语句可以通过,但是稍微复杂的sql就报错,什么Object里面没有这个字段,,,那个字段却是我sql的一个片段,

queryForList queryForMap queryForObject 各种测试未果,,最后只能改成最开始写的那样,,,

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值