【问题标题】:How to return a list of objects using MappingSqlQuery如何使用 MappingSqlQuery 返回对象列表
【发布时间】:2015-11-11 09:24:07
【问题描述】:

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

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

【问题讨论】:

    标签: java sql sql-server mapping


    【解决方案1】:
     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 语法。并且where 子句中的&lt;&gt; null 条件已替换为is not null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多