MyBatis分页插件应用
1、前台代码(element-ui结构省略)
1.1 定义分页相关模型数据
pagination: {
//分页相关模型数据 currentPage: 1,//当前页码 pageSize:10,//每页显示的记录数 total:0,//总记录数 queryString:null//查询条件 }, dataList: [],//当前页要展示的分页列表数据
1.2 分页定义方法
在页面中提供了findPage方法用于分页查询,为了能够在checkitem.html页面加载后直接可以展示分页数据,可以在VUE提供的钩子函数created中调用findPage方法
//钩子函数,VUE对象初始化完成后自动执行 created() {
this.findPage(); }
1.3 完善分页方法执行时机
除了在created钩子函数中调用findPage方法查询分页数据之外,当用户点击查询按钮或者点击分页条中的页码时也需要调用findPage方法重新发起查询请求。
为查询按钮绑定单击事件,调用findPage方法
<el-button @click="findPage()" class="dalfBut">查询</el-button>
为分页条组件绑定current-change事件,此事件是分页条组件自己定义的事件,当页码改变时触发,对应的处理函数为handleCurrentChange
<el-pagination class="pagiantion" @current-change="handleCurrentChange" :current-page="pagination.currentPage" :page-size="pagination.pageSize" layout="total, prev, pager, next, jumper" :total="pagination.total"> </el-pagination>
定义handleCurrentChange方法
//切换页码 handleCurrentChange(currentPage) {
//currentPage为切换后的页码 this.pagination.currentPage = currentPage; this.findPage(); }
2、后台代码
2.1 Controller
在CheckItemController中增加分页查询方法
// 检查项分页查询 @RequestMapping("/findPage") public PageResult add(@RequestBody QueryPageBean queryPageBean){
PageResult pageResult = checkItemService.pageQuery(queryPageBean); return pageResult; }
2.2 服务接口
在CheckItemService服务接口中扩展分页查询方法
public interface CheckItemService {
PageResult pageQuery(QueryPageBean queryPageBean); }
2.3 服务实现类
在CheckItemServiceImpl服务实现类中实现分页查询方法,基于Mybatis分页助手插件实现分页
@Override // 检查项分页查询 public PageResult pageQuery(QueryPageBean queryPageBean) {
Integer currentPage = queryPageBean.getCurrentPage(); Integer pageSize = queryPageBean.getPageSize(); String queryString = queryPageBean.getQueryString(); // 做的时候发现的一个bug,在除了第一页按条件查询的时候,不显示查询结果,原因就是当前页面不为第一页,所以要判断是否有查询条件,有则将当前页面设置为第一页 if(queryString != null && queryString.length() > 0){
currentPage = 1; } // 完成分页插件,基于mybatis框架提供的分页助手插件完成 PageHelper.startPage(currentPage, pageSize); Page<CheckItem> page = checkItemDao.selectByCondition(queryString); long total = page.getTotal(); List<CheckItem> rows = page.getResult(); return new PageResult(total,rows); }
2.4 Dao接口
在CheckItemDao接口中扩展分页查询方法
@Mapper public interface CheckItemDao {
Page<CheckItem> selectByCondition(String queryString); }
2.5 Mapper映射文件
在CheckItemDao.xml文件中增加SQL定义
<select id="selectByCondition" parameterType="string" resultType="com.itheima.pojo.CheckItem"> select * from t_checkitem <if test="value != null and value.length > 0"> where code = #{value} or name = #{value}
if>
select>
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178164.html原文链接:https://javaforall.net
