Java Spring 3.x + SimpleJdbcTemplate:返回多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24153477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.x + SimpleJdbcTemplate : Returning multiple columns
提问by user182944
I am using SimpleJdbcTemplate
with Spring 3.x
. For getting a single column, I use the below code and it works fine:
我使用SimpleJdbcTemplate
带Spring 3.x
。为了获得单列,我使用以下代码并且它工作正常:
public String selectSingleColumn(int deptId){
return jdbcTemplate.queryForObject("SELECT DEPT_NAME FROM DEPT WHERE DEPT_ID = ?", String.class, deptId);
}
Problem:
问题:
I want to fetch multiple columns such as DEPT_NAME
and DEPT_CODE
from that above table (but not all that belong to the table), how to modify the above code to get it done? I am confused with the second parameter
of the queryForObject
in this case; ideally I think it should be Object[]
but stilll confused. Please guide me.
我想取多列,如DEPT_NAME
和DEPT_CODE
从上面的表(但不是全部属于表),如何修改上面的代码来完成它?我很困惑与second parameter
的queryForObject
在这种情况下,理想情况下,我认为它应该Object[]
但仍然困惑。请指导我。
采纳答案by Paul Samsotha
I would just query for an entire domain object, instead of having to write different queries for different columns. For one, it makes the dao more reusable.
我只会查询整个域对象,而不必为不同的列编写不同的查询。一方面,它使 dao 更具可重用性。
For example:
例如:
Department domain object
部门域对象
public class Department {
private long id;
private String deptName;
private String deptCode;
// other fields
// getters and setters
}
DepartmentDao
部门道
public class DepartmentDaoImpl extends JdbcTemplate implements DepartmentDao {
private static final String DEPT_BY_ID
= "select * from DEPARTMENT where DEPT_ID = ?";
@Override
public Department getDepartmentById(long id) {
return (Department) queryForObject(
DEPT_BY_ID,
new Object[] { id },
new RowMapper<Department>() {
@Override
public Department mapRow(ResultSet rs, int rowNumber) {
Department dept = new Department();
dept.setId(rs.getLong("DEPT_ID");
dept.setDeptName(rs.getString("DEPT_NAME");
dept.setDeptCode(rs.getString("DEPT_CODE");
// set other properties
return dept;
}
});
}
}
If you really, really only want two columns, you could use queryForMap
如果你真的,真的只想要两列,你可以使用 queryForMap
public class TestCustomerDao extends JdbcTemplate implements DepartmentDao {
private static final String FOR_MAP
= "select DEPT_NAME,DEPT_CODE from DEPARTMENT where DEPT_ID = ?";
@Override
public Map<String, Object> getCoupleColumnsById(long id) {
return (Map<String, Object>)queryForMap(FOR_MAP, new Object[] {id});
}
}
The map will return as
地图将返回为
key value
DEPT_NAME = value
DEPT_CODE = value
回答by Punnoose
Given below are the steps to do this -
以下是执行此操作的步骤 -
(1) Create a domain Object for Department
(1)为部门创建域对象
public class Department{
private String departmentName;
private String departmentCode;
//getters and setters omitted for brevity
}
(2) Create a RowMapper class to map the result set to the Department Object
(2)创建一个RowMapper类,将结果集映射到Department对象
public class DepartmentRowMapper implements RowMapper<Department>
{
public Department mapRow(ResultSet rs, int rowNum) throws SQLException {
Department department= new Department ();
department.setDepartmentName(rs.getString("DEPT_NAME"));
department.setDepartmentCode(rs.getString("DEPT_CODE"));
return department;
}
}
(3) Create the Dao class
(3)创建Dao类
public class DepartmentDao
private JdbcTemplate jdbcTemplate;
//getters and setters omitted for brevity
public Department getDepartment(int deptId){
return (Department)jdbcTemplate.queryForObject("SELECT DEPT_NAME FROM DEPT WHERE DEPT_ID = ?",new Object[] {deptId}, new DepartmentRowMapper ());
}
}