分页查询
Controller层
Controller层:MVC架构中的接口层,用户访问请求时对接;是对项目里的功能做统一的调度。
@Controller
@RequestMapping("/api/v1/TStorageRecord")
public class TStorageRecordController {
@Autowired
private TStorageRecordService tStorageRecordService;
@RequestMapping("/listpage")
public String listpage(TStorageRecord tStorageRecord, Model model){
List<TStorageRecord> list=tStorageRecordService.getlist(tStorageRecord);
Page01 page01=new Page01();
// model.addAttribute("page",page01);
model.addAttribute("tslist",list);
return "list.jsp";
}
@RequestMapping("/goadd")
public String goadd(){
return "add.jsp";
}
@RequestMapping("/add")
public String add(TStorageRecord tStorageRecord){
tStorageRecordService.add(tStorageRecord);
return "redirect:/api/v1/TStorageRecord/listlimit";
}
@RequestMapping("/del")
public String del(int id){
tStorageRecordService.del(id);
return "redirect:/api/v1/TStorageRecord/listlimit";
}
@RequestMapping("/update")
public String update(TStorageRecord tStorageRecord){
tStorageRecordService.update(tStorageRecord);
return "redirect:/api/v1/TStorageRecord/listlimit";
}
@RequestMapping("/gotupdate")
public String gotupdate(Integer id,Model model){
TStorageRecord tStorageRecord=tStorageRecordService.gettStorage(id);
model.addAttribute("TSto",tStorageRecord);
return "update.jsp";
}
@RequestMapping("getRoleLimit")
public String getRolelimit(HttpServletRequest request){
Page01 page01=new Page01();
String currPageNostr=request.getParameter("currPageNo");
if(currPageNostr==null||"".equals(currPageNostr)){
page01.setCurrPageNo(1);
}else{
page01.setCurrPageNo(Integer.parseInt(currPageNostr));
}
String pageSizeStr=request.getParameter("pageSize");
if(pageSizeStr==null||"".equals(pageSizeStr)){
page01.setPageSize(2);
}else{
page01.setPageSize(Integer.parseInt(pageSizeStr));
}
page01.setTotalCount(tStorageRecordService.getRoleCount());
page01.settStorageRecords(tStorageRecordService.getSysRoleList(page01.getCurrPageNo(),page01.getPageSize()));
request.setAttribute("page",page01);
request.setAttribute("Records",page01.gettStorageRecords());
return "list.jsp";
}
}
mapper层
mapper层的作用是对数据库进行数据持久化操作,他的方法语句是直接针对数据库操作的现在用mybatis逆向工程生成的
public interface TStorageRecordMapper {
List<TStorageRecord> getlist(TStorageRecord tStorageRecord);
int add(TStorageRecord tStorageRecord);
TStorageRecord gettStorage(Integer id);
int update (TStorageRecord tStorageRecord);
int del(Integer id);
int getRoleCount();
List<TStorageRecord> getSysRoleList(@Param("num")int num,@Param("pageSize")int pageSize);
}
service层
Service层主要负责业务模块的逻辑应用设计。同样是首先设计接口,再设计其实现的类,接着再Spring的配置文件中配置其实现的关联
public interface TStorageRecordService {
List<TStorageRecord> getlist(TStorageRecord tStorageRecord);
int add(TStorageRecord tStorageRecord);
TStorageRecord gettStorage(Integer id);
int update (TStorageRecord tStorageRecord);
int del(Integer id);
int getRoleCount();
List<TStorageRecord> getSysRoleList(@Param("pageSize")int pageSize, @Param("currPageNo")int currPageNo);
}
@Service
public class TStorageRecordServiceimpl implements TStorageRecordService {
@Autowired
private TStorageRecordMapper tStorageRecordMapper;
@Override
public List<TStorageRecord> getlist(TStorageRecord tStorageRecord) {
return tStorageRecordMapper.getlist(tStorageRecord);
}
@Override
public int add(TStorageRecord tStorageRecord) {
return tStorageRecordMapper.add(tStorageRecord);
}
@Override
public TStorageRecord gettStorage(Integer id) {
return tStorageRecordMapper.gettStorage(id);
}
@Override
public int update(TStorageRecord tStorageRecord) {
return tStorageRecordMapper.update(tStorageRecord);
}
@Override
public int del(Integer id) {
return tStorageRecordMapper.del(id);
}
@Override
public int getRoleCount() {
return tStorageRecordMapper.getRoleCount();
}
@Override
public List<TStorageRecord> getSysRoleList(int currPageNo , int pageSize) {
int num=(currPageNo-1)*pageSize;
return tStorageRecordMapper.getSysRoleList(num,pageSize);
}
}
until层
存放工具类信息
public class Page01 {
private int totalPageCount=0;//总页数 计算 根据每页展示记录数和记录总数计算出来的
private int pageSize=3;//每页展示记录数,用户指定,通常有默认值
private int totalCount;//记录总数,数据库查询
private int currPageNo=1;//当前页码 用户指定 ,默认显示第一页
private List<TStorageRecord> tStorageRecords;//每页数据集合 数据库查询
public int getTotalPageCount() {
if (totalCount%pageSize==0){
return totalCount/pageSize;
}else{
return totalCount/pageSize+1;
}
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrPageNo() {
return currPageNo;
}
public void setCurrPageNo(int currPageNo) {
this.currPageNo = currPageNo;
}
public List<TStorageRecord> gettStorageRecords() {
return tStorageRecords;
}
public void settStorageRecords(List<TStorageRecord> tStorageRecords) {
this.tStorageRecords = tStorageRecords;
}
}
mappers层
<?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.xinxi2.mapper.TStorageRecordMapper">
<insert id="add" keyProperty="id" keyColumn="id" useGeneratedKeys="true" parameterType="com.xinxi2.bean.TStorageRecord">
INSERT INTO `t_storage_record`(goodsname)
VALUES (#{goodsname})
</insert>
<update id="update" parameterType="com.xinxi2.bean.TStorageRecord">
update t_storage_record
<set>
<if test="goodsname!=null">
goodsname=#{goodsname},
</if>
</set>
where id=#{id}
</update>
<delete id="del" parameterType="com.xinxi2.bean.TStorageRecord">
delete from `t_storage_record` WHERE id=#{id}
</delete>
<select id="getlist" resultType="com.xinxi2.bean.TStorageRecord">
SELECT * FROM `t_storage_record`
<where>
<if test="goodsname!=null">
and goodsname=#{goodsname}
</if>
<if test="id!=null">
and id=#{id}
</if>
</where>
</select>
<select id="gettStorage" resultType="com.xinxi2.bean.TStorageRecord">
SELECT * FROM `t_storage_record` WHERE id=#{id}
</select>
<select id="getRoleCount" resultType="java.lang.Integer">
SELECT count(1) FROM `t_storage_record`
</select>
<select id="getSysRoleList" resultType="com.xinxi2.bean.TStorageRecord">
SELECT * FROM `t_storage_record` LIMIT #{num},#{pageSize}
</select>
</mapper>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/161199.html