here is my following dao implementaion
@Override
public List getAddresses(int pageid,int total) {
String sql = \"select * FRO
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);
}
}