Java 如何使用SpringRESTAPI从hibernate中的两个表中获取数据并以单个对象返回url
我将SpringRESTfulAPI与hibernate结合使用。我使用两个名为Employee和Second的实体类从两个表中获取数据。我希望从两个表的列表中获得结果,并希望在单个json对象中返回结果 这是我的刀课Java 如何使用SpringRESTAPI从hibernate中的两个表中获取数据并以单个对象返回url,java,spring,hibernate,rest,spring-mvc,Java,Spring,Hibernate,Rest,Spring Mvc,我将SpringRESTfulAPI与hibernate结合使用。我使用两个名为Employee和Second的实体类从两个表中获取数据。我希望从两个表的列表中获得结果,并希望在单个json对象中返回结果 这是我的刀课 // Method to get the result from employee table @SuppressWarnings("unchecked") public List<Employee> getEntityList() throws Exception
// 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;
}
//从employee表获取结果的方法
@抑制警告(“未选中”)
公共列表getEntityList()引发异常{
session=sessionFactory.openSession();
tx=session.beginTransaction();
List employeeList=session.createCriteria(Employee.class)
.list();
tx.commit();
session.close();
返回员工名单;
}
//方法从第二个表中获取结果
@抑制警告(“未选中”)
public List getSecondList()引发异常{
session=sessionFactory.openSession();
tx=session.beginTransaction();
List secondList=session.createCriteria(Second.class)
.list();
tx.commit();
session.close();
返回第二个列表;
}
我的服务班
@Autowired
DataDao dataDao;
public List<Employee> getEntityList() throws Exception {
return dataDao.getEntityList();
}
public List<Second> getSecondList() throws Exception {
return dataDao.getSecondList();
}
@Autowired
DataDao DataDao;
公共列表getEntityList()引发异常{
返回dataDao.getEntityList();
}
public List getSecondList()引发异常{
返回dataDao.getSecondList();
}
这是我的控制器
@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;
}
@RequestMapping(value=“/list”,method=RequestMethod.GET)
公共@ResponseBody
列表getEmployee(){
List employeeList=null;
试一试{
employeeList=dataServices.getEntityList();
}捕获(例外e){
e、 printStackTrace();
}
返回员工名单;
}
这里的数据只来自一个表employee,但我也希望从第二个表中获取数据,并希望在employeeList中返回该数据。W
我该怎么办?请建议我。
Thanx提前@java开发者
问题是您不能将查询输出收集为单个提取。您需要创建一个单独的值对象类,该类以迭代方式映射到两个查询的结果
然后需要将这个值对象类映射为SpringRESTAPI的返回类型。示例如下所示:
客户端类从两个不同的表组成profile和advisor。
另外,请注意parseClientSpreadsheet方法是如何设置clientprofiles和ClientAdvisor的
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;
}
公共类客户端扩展了ReportModel{
私有静态最终长serialVersionUID=22929968356764522338L;
私人客户资料;
私人名单顾问;
public ClientProfile getProfile(){
回报曲线;
}
公共void setProfile(ClientProfile){
this.profile=profile;
}
公共列表getAdvisor(){
返回顾问;
}
公共无效设置顾问(列表顾问){
this.advisor=advisor;
}
}
@RequestMapping(method=RequestMethod.GET,value=“/list”)
公共列表getClients(){
列出客户名单;
//调用客户机服务并从数据库加载客户机数据,然后
//返回客户端列表。
试试{
clients=clientService.getClients();
}捕获(例外e){
文件\u error\u logger.error(“读取客户端时异常”,e);
投掷e;
}
if(logger.isDebugEnabled())logger.debug(“获取客户端详细信息成功”);
返回客户;
}
公共列表parseClientSpreadsheet(字符串clientDetailsFilePath,
int numberOfSheets)引发ClientDataNotFoundException{
List clientList=new ArrayList();
//如果行有效,则解析数据
如果(isValidRow)(行,最后一列,
ClientDataConstants.CLIENT(需要(列)){
ClientProfile ClientProfile;
List advisorList=new ArrayList();
客户端=新客户端();
//设置客户端配置文件对象值
clientProfile=setClientProfileObject(行);
//设置顾问列表
advisorList=setAdvisorList(行,最后一列);
//设置客户端对象
字符串[]additionalRecipients=行
格塞尔先生(
整数.parseInt(ReportMailerConfiguration服务
.getPropertyValue(ClientDataConstants.ADDITIONAL\u RECIPIENTS\u CELLNUMBER)))
.toString().split(“;”);
List additionalRecipientsList=new ArrayList();
//最多允许4个其他收件人。因此,如果需要更多收件人
//找到,在数组中取4,忽略其余部分
对于(int i=0;i
我想你可能需要这样的例子
@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;
}
}
@RestController
公共类EmployeeRestController{
@请求映射(value=“/employees”)
公共包装器getEmployees(){
Wrapper=getWrapper();
返回包装器;
}
公共包装器getWrapper(){
包装器=新包装器();
List employees=getemployees();
列表组织=getOrg();
包装器。setEmployees(员工);
3.3.3组织(组织);
返回包装器;
}
公共列表getEmployee(){
员工1=新员工(101,“abc”、“abc”、“SE”);
雇员雇员2=新雇员(102,“def”、“def”、“SE”);
雇员雇员3=新雇员