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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:30:57  来源:igfitidea点击:

Spring 3.x + SimpleJdbcTemplate : Returning multiple columns

javaspringspring-3spring-jdbc

提问by user182944

I am using SimpleJdbcTemplatewith Spring 3.x. For getting a single column, I use the below code and it works fine:

我使用SimpleJdbcTemplateSpring 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_NAMEand DEPT_CODEfrom 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 parameterof the queryForObjectin this case; ideally I think it should be Object[]but stilll confused. Please guide me.

我想取多列,如DEPT_NAMEDEPT_CODE从上面的表(但不是全部属于表),如何修改上面的代码来完成它?我很困惑与second parameterqueryForObject在这种情况下,理想情况下,我认为它应该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 ());
   }
}