企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
查询常用方法如下: <br/> **1. `Map<String, Object> queryForMap(String sql, @Nullable Object... args)`** ```java String sql = "select id, name, age, gender from student where id=?"; Map<String, Object> dataMap = jdbcTemplate.queryForMap(sql, new Object[]{51}); ``` **2. `<T> List<T> query(String sql, RowMapper<T> rowMapper)`** ```java String sql = "select id, name, age, gender from student"; List<Student> dataList = jdbcTemplate.query(sql, new RowMapper<Student>() { @Override public Student mapRow(ResultSet rs, int i) throws SQLException { Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); student.setGender(rs.getString("gender")); return student; } }); ``` **3. `<T> List<T> query(String sql, @Nullable Object[] args, RowMapper<T> rowMapper)`** ```java String sql = "select id, name, age, gender from student where id = ?"; List<Student> dataList = jdbcTemplate.query(sql, new Object[]{51}, new RowMapper<Student>() { @Override public Student mapRow(ResultSet rs, int i) throws SQLException { Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); student.setGender(rs.getString("gender")); return student; } }); ``` **4. `<T> List<T> query(String sql, @Nullable PreparedStatementSetter pss, RowMapper<T> rowMapper)`** ```java String sql = "select id, name, age, gender from student where id = ?"; List<Student> dataList = jdbcTemplate.query(sql, new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setInt(1, 51); } }, new RowMapper<Student>() { @Override public Student mapRow(ResultSet rs, int i) throws SQLException { Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); student.setGender(rs.getString("gender")); return student; } }); ``` **5. `List<Map<String, Object>> queryForList(String sql)`** ```java String sql = "select id, name, age, gender from student"; List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql); ``` **6. `<T> List<T> queryForList(String sql, Class<T> elementType)`** ```java String sql = "select id, name, age, gender from student"; List<Student> dataList = jdbcTemplate.queryForList(sql, Student.class); ``` **7. `List<Map<String, Object>> queryForList(String sql, @Nullable Object... args)`** ```java String sql = "select id, name, age, gender from student where id=?"; List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql, new Object[]{51}); ``` **8. ` <T> List<T> queryForList(String sql, Class<T> elementType, @Nullable Object... args)`** ```java String sql = "select id, name, age, gender from student where id=?"; List<Student> dataList = jdbcTemplate.queryForList(sql, Student.class, new Object[]{51}); ``` **9. 分页查询** `<T> List<T> queryForList(String sql, Class<T> elementType, @Nullable Object... args)` ```java //当前页 int page = 1; //页面大小 int size = 10; String sql = "select id, name, age, gender from student where gender=? limit ?,?"; List<Student> dataList = jdbcTemplate.queryForList(sql, Student.class, new Object[]{"男", (page - 1) * size, size}); ``` **10. Oracle的分页查询语句** ```sql SELECT * FROM (SELECT A.*,ROWNUM AS RN FROM (SELECT * FROM ${tableName} WHERE 1=1 -- 条件 ORDER BY CREATETIME DESC,ID -- 排序 ) A WHERE A.ROWNUM <= #{pageNum}*#{pageSize} ) B WHERE B.RN > #{pageNum-1}*#{pageSize} /* 由于oracle排序算法问题,如果排序遇到相同的条件,比如时间, 会使分页后一页包含前一页的内容,所以这个时候添加一个唯一的字段,比如ID。 */ ```