我正在使用Spring restful api和hibernate。我使用两个名为Employee和Second的实体类从两个表中获取数据。我想在两个表的列表中得到结果,并希望在单个json对象中返回它。
这是我的DAO课程
// Method to get the result from employee table
@SuppressWarnings("unchecked")
public List<Employee> getEntityList() throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
List<Employee> employeeList = session.createCriteria(Employee.class)
.list();
tx.commit();
session.close();
return employeeList;
}
// Method to get the result from second table
@SuppressWarnings("unchecked")
public List<Second> getSecondList() throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
List<Second> secondList = session.createCriteria(Second.class)
.list();
tx.commit();
session.close();
return secondList;
}
我的服务类
@Autowired
DataDao dataDao;
public List<Employee> getEntityList() throws Exception {
return dataDao.getEntityList();
}
public List<Second> getSecondList() throws Exception {
return dataDao.getSecondList();
}
这是我的RestController
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody
List<Employee> getEmployee() {
List<Employee> employeeList = null;
try {
employeeList = dataServices.getEntityList();
} catch (Exception e) {
e.printStackTrace();
}
return employeeList;
}
这里的数据只来自一个表员工,但我也希望从第二个表中获取数据,并希望在employeeList中返回该数据。 w ^
我该怎么办请建议我。 Thanx提前
答案 0 :(得分:1)
我想你可能需要这样的例子
@RestController
public class EmployeeRestController {
@RequestMapping(value = "/employees")
public Wrapper getEmployees() {
Wrapper wrapper = getWrapper();
return wrapper;
}
public Wrapper getWrapper() {
Wrapper wrapper = new Wrapper();
List<Employee> employees = getEmployee();
List<Organizations> organizations = getOrg();
wrapper.setEmployees(employees);
wrapper.setOrganizations(organizations);
return wrapper;
}
public List<Employee> getEmployee() {
Employee employee1 = new Employee(101, "abc", "abc", "SE");
Employee employee2 = new Employee(102, "def", "def", "SE");
Employee employee3 = new Employee(103, "xyz", "xyz", "SE");
List<Employee> employees = new ArrayList<Employee>();
employees.add(employee1);
employees.add(employee2);
employees.add(employee3);
return employees;
}
public List<Organizations> getOrg() {
Organizations organizations1 = new Organizations();
organizations1.setName("Google");
Organizations organizations2 = new Organizations();
organizations2.setName("Facebook");
Organizations organizations3 = new Organizations();
organizations3.setName("Apple");
List<Organizations> organizations = new ArrayList<Organizations>();
organizations.add(organizations1);
organizations.add(organizations2);
organizations.add(organizations3);
return organizations;
}
}
public class Wrapper {
private List<Employee> employees;
private List<Organizations> organizations;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public List<Organizations> getOrganizations() {
return organizations;
}
public void setOrganizations(List<Organizations> organizations) {
this.organizations = organizations;
}
}
这里的Organization和Employee是两个bean类,它们被设置在包装类中。
因此,在您的情况下,您从两个表中获取这两个不同的对象,并将它们包装在一个Wrapper类中并将其发回。
我希望这可以帮到你!!
答案 1 :(得分:0)
@java开发者。
问题是您无法将查询输出收集为单个提取。您需要创建一个单独的值对象类,它迭代地映射到两个查询的结果。
然后,您需要将此值对象类映射为spring rest API的返回类型。示例示例如下所示: 客户端类正在从两个不同的表组成配置文件和顾问。 另请注意parseClientSpreadsheet方法如何设置clientprofiles和clientadvisors。
public class Client extends ReportModel {
private static final long serialVersionUID = 2292996835674522338L;
private ClientProfile profile;
private List<Advisor> advisor;
public ClientProfile getProfile() {
return profile;
}
public void setProfile(ClientProfile profile) {
this.profile = profile;
}
public List<Advisor> getAdvisor() {
return advisor;
}
public void setAdvisor(List<Advisor> advisor) {
this.advisor = advisor;
}
}
@RequestMapping(method = RequestMethod.GET, value = "/list")
public List<Client> getClients() {
List<Client> clients;
// Call the client service and load client data from the database and
// return the list of clients.
try {
clients = clientService.getClients();
} catch (Exception e) {
file_error_logger.error("Exception while reading clients", e);
throw e;
}
if (logger.isDebugEnabled()) logger.debug("Get client details successful.");
return clients;
}
public List<Client> parseClientSpreadsheet(String clientDetailsFilePath,
int numberOfSheets) throws ClientDataNotFoundException {
List<Client> clientList = new ArrayList<Client>();
// if the row is valid, parse the data
if (isValidRow(row, lastColumn,
ClientDataConstants.CLIENT_REQUIRED_COLUMNS)) {
ClientProfile clientProfile;
List<Advisor> advisorList = new ArrayList<Advisor>();
Client client = new Client();
// Set client profile object values
clientProfile = setClientProfileObject(row);
// set Advisor list
advisorList = setAdvisorList(row, lastColumn);
// set Client object
String[] additionalRecipients = row
.getCell(
Integer.parseInt(reportMailerConfigurationService
.getPropertyValue(ClientDataConstants.ADDITIONAL_RECIPIENTS_CELLNUMBER)))
.toString().split(";");
List<String> additionalRecipientsList = new ArrayList<String>();
// max 4 additional recipients are allowed. So if more are
// found, take 4 in array and ignore the rest
for (int i = 0; i < additionalRecipients.length; i++) {
if (!additionalRecipients[i].isEmpty()) {
additionalRecipientsList.add(additionalRecipients[i]);
if (additionalRecipientsList.size() == 4) {
break;
}
}
}
client.setProfile(clientProfile);
client.setAdvisor(advisorList);
client.setadditional_recipients(additionalRecipientsList);
// add the client in the collection
clientList.add(client);
}
}
return clientList;
}