how can i implement a pagination in spring jdbcTemplate

后端 未结 4 1816
时光取名叫无心
时光取名叫无心 2021-01-13 23:22

here is my following dao implementaion

@Override
    public List getAddresses(int pageid,int total) {

        String sql = \"select * FRO         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-14 00:06

    This can be done as long as your database supports LIMIT and OFFSET.

    An example is given here. The critical code is shown below (you can ignore the fluent builder clauses):

    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.Pageable;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class DemoRepository {
        private JdbcTemplate jdbcTemplate;
    
        @Autowired
        public DemoRepository(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        public List findDemo() {
            String querySql = "SELECT name, action, operator, operated_at " +
                    "FROM auditing " +
                    "WHERE module = ?";
            return jdbcTemplate.query(querySql, new Object[]{Module.ADMIN_OPERATOR.getModule()}, (rs, rowNum) ->
                    Demo.builder()
                            .rowNum(rowNum)
                            .operatedAt(rs.getTimestamp("operated_at").toLocalDateTime())
                            .operator(rs.getString("operator"))
                            .action(rs.getString("action"))
                            .name(rs.getString("name"))
                            .build()
            );
        }
    
        public Page findDemoByPage(Pageable pageable) {
            String rowCountSql = "SELECT count(1) AS row_count " +
                    "FROM auditing " +
                    "WHERE module = ? ";
            int total =
                    jdbcTemplate.queryForObject(
                            rowCountSql,
                            new Object[]{Module.ADMIN_OPERATOR.getModule()}, (rs, rowNum) -> rs.getInt(1)
                    );
    
            String querySql = "SELECT name, action, operator, operated_at " +
                    "FROM auditing " +
                    "WHERE module = ? " +
                    "LIMIT " + pageable.getPageSize() + " " +
                    "OFFSET " + pageable.getOffset();
            List demos = jdbcTemplate.query(
                    querySql,
                    new Object[]{Module.ADMIN_OPERATOR.getModule()}, (rs, rowNum) -> Demo.builder()
                            .rowNum(rowNum)
                            .operatedAt(rs.getTimestamp("operated_at").toLocalDateTime())
                            .operator(rs.getString("operator"))
                            .action(rs.getString("action"))
                            .name(rs.getString("name"))
                            .build()
            );
    
            return new PageImpl<>(demos, pageable, total);
        }
    }
    

提交回复
热议问题