这里只是做一个示意性的例子,具体还是需要参看具体情况
@Controller
public class BaseControler
{
protected interface DoPage{
public PageInfo doInitPage(HttpServletRequest request, PageInfo page);
}
public String findByPage(final DoPage doPage, final HttpServletRequest request){
int p = NumberUtils.toInt(request.getParameter("page"), 1);
int rows = NumberUtils.toInt(request.getParameter("rows"), 10);
PageInfo page = new PageInfo();
page.setPage(p);
page.setRows(rows);
page = doPage.doInitPage(request, page);
return JSONUtil.outputJSONDataByList(page);
}
}
@Controller
public class TestController extends BaseControler
{
@Autowired
private ITestService testService;
@RequestMapping(value = "/testData", produces = "text/plain;charset=UTF-8")
@ResponseBody
public String testData(HttpServletRequest request){
return findByPage(new DoPage(){
@Override
public PageInfo doInitPage(HttpServletRequest request, PageInfo page)
{
String keyword = request.getParameter("keyword");
return testService.selectList(keyword, page);
}
}, request);
}
}
@Service
public class TestService implements ITestService{
@Resource
private SqlSession sqlSession;
@Override
public PageInfo selectList(String keyword, PageInfo page)
{
HashMap<String, Object> params = new HashMap<String, Object>();
String k = StringUtils.trimToNull(keyword);
if (k != null)
{
params.put("keyword", SqlUtils.generateLikeString(k));
}
params.put("page", page);
List<Map> result = sqlSession.selectList("com.test.dao.TestMapper.selectListPage", params);
page.setResultsList(result);
return page;
}
}