如何编写一个通用方法来进行GET请求,从数据库中获取数据?

huangapple 未分类评论49阅读模式
英文:

How to write a generic method for get request which is fetching data from Databased?

问题

以下是你提供的代码的翻译部分:

DAO类中的方法:

// 获取员工主列表
public List<EmployeeMaster> getEmployeeMasterList() {
    return appHibernateUtils.getSession(sessionFactory)
        .createCriteria(EmployeeMaster.class).list();
}

// 根据员工ID获取员工列表
public EmployeeMaster getEmployeeListById(Integer empId) {
    if (empId == 0 || empId == null) {
        return null;
    }
    return (EmployeeMaster) appHibernateUtils.getSession(sessionFactory)
        .createCriteria(EmployeeMaster.class)
        .add(Restrictions.eq(AppConstant.EMP_ID, empId))
        .uniqueResult();
}

Service类中的方法:

// 获取员工主列表
public List<EmployeeMasterDto> getEmployeeMasterList() {
    List<EmployeeMaster> empDataList = empMasterDao.getEmployeeMasterList();

    List<EmployeeMasterDto> listToSend = new ArrayList<>();
    for (EmployeeMaster data : empDataList) {
        EmployeeMasterDto dtoObject = new EmployeeMasterDto();
        dtoObject.setEmpId(data.getEmpId());
        dtoObject.setEmpName(data.getEmpName());
        listToSend.add(dtoObject);
    }
    return listToSend;
}

// 根据员工ID获取员工列表
public EmployeeMasterDto getEmployeeListById(Integer empId) {
    if (empId == null)
        return null;

    EmployeeMaster empData = empMasterDao.getEmployeeListById(empId);
    EmployeeMasterDto dataToSend = new EmployeeMasterDto();
    dataToSend.setEmpId(empData.getEmpId());
    dataToSend.setEmpName(empData.getEmpName());
    return dataToSend;
}

如果可能的话,请帮助我构建一个从上述两个方法中创建一个通用方法。

英文:

I am new to coding and trying to create method in more generic way. can someone help me here to understand the generics.

I have two method in my DAO class getEmployeeMasterList() and getEmployeeListById(Integer empId) to fetch record from database:

    public List&lt;EmployeeMaster&gt; getEmployeeMasterList() {
	
	return appHibernateUtils.getSession(sessionFactory).createCriteria(EmployeeMaster.class).list();
}

    public EmployeeMaster getEmployeeListById(Integer empId) {
	
	if(empId == 0 || empId == null) {
		return null;
	}
	
	return (EmployeeMaster) appHibernateUtils.getSession(sessionFactory)
	.createCriteria(EmployeeMaster.class)
	.add(Restrictions.eq(AppConstant.EMP_ID, empId))
	.uniqueResult();
}

Can someone help me how to construct a generic method from above two if it is possible.

And in service class I have two method which set database data with Pojo and send to UI

    public List&lt;EmployeeMasterDto&gt; getEmployeeMasterList() {
	
	List&lt;EmployeeMaster&gt; empDataList= empMasterDao.getEmployeeMasterList();

	List&lt;EmployeeMasterDto&gt; listToSend = new ArrayList&lt;&gt;();
	for(EmployeeMaster data : empDataList) {
		EmployeeMasterDto dtoObject = new EmployeeMasterDto();
		dtoObject.setEmpId(data.getempId());
		dtoObject.setEmpName(data.getEmpName());
		
		listToSend.add(dtoObject);
	}
	return listToSend;
}


    public EmployeeMasterDto getEmployeeListById(Integer empId) {
	
	if(empId == null)
		return null;
	
	EmployeeMaster empData = empMasterDao.getEmployeeListById(empId);		
	EmployeeMasterDto dataToSend = new EmployeeMasterDto();
	dataToSend.setEmpId(empData.getEmpId());
	dataToSend.setEmpName(empData.getEmpName());
	return dataToSend;
}

Please help me here to construct a generic method from the above two methods.

huangapple
  • 本文由 发表于 2020年5月4日 00:26:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/61577880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定