问题
here is my following dao implementaion
@Override
public List<UserAddress> getAddresses(int pageid,int total) {
String sql = "select * FROM user_addresses order by id desc limit "+(pageid-1)+","+total;
List<UserAddress> userAddresses = jdbcTemplate.query(sql, new RowMapper<UserAddress>() {
@Override
public UserSessionLog mapRow(ResultSet rs, int rowNum) throws SQLException {
UserAddress userAdd = new UserAddress();
userAdd.setId(rs.getInt("id"));
userAdd.setId(rs.getString("city"));
return userSession;
}
});
return userAddresses;
}
in the above dao implementaion, i list all the user addresses, trying to list with limit
@RequestMapping("/userAddresses/{pageid}")
public ModelAndView userAddresses(@PathVariable int pageid) {
int total=5;
if(pageid==1){}
else{
pageid=(pageid-1)*total+1;
}
List<UserAddress> listAddresses = userAddressFacade.getAddresses(pageid,total);
return new ModelAndView("userAddresses", "listAddresses", listAddresses);
}
this is my view part,
<table class="table table-condensed">
<thead>
<tr>
<th>Address1</th>
<th>City</th>
</tr>
</thead>
<tbody>
<c:if test="${not empty addresses}">
<c:forEach var="address" items="${addresses}">
<tr>
<td>${address.address1}</td>
<td>${address.city}</td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
<br/>
<a href="/pro/userAddress/1">1</a>
<a href="/pro/userAddress/2">2</a>
<a href="/pro/userAddress/3">3</a>
I have hardcoded the pagination part, do any one have idea, how to do pagination. i am newbie to java jdbcTemplate,
回答1:
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):
@Repository
public class DemoRepository {
private JdbcTemplate jdbcTemplate;
@Autowired
public DemoRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Demo> 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<Demo> 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<Demo> 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);
}
}
回答2:
I came across this while I was searching for something else, and noticed this has not been answered, so thought to post my 2cents. You can create a wrapper (Request) object containing Pagination object and pageId.
Request
- Pagination pagination
- int pageId (any business related data/ SQL parameter)
- ANY DOMAIN OBJECT
Pagination
- int start (Use to set OFFSET property in the SQL)
- int size (Use to set the FETCH NEXT property in the SQL)
回答3:
You don't have to create own implementation logic for pagination. Use Spring's PagedListHolder, it's suitable and configurable for pagination purposes.
Here you can see an example implementation: Spring Pagination Example.
来源:https://stackoverflow.com/questions/42043701/how-can-i-implement-a-pagination-in-spring-jdbctemplate