1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package com.example.rcadmin.commonutils;
import com.example.rcadmin.model.PageList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository;
import java.sql.ResultSet; import java.sql.SQLException; import java.util.List;
@Repository public class pageTool<T> {
@Autowired private JdbcTemplate jdbcTemplate;
public List<T> findAll(String sql, String TableName, rstoobj re) { if (sql.equals("")) sql = "select * from " + TableName; return jdbcTemplate.query(sql, new RowMapper<T>() { @Override public T mapRow(ResultSet rs, int rowNum) throws SQLException { return (T) re.rtodata(rs); } }); }
public PageList<T> getListByPage(int currentPage, int pageSize, String findAllsql, String DataNumbersql, String ListByPagesql, String TableName, rstoobj re) { if (DataNumbersql.equals("")) DataNumbersql = "SELECT count(id) FROM " + TableName; if (ListByPagesql.equals("")) ListByPagesql = "SELECT * FROM " + TableName + " limit ?,?";
List<T> dataList = findAll(findAllsql, TableName, re); int dataNumber = dataList.size(); PageList<T> dataPageList = new PageList<>(currentPage, pageSize, dataNumber); dataPageList.setDataNumber(jdbcTemplate.queryForObject(DataNumbersql, Integer.class)); if (dataPageList.getCurrentPage() == dataPageList.getPageNumber()) { dataPageList.setDataList(jdbcTemplate.query(ListByPagesql, new RowMapper<T>() { @Override public T mapRow(ResultSet rs, int rowNum) throws SQLException { return (T) re.rtodata(rs); } }, new Object[]{(currentPage - 1) * pageSize, dataPageList.getDataNumber() - (currentPage - 1) * pageSize})); } else { dataPageList.setDataList(jdbcTemplate.query(ListByPagesql, new RowMapper<T>() { @Override public T mapRow(ResultSet rs, int rowNum) throws SQLException { return (T) re.rtodata(rs); } }, new Object[]{(currentPage - 1) * pageSize, pageSize})); } return dataPageList; } }
|