JdbcTemplate查询中的问题

1、数据库表机构如下:

/*==============================================================*/
/* Table: function                                              */
/*==============================================================*/
CREATE TABLE `itams`.`function` (
  `function_id` VARCHAR(32) NOT NULL,
  `function_name` VARCHAR(50) NOT NULL,
  `parent_function_id` VARCHAR(32) NOT NULL,
  `function_order` INTEGER UNSIGNED,
  `function_level` INTEGER UNSIGNED,
  `function_url` VARCHAR(200),
  `function_remark` TEXT,
  PRIMARY KEY (`function_id`)
)
ENGINE = InnoDB
CHARACTER SET utf8 COLLATE utf8_general_ci;



2、有一个Function.java如下:

package com.zb.itams.domain;

import java.io.Serializable;

public class Function implements Serializable {
  private String functionId;
  private String functionName;
  private String parentFunctionId;
  private int functionOrder;
  private int functionLevel;
  private String functionUrl;
  private String functionRemark;
    
  /* JavaBeans Properties */
  // 省略setter和getter方法
}



3、FunctionDaoImpl中的基于Id查询的方法如下:

public Function getFunction(String functionId) throws DataAccessException {
  Iterator<Function> iteFunction = jdbcTemplate.queryForList("select * from function where function_id='"+functionId+"'").iterator();
        
  if (iteFunction.hasNext()) {
    return iteFunction.next();
  }
  else {
    return null;
  }
}


4、打印显示方法中的iteFunction.next()

{
  function_id=8abc8098269d0fd32949d11490e0000,
  function_name=ss,
  parent_function_id=8abc8098269c7b4a2949c7b4a010000,
  function_order=0,
  function_level=3,
  function_url=,
  function_remark=ss
}


无法强制转换成Function:return (Function)iteFunction.next();
报异常:java.lang.ClassCastException: java.util.LinkedHashMap

 

上网搜了半天,没找到原因和解决方法

 

可以把返回结果,需要实现RowMapper接口,使用一个匿名类最简单:

[code="java"]public void aaa(String id) {
Object[] sqlArgs = new Object[0];
String sql = "select * from function where function_id=" + id;
jdbcTemplate.query(sql, sqlArgs, new RowMapper() {
//@Override
public Function mapRow(ResultSet rs, int ar)
throws SQLException {
Function func = new Function();
func.setFunctionId(rs.getString(1));
func.setFunctionName(rs.getString(2));
// ...
return func;
}
});
}[/code]

jdbcTemplate.queryForList 返回的是一个List而不是一个List直接转肯定是出不来的……
你这样试试

[code="java"]
public void getFunction(String functionId) throws DataAccessException {
Iterator iteFunction = jdbcTemplate.queryForList("select * from function where function_id='"+functionId+"'").iterator();

if (iteFunction.hasNext()) {
Map functionMap = iteFunction.next();
System.out.println(functionMap.get("function_name"));
}
}
[/code]

public void aaa(String id) {

Object[] sqlArgs = new Object[0];

String sql = "select * from function where function_id=" + id;

List returnList = jdbcTemplate.query(sql, sqlArgs, new RowMapper() {

//@Override

public Function mapRow(ResultSet rs, int ar)

throws SQLException {

Function func = new Function();

func.setFunctionId(rs.getString(1));

func.setFunctionName(rs.getString(2));

// ...

return func;

}

});

}

返回List。