java - 如何使用 spring Rest api 从 hibernate 中的两个表中获取数据并以单个对象返回到 url

标签 java spring hibernate rest spring-mvc

我正在使用 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;
}

这里的数据仅来自一个表employee,但我也想从第二个表获取数据并希望在employeeList 中返回该数据。

我应该做什么,请建议我。 提前致谢

最佳答案

我想你可能需要这种例子

@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 类中并将其发送回。

希望这对你有帮助!!

关于java - 如何使用 spring Rest api 从 hibernate 中的两个表中获取数据并以单个对象返回到 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199158/

相关文章:

java - Java 中最大精度的测试编号

spring - 在其中保存带有缓存对象的实体导致分离实体异常

java - Spring,在 Hibernate 中创建当前日期的注释?

java - 如何使用 @ComponentScan 排除两个同名包之一?

hibernate - 查看 NHibernate session 对象

java - 将多个表映射到单个实体

java - "spring.config.location"系统属性的 Spring 常数

java - id 具有相同列名的对象的 ManyToMany 关系

java - 音频播放不正常

java - Spring @Scheduled 在 @Configuration 类中