Java 使用SpringJDBC模板填充结果
我有两节课Java 使用SpringJDBC模板填充结果,java,spring,jdbctemplate,Java,Spring,Jdbctemplate,我有两节课 class Deptartment{ int deptid, String deptname; List<Employee> employees; } class Employee{ int empid; String empname; int deptid; } Table: Department: deptid,deptname Employee empid,empname,deptid Query: select
class Deptartment{
int deptid,
String deptname;
List<Employee> employees;
}
class Employee{
int empid;
String empname;
int deptid;
}
Table:
Department:
deptid,deptname
Employee
empid,empname,deptid
Query: select * from deptartment d,employee e where d.deptid= e.deptid
类启动{
国际部,
字符串deptname;
列出员工名单;
}
班级员工{
int empid;
字符串名称;
国际部;
}
表:
部门:
deptid,deptname
雇员
empid,empname,deptid
查询:从部门d、员工e中选择*,其中d.deptid=e.deptid
现在,我如何使用spring jdbc模板填充Department对象?目前spring中不直接支持此方案。但是,您可以使用proposed将其包含到Spring的未来版本中。类似的操作可以:
Department department = this.jdbcTemplate.queryForObject(
"select deptid, deptname from Department where deptid = :deptid",
new Object[deptid],
new RowMapper<Department>() {
public Department mapRow(ResultSet rs, int rowNum) throws SQLException {
Department department = new Department();
department.setDeptid(rs.getInt("deptid"));
department.setDeptname(rs.getString("deptname"));
return department;
}
});
department.setEmployees(this.jdbcTemplate.query(
"select empid, empname from Employee",
new RowMapper<Employee>() {
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
Employee employee = new Employee();
employee.setEmpid(rs.getInt("emptid"));
employee.setEmpname(rs.getString("empname"));
return department;
}
});
Department Department=this.jdbcTemplate.queryForObject(
“从deptid=:deptid所在的部门中选择deptid,deptname”,
新对象[deptid],
新的行映射器(){
公共部门映射行(结果集rs,int rowNum)引发SQLException{
部门=新部门();
(rs.getInt);;
department.setDeptname(rs.getString(“deptname”);
退货部;
}
});
department.setEmployees(this.jdbcTemplate.query(
“从员工中选择empid、empname”,
新的行映射器(){
公共雇员mapRow(ResultSet rs,int rowNum)抛出SQLException{
员工=新员工();
employee.setEmpid(rs.getInt(“emptid”);
employee.setEmpname(rs.getString(“empname”);
退货部;
}
});
为了帮助肖恩·帕特里克·弗洛伊德,以下是他的解决方案,只需一个查询:
final Map<Integer, Department> departments = new HashMap<Integer, Department>();
this.jdbcTemplate.query(
"select d.deptid, d.deptname, e.empid, e.empname from Department d inner join Employee on e.deptid = e.deptid",
new RowMapper<Employee>() {
public Department mapRow(ResultSet rs, int rowNum) throws SQLException {
Integer deptId = rs.getInt("deptid");
Department d = (Department) departments.get(deptId);
if (d == null) {
String deptName = rs.getString("deptname");
d = new Department();
d.setDeptId(deptId);
d.setDeptName(deptName);
departments.put(deptId, d);
}
Employee employee = new Employee();
employee.setEmpId(rs.getInt("empid"));
employee.setEmpName(rs.getString("empname"));
employee.setDeptId(deptId);
d.getEmployees().add(employee);
return employee;
}
});
List<Department> result = new ArrayList<Department>(departments.values());
final Map departments=new HashMap();
this.jdbcTemplate.query(
“在e.deptid=e.deptid上从部门d内部加入员工中选择d.deptid、d.deptname、e.empid、e.empname”,
新的行映射器(){
公共部门映射行(结果集rs,int rowNum)引发SQLException{
整数deptId=rs.getInt(“deptId”);
Department d=(Department)departments.get(deptId);
如果(d==null){
String deptName=rs.getString(“deptName”);
d=新部门();
d、 setDeptId(deptId);
d、 setDeptName(deptName);
政府统计处(工业贸易署署长,丁),;
}
员工=新员工();
employee.setEmpId(rs.getInt(“empid”);
employee.setEmpName(rs.getString(“empname”);
employee.setDeptId(deptId);
d、 getEmployees().add(员工);
返回员工;
}
});
列表结果=新的ArrayList(departments.values());
它恰好更短、更高效。对于ResultTextRactor来说,这不是可行的吗?@abalogh:显然,你也可以手动完成,但建议的类简化了它。你是对的,我检查了链接,只是惊讶于你写了“无直接支持”,起初我理解为“不可行”:)所以,不是一个查询,您正在执行n+1(n是部门数)。我不认为这是一个好的解决方案。@JB Nizet:当然,使用联接会更有效,但是Java端会变得更复杂,而且我很懒。我想要一个代码,用于检索给定deptid的部门,因此将有一个包含列表的部门对象,你能提供一个代码片段吗it@akshay:那么为什么要指定一个查询,返回所有部门及其所有员工?是否可以在单独的类中提取整个代码段,哪一个实现了RowMapper?如果您可以使用ParameterizedBeanPropertyRowMapper.newInstance(例如ParameterizedBeanPropertyRowMapper.newInstance(Employee.class))来映射属性以及您所在部门的属性,而无需rs.gets,那将是一件好事。