我正在尝试从数据库中检索连接表结果,而我当前的代码是这样的。
查询
SELECT a.Name,a.employeeNo,b.status,b.amount FROM Tbemployee a left join Tbtransact b on a.employeeno = b.employeeno WHERE b.status = '7'
代码
DBService dbsrvc = DBServiceImpl();
List<EmployeeForm> formList = new ArrayList<EmployeeForm>();
List<Object[]> list = dbsrvc.executeListSQLQuery(query, params);
for(Object[] row: list){
EmployeeForm form = new EmployeeForm();
form.setEmpno(row[0]);
form.setName(row[1]);
form.setStatus(row[2]);
form.setAmount(row[3]);
formList.add(form);
}
有更好的方法吗?比如将列表直接检索到EmployeeForm?
这是我的DBService方法
public List executeListSQLQuery(String sQuery, Map parameters) {
// ---
EMPLOYEEDB service = (EMPLOYEEDB) RuntimeAccess.getInstance().getServiceBean(
DBNAME);
try {
service.begin();
Session session = service.getDataServiceManager().getSession();
SQLQuery query = session.createSQLQuery(sQuery);
query.setProperties(parameters);
List list = query.list();
service.commit();
// ---
return list;
} catch (Exception ex) {
logger.warn("Error executing query", ex);
service.rollback();
}
return null;
}