导入依赖包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
在yam或者是properties文件加上这个
我用的是properties文件 yam 自己上网找
我的接口
package com.cskt.mapper;
import com.cskt.pojo.Goods;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface GoodsMapper {
/**
* id删除
* */
int deleteByPrimaryKey(Integer goodSid);
/**
* 新增
* */
int insert(Goods record);
/**
* 新增
* */
int insertSelective(Goods record);
/**
* id查询
* */
Goods selectByPrimaryKey(Integer goodSid);
/**
* 修改
* */
int updateByPrimaryKeySelective(Goods record);
/**
* 修改
* */
int updateByPrimaryKey(Goods record);
/**
* 查询
* */
List<Goods> selectfn(Goods goods);
}
我的实现接口
<?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="com.cskt.mapper.GoodsMapper">
<resultMap id="BaseResultMap" type="com.cskt.pojo.Goods">
<!--@mbg.generated-->
<!--@Table goods-->
<id column="good_sid" jdbcType="INTEGER" property="goodSid" />
<result column="goods_name" jdbcType="VARCHAR" property="goodsName" />
<result column="goodsPrice" jdbcType="INTEGER" property="goodsprice" />
<result column="good_type" jdbcType="INTEGER" property="goodType" />
<result column="goods_spname" jdbcType="VARCHAR" property="goodsSpname" />
<association property="goodstype" javaType="com.cskt.pojo.Goodstype">
<id property="id" column="id"></id>
<result property="typeName" column="type_name"></result>
</association>
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
good_sid, goods_name, goodsPrice, good_type, goods_spname
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from goods
where good_sid = #{goodSid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--@mbg.generated-->
delete from goods
where good_sid = #{goodSid,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="good_sid" keyProperty="goodSid" parameterType="com.cskt.pojo.Goods" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into goods (goods_name, goodsPrice, good_type,
goods_spname)
values (#{goodsName,jdbcType=VARCHAR}, #{goodsprice,jdbcType=INTEGER}, #{goodType,jdbcType=INTEGER},
#{goodsSpname,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="good_sid" keyProperty="goodSid" parameterType="com.cskt.pojo.Goods" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into goods
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="goodsName != null">
goods_name,
</if>
<if test="goodsprice != null">
goodsPrice,
</if>
<if test="goodType != null">
good_type,
</if>
<if test="goodsSpname != null">
goods_spname,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="goodsName != null">
#{goodsName,jdbcType=VARCHAR},
</if>
<if test="goodsprice != null">
#{goodsprice,jdbcType=INTEGER},
</if>
<if test="goodType != null">
#{goodType,jdbcType=INTEGER},
</if>
<if test="goodsSpname != null">
#{goodsSpname,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.cskt.pojo.Goods">
<!--@mbg.generated-->
update goods
<set>
<if test="goodsName != null">
goods_name = #{goodsName,jdbcType=VARCHAR},
</if>
<if test="goodsprice != null">
goodsPrice = #{goodsprice,jdbcType=INTEGER},
</if>
<if test="goodType != null">
good_type = #{goodType,jdbcType=INTEGER},
</if>
<if test="goodsSpname != null">
goods_spname = #{goodsSpname,jdbcType=VARCHAR},
</if>
</set>
where good_sid = #{goodSid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.cskt.pojo.Goods">
<!--@mbg.generated-->
update goods
set goods_name = #{goodsName,jdbcType=VARCHAR},
goodsPrice = #{goodsprice,jdbcType=INTEGER},
good_type = #{goodType,jdbcType=INTEGER},
goods_spname = #{goodsSpname,jdbcType=VARCHAR}
where good_sid = #{goodSid,jdbcType=INTEGER}
</update>
<select id="selectfn" resultMap="BaseResultMap" resultType="com.cskt.pojo.Goods">
select good_sid,goods_name,goodsPrice,good_type,goods_spname, b.id, b.type_name from goods as a ,goodstype as b
<where>
<if test="goodsName == null and goodsName != ''">
and goodsName like '%${goodsName}%'
</if>
and a.good_type=b.id
</where>
</select>
</mapper>
然后在@Controller层怎么写
package com.cskt.wed;
import com.cskt.pojo.ActionId;
import com.cskt.service.ActionIdService;
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;
@Controller
public class WedActionId {
@Autowired
private ActionIdService actionIdService;
@RequestMapping("/list")
public String list(Model model,
@RequestParam(value = "orderStatus",required = false,defaultValue = "0") int orderStatus,
@RequestParam(value = "payStatus",required = false,defaultValue = "0") int payStatus,
@RequestParam(value = "actionTime",required = false) String actionTime,
@RequestParam(value = "num",required = false,defaultValue = "1") int num
){
final PageInfo pageInfo = actionIdService.select(num, orderStatus, payStatus, actionTime);
model.addAttribute("page",pageInfo);
model.addAttribute("orderStatus",orderStatus);
model.addAttribute("payStatus",payStatus);
model.addAttribute("actionTime",actionTime);
model.addAttribute("path","/list?num=");
return "list";
}
@RequestMapping("/add_1")
public String add(){
return "add";
}
@RequestMapping("/add")
public String add(ActionId actionId){
final int i = actionIdService.insert(actionId);
if(i>0){
return "redirect:/list";
}
return "404";
}
@RequestMapping("/up")
public String up(ActionId actionId){
final int i = actionIdService.updateByPrimaryKeySelective(actionId);
if(i>0){
return "redirect:/list";
}
return "404";
}
@RequestMapping("/del")
public String del(int id){
final int i = actionIdService.deleteByPrimaryKey(id);
if(i>0){
return "redirect:/list";
}
return "404";
}
@RequestMapping("/up_1")
public String up_1(Model model,int id){
final ActionId select = actionIdService.selectByPrimaryKey(id);
model.addAttribute("select",select);
return "up";
}
}
我有一个page.html分页文件
<!DOCTYPE html>
<!--suppress ALL--> <!-- 去掉红色下划线-->
<div th:fragment="page">
<style>
.page{
border: 1px solid blue;
margin-right: 5px;
padding: 3px 5px;
}
.page.active{
color: red;
border: 1px solid red;
}
</style>
<div style="float: left">
当前第<span th:text="${page.PageNum}"></span>页
总记录数<span th:text="${page.total}"></span>
</div>
<div style="float: right">
<a th:text="首页" th:if="${page.PageNum>1}" th:href="@{${path}+'&goodsName='+${goods.goodsName}}"></a>
<a th:text="上一页" th:if="${page.PageNum>1}" th:href="@{${path}+${page.PageNum-1}+'&goodsName='+${goods.goodsName}}"></a>
<a th:href="@{${path}+${i}+'&goodsName='+${goods.goodsName}}" th:each="i:${#numbers.sequence(1,page.Pages)}" th:text="${i}"
th:class="${page.PageNum==i}? 'page active':'page'"></a>
<a th:text="下一页" th:if="${page.PageNum<page.Pages}" th:href="@{${path}+${page.PageNum+1}+'&goodsName='+${goods.goodsName}}"></a>
<a th:text="尾页" th:if="${page.PageNum<page.Pages}" th:href="@{${path}+${page.Pages}+'&goodsName='+${goods.goodsName}}"></a>
</div>
</div>
</html>
用这个分页比较好,复用性高
在实现的页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/list.html}">
<input type="text" name="goodsName" th:value="${goods.goodsName}">
<input type="submit" value="查询">
</form>
<form th:action="@{/add2}">
<input type="submit" value="新增">
</form>
<table border="1">
<tr th:each="next:${page.list}">
<td>[[${next.goodsName}]]</td>
<td>[[${next.goodsprice}]]</td>
<td>[[${next.goodsSpname}]]</td>
<td>[[${next.goodstype.typeName}]]</td>
<td>
<a th:href="@{/del1(goodSid=${next.goodSid})}">删除</a>
<a th:href="@{/up2(goodSid=${next.goodSid})}">编辑</a>
</td>
</tr>
</table>
<div th:insert="page :: page"></div>
</body>
</html>
<div th:insert="page :: page"></div>
这个是分页添加的页面,第一个page页面名称 第二个page数分页的集合这个名称不能该
如果你想改的话这里也要改
改成你的分页集合名称