分页插件查询
Posted hong999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分页插件查询相关的知识,希望对你有一定的参考价值。
springmvc-servlet配置:
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="typeAliases" value="cn.kgc.util.PageUtil"/>-->
<property name="typeAliasesPackage" value="cn.kgc.vo;cn.kgc.util"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--分页插件版本使用4的版本-->
<property name="plugins" >
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>dialect=mysql</value>
</property>
</bean>
</array>
</property>
</bean>
分页查询,控制层
package cn.kgc.controller;
import cn.kgc.service.EmpService;
import cn.kgc.vo.Emp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/emp")
public class EmpController
@Autowired
private EmpService empService;
@RequestMapping("/page.do")
public String showPage(Model model, @RequestParam(value = "pageno",
required = false,defaultValue = "1")Integer pageno,
@RequestParam(value = "deptno",required = false,
defaultValue = "-1")Integer deptno)
//设置分页格式,仅对下面第一个集合有效
Page page= PageHelper.startPage(pageno, 3);
//排序方式
PageHelper.orderBy("empno desc");
//查询信息,受分页格式影响
List<Map<String, Object>> emplist = empService.showPage(new Emp(deptno));
model.addAttribute("emplist", emplist);
//page信息放入PageInfo,否则前台无法接受
PageInfo pageinfo = new PageInfo(emplist);
System.out.println(emplist);
System.out.println(pageinfo);
model.addAttribute("deptno",deptno);
model.addAttribute("pageinfo", pageinfo);
return "/main.jsp";
分页,映射xml文件的SQL语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kgc.mapper.EmpMapper">
<!--各种查询-->
<select id="selectEmp" parameterType="Emp" resultType="map">
select d.dname,d.loc,e.* from emp e,dept d where e.deptno=d.deptno
<if test="empno!=null">
and e.empno=#empno
</if>
<if test="deptno!=null and deptno!=-1">
and d.deptno=#deptno
</if>
</select>
</mapper>
basedao接口
package cn.kgc.mapper;
import cn.kgc.vo.Emp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2019/6/26.
*/
public interface EmpMapper
//查询(查询所有,根据empno查询,根据detpno查询),mapper.xml
List<Map<String,Object>> selectEmp(Emp emp);
service实现类:
package cn.kgc.service;
import cn.kgc.mapper.EmpMapper;
import cn.kgc.vo.Emp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class EmpServiceImpl implements EmpService
@Autowired
private EmpMapper empMapper;
@Override
public List<Map<String, Object>> showPage(Emp emp)
return empMapper.selectEmp(emp);
结果集
以上是关于分页插件查询的主要内容,如果未能解决你的问题,请参考以下文章
手动分页解决一对多查询使用PAGEHELPER分页插件查询总条数不对