码迷,mamicode.com
首页 > 其他好文 > 详细

分页插件查询

时间:2019-06-29 13:04:41      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:let   映射   star   resource   util   show   wired   mes   技术   

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

结果集技术图片

 

分页插件查询

标签:let   映射   star   resource   util   show   wired   mes   技术   

原文地址:https://www.cnblogs.com/hong999/p/11106043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!