Pertanyaan menggunakan template spring jdbc untuk mengisi hasil


Saya memiliki dua kelas

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

Sekarang bagaimana saya bisa mengisi objek Departemen menggunakan template spring jdbc?


5
2018-06-29 14:05


asal


Jawaban:


Untuk membantu Sean Patrick Floyd, berikut solusinya dengan satu pertanyaan:

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());    

Itu terjadi menjadi lebih pendek dan lebih efisien.


10
2018-06-29 14:42



Tidak ada dukungan langsung dari skenario ini di Spring saat ini. Namun, Anda bisa menggunakannya pengikut MultipleRowMapper kelas diusulkan untuk dimasukkan ke versi Spring mendatang.


2
2018-06-29 14:12



Sesuatu seperti ini akan dilakukan:

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;
                }
            });

0
2018-06-29 14:20