抱歉,标题问题的框架很糟糕。
但基本上如果我有两个类,员工和地址。 Employee 引用了 Address 的实例。
Class Employee{
@Id
Integer id;
@Column(name="Name")
private String name;
@Column(name="Address")
private Id addressId;
@ManyToOne(name="Address", referencedColumnName="ID", insertable=false, updatable=false)
private Address address;
}
现在使用 spring-hibernate,
当我执行类似 "Select * from Employee"
的操作时,它不仅会获取每个员工记录,还会获取相应的包含地址记录。
这就是 hibernate 的方式。我不仅获得了地址 ID,还获得了每个员工对象中的完整地址数据。
所以我可以做一些类似employee.getAddress().getName()的事情
但问题是由于一些奇怪的客户端要求,我们被迫只在代码中使用过程来访问数据,而过程又会调用查询。所以我们的代码中没有直接查询。 但是通过程序我们将无法利用spring-hibernate的这个特性。
程序是否可以执行相同的操作(返回每个员工对象中每个员工的完整数据)??
最佳答案
基本上你可以使用RowMapper
来实现。您的程序应返回已加入的数据员工和地址。使用 JdbcTemplate
的简单示例:
public List<Employee> getEmployees(){
final String sql = "CALL employees_procedure";
final List<Employee> employees = (Employee) jdbcTemplate.queryForList("CALL employees_procedure", new EmployeeRowMapper());
return employees ;
}
public class EmployeeRowMapper implements RowMapper {
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
Employee employee= new Employee ();
employee.set(rs.getString("e.id"));
...
return employee;
}
}
关于java - 是否可以使用过程从数据库 MVC 风格获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35426290/