ORMLite中 DAO.queryRaw方法的用法

有时候,我们的SQL语句可能会比较复杂,用DAO的query方法可能不能满足需要,就需要调用queryRaw方法来查询原生的SQL语句。
下例中,ShortMessage是Mode类



	try {
			GenericRawResults results = messageDao
					.queryRaw(
							"select id,_to,_from,message,MAX(date),username,_type,read from message group by username order by date desc;",
							new RawRowMapper() {
								@Override
								public ShortMessage mapRow(
										String[] columnNames,
										String[] resultColumns)
										throws SQLException {
									// TODO Auto-generated method stub
									ShortMessage message = new ShortMessage();
									message.setId(Integer
											.parseInt(resultColumns[0]));
									message.setTo(resultColumns[1]);
									message.setFrom(resultColumns[2]);
									message.setMessage(resultColumns[3]);
									String dateStr = resultColumns[4];// 存储格式为2012-12-05
																		// 14:46:23.000927
									SimpleDateFormat sdf = new SimpleDateFormat(
											"yyyy-MM-dd HH:mm:ss");
									try {
										message.setDate(sdf.parse(dateStr));
									} catch (ParseException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
									message.setUsername(resultColumns[5]);
									message.setType(Integer
											.parseInt(resultColumns[6]));
									int readed = Integer
											.parseInt(resultColumns[7]);
									if (readed == 0) {
										message.setRead(false);
									} else {
										message.setRead(true);
									}
									return message;
								}
							});

			Iterator iterator = results.iterator();
			List messageList = new ArrayList();
			while (iterator.hasNext()) {
				ShortMessage message = iterator.next();
				messageList.add(message);
			}

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记