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"}); } });
时间: 2024-10-11 02:49:09