发布于 

java,jdbcTemplate分页实现

PageList

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
package com.example.rcadmin.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageList<T> {

private int pageSize; //单页最大数据量

private int dataNumber; //Java类T 总的数据量

private int pageNumber; //总的页数 总的页数=(总的数据量%单页最大数据量)==0?(总的数据量/单页最大数据量):((总的数据量/单页最大数据量)+1)

private int currentPage; //当前页

private List<T> dataList = new ArrayList<T>(); //当前页的全部数据

public PageList(int currentPage,int pageSize,int dataNumber){
this.currentPage = currentPage;
this.pageSize = pageSize;
pageNumber = (dataNumber%pageSize==0?(dataNumber/pageSize):(dataNumber/pageSize+1));
}

}

pageTool

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;
}
}

rstoobj

1
2
3
4
5
6
7
8
package com.example.rcadmin.commonutils;

import java.sql.ResultSet;
import java.sql.SQLException;

public interface rstoobj<T> {
T rtodata(ResultSet rs) throws SQLException;
}

实体类实现接口

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
package com.example.rcadmin.model;

import com.example.rcadmin.commonutils.rstoobj;
import com.example.rcadmin.enums.bl_apply_to;
import com.example.rcadmin.enums.bl_type;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

//exrc数据库
@Data
@Builder
@ToString
@NoArgsConstructor //自动生成无参数构造函数。
@AllArgsConstructor //自动生成全参数构造函数
@ApiModel("未知")
public class blacklist implements rstoobj {
@ApiModelProperty("id")
private int id;
@ApiModelProperty("未知属性")
private bl_type type;
@ApiModelProperty("未知属性")
private String value;
@ApiModelProperty("未知属性")
private bl_apply_to apply_to;
@ApiModelProperty("未知属性")
private String exact_apply_to;
@ApiModelProperty("应该是创建时间")
private Date created_at;
@ApiModelProperty("应该是更新时间")
private Date updated_at;


@Override
public blacklist rtodata(ResultSet rs) throws SQLException {
return new blacklist(rs.getInt("id"),bl_type.valueOfName(rs.getString("type")), rs.getString("value"), bl_apply_to.valueOfName(rs.getString("apply_to")),
rs.getString("exact_apply_to"), rs.getDate("created_at"), rs.getDate("updated_at"));
}
}

service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.rcadmin.service;

import com.example.rcadmin.commonutils.pageTool;
import com.example.rcadmin.dao.blacklistDao;
import com.example.rcadmin.model.PageList;
import com.example.rcadmin.model.blacklist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class blacklistService {
@Autowired
private blacklistDao blacklistdao;
@Autowired
private pageTool pagetool;

public PageList<blacklist> getListByPage(int currentPage, int pageSize) {
return pagetool.getListByPage(2,10,"","","","blacklist",new blacklist());
}
}

controller

1
2
3
4
5
@PostMapping("/list")
public R list() {
blacklistservice.getListByPage(1,10);
return null;
}