关于YMP框架JDBC查询结果集自定义处理方法

final String _sql = "SELECT u.name as name, u.age, u.type, u.birth FROM ym_user u WHERE u.age > ?, u.type = ?";
// 1. 将SQL查询的结果集采用数组的形式封装
List<Object[]> _results = JDBC.openSession(new ISessionExecutor<List<Object[]>>() {
	public List<Object[]> execute(ISession session) throws OperatorException {
		return session.findAll(_sql, new ArrayResultSetHandler(), new Object[] { 18, "M"});
	}
});
// 2. 将SQL查询的结果集采用MAP的形式封装
List<Map<String, Object>> _results = JDBC.openSession(new ISessionExecutor<List<Map<String, Object>>>() {
	public List<Map<String, Object>> execute(ISession session) throws OperatorException {
		return session.findAll(_sql, new MapResultSetHandler(), new Object[] { 18, "M"});
	}
});
// 1和2的结果集遍历方法如下
ResultSetHelper _help = ResultSetHelper.bind(_results);
for (int _idx = 0; _idx < _help.getRowCount(); _idx++) {
	System.out.println(_help.getAsString("name"));
	System.out.println(_help.getAsInteger(2));
}
// 3. 自定义结果集记录处理过程
List<CustomUser> _results = JDBC.openSession(new ISessionExecutor<List<CustomUser>>() {
	public List<CustomUser> execute(ISession session) throws OperatorException {
		return session.findAll(_sql, new AbstractResultSetHandler<CustomUser>() {
			public void processRowData(ResultSet rs, List<CustomUser> result) throws OperatorException, SQLException {
				CustomUser _u = new CustomUser();
				_u.setUserName(rs.getString("name"));
				// ......
				result.add(_u);
			}
		}, new Object[]{18, "M"});
	}
});



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。