如何使用MappingSqlQuery返回对象列表

时间:2015-08-18 12:27:57

标签: java sql sql-server mapping

我有以下类必须返回对象列表

private class getOfficeNumberQuery extends MappingSqlQuery<OfficeNumberVO>
    {
        private Log log = LogFactory.getLog(GetAccruedBalanceForAssociateQuery.class);

        static private final String SQL = " SELECT b.xxxx AS associateMasterRecordId,a.zzz AS accruedHour, offices.yyy AS officeNumber  "
                + " FROM table3  a, table1 b, table2 offices WHERE a.Aident = b.Aident AND offices.branchId = b.branchId AND (a.AvailableBalance <> 0.0000 OR a.AvailableBalance <> null) "
                + " AND offices.BranchName IN(?)) ";

        public getOfficeNumberQuery(DataSource ds)
        {
            super(ds, SQL.toString());
            super.declareParameter(new SqlParameter("BranchName", Types.VARCHAR));

            try
            {
                compile();
            }
            catch (InvalidDataAccessApiUsageException e)
            {
                log.error("getOfficeNumberQuery(DataSource)", e); //$NON-NLS-1$

                throw new StaffTrackSystemFailureException(e);
            }
        }

        @Override
        protected OfficeNumberVO mapRow(ResultSet rs, int rowNum) throws SQLException
        {
            OfficeNumberVO payInfo = new OfficeNumberVO() ;

            payInfo.setAssociateMasterRecordId(rs.getLong("associateMasterRecordId"));
            payInfo.setAccruedHour(rs.getLong("accruedHour"));
            payInfo.setOfficeNumber(rs.getString("officeNumber"));

            return payInfo;
        }

    }

如何修改此方法以返回对象列表

@Override
            protected OfficeNumberVO mapRow(ResultSet rs, int rowNum) throws SQLException
            {
                OfficeNumberVO payInfo = new OfficeNumberVO() ;

                payInfo.setAssociateMasterRecordId(rs.getLong("associateMasterRecordId"));
                payInfo.setAccruedHour(rs.getLong("accruedHour"));
                payInfo.setOfficeNumber(rs.getString("officeNumber"));

                return payInfo;
            }

也有人会帮我查一下SQL是否合适?

环境:SQL Server

1 个答案:

答案 0 :(得分:0)

 SELECT b.xxxx AS associateMasterRecordId,
 a.zzz AS accruedHour, offices.yyy AS officeNumber
 FROM table3 a JOIN table1 b ON a.Aident = b.Aident
 JOIN table2 offices ON offices.branchId = b.branchId
 WHERE a.AvailableBalance <> 0.0000 OR a.AvailableBalance is not null
 AND offices.BranchName IN(?)

您的SQL进行了一些更改。首先,使用ANSI SQL join语法。 <> null子句中的where条件已替换为is not null

相关问题