springboot 2.3 整合mybatis分页插件,以及注意事项案例

1.添加依赖


			com.github.pagehelper
			pagehelper-spring-boot-starter
			1.2.5
		

2.控制层代码的方法

@RestController
@Validated
@Api(tags = "我的报名相关接口")
@RequestMapping("/entroll")
public class EntrollController {
	@RequestMapping("/getMyEntrollZsbmStudentsList")
    public ResultVO getMyEntrollZsbmStudentsList(HttpServletRequest request,@NotNull(message ="currentPage不能为空!") @RequestParam(value="currentPage") Integer currentPage,
                                                              @NotNull(message ="pageSize不能为空!")    @RequestParam(value="pageSize") Integer pageSize,@RequestParam(value="name",required = false) String name){
        Object object = request.getSession().getAttribute(Constants.SESSION_USER_Attribute);
        if(object!=null){
            ZsbmUser user=(ZsbmUser)object;
            return enterService.getMyEntrollZsbmStudentsList(currentPage,pageSize,name,user);
        }
       return new ResponseResultUtil().success(ResponseCodeEnum.UNLOGIN_ERROR.getCode(),
               ResponseCodeEnum.UNLOGIN_ERROR.getMessage(),null,true);
   		 }
  }

3.业务层方法代码

@Service
public class EnterService {

    /** 我的报名模块接口注入 **/
    @Autowired
    private ZsbmStudentDao zsbmStudentDao;

    /**
     * @description 我的报名列表查询
     * @param currentPage 当前页
     * @param pageSize 当前页的大小
     * @param name 姓名
     * @return ResultVO
     */
    public ResultVO getMyEntrollZsbmStudentsList(Integer currentPage, Integer pageSize,String name,ZsbmUser user) {
    	//给mybatis插件设置起始页 要注意此处的位置【放在查询数据代码的前后都是不一样的】
        PageHelper.startPage(currentPage, pageSize);
        String uid = user.getUid();
        String roleEname =user.getRoleEname();
        Boolean isAdmin=false;
        //如果是管理员用户
        if(Constants.ROLE_ADMIN.equals(roleEname)){
            isAdmin=true;
        }
        List zsbmStudents = zsbmStudentDao.getZsbmStudentList(uid,name,isAdmin);
        PageInfo pageInfo = new PageInfo(zsbmStudents);
        Page page=new Page(currentPage,true,pageSize,pageInfo.getSize());
        if(pageInfo!=null&&pageInfo.getList().size()>0){
            for(int  i=0;i<=pageInfo.getList().size()-1;i++){
                String sid=pageInfo.getList().get(i).getSid();
                List zsbmStudentParentList = zsbmStudentDao.getZsbmStudentParentsBySid(sid);
                if(zsbmStudentParentList!=null&&zsbmStudentParentList.size()>0){
                    pageInfo.getList().get(i).setZsbmStudentParents(zsbmStudentParentList);
                }
                List zsbmAttachList = zsbmStudentDao.getBestSoreBySid(sid);
                if(zsbmAttachList!=null&&zsbmAttachList.size()>0){
                    pageInfo.getList().get(i).setBestScores(zsbmAttachList);
                }
            }
            return new ResponseResultUtil().success(new PageVO(pageInfo.getList(), page), true);
        }
        return new ResponseResultUtil().success(true);
    }
}

4.接口代码

package com.iflytek.edu.hnezzhxy.dao;

import com.iflytek.edu.hnezzhxy.model.ZsbmAttach;
import com.iflytek.edu.hnezzhxy.model.ZsbmStudent;
import com.iflytek.edu.hnezzhxy.model.ZsbmStudentParent;
import com.iflytek.edu.hnezzhxy.vo.MyEntrollVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ZsbmStudentDao {
    /** 查询学生信息列表 **/
    List getZsbmStudentList(@Param("uid") String uid,@Param("name") String name,@Param("isAdmin")Boolean isAdmin);zsbmStudentParent);
    /** 通过sid查询自己亲人信息 **/
    List getZsbmStudentParentsBySid(@Param("sid") String sid);
    /** 获取成绩照片信息 **/
    List getBestSoreBySid(@Param("sid") String sid);
}

5.xml文件





    
         a.id,a.sid,a.name,a.sex,a.birth,a.nation_code,a.graduate_school,a.stu_phone_num,a.ID_card_num
        ,a.admission_no,a.create_time,a.modify_time,a.del_flag,a.status,a.examine_time,examine_uid,a.examine_reason
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
            
            
            
            
        
    


    

    

    

    

    

    
    insert into zsbm_student (sid,name,sex,birth,nation_code,
        graduate_school,stu_phone_num,ID_card_num,
        admission_no,creator_id,create_time,modify_time,del_flag
      )
    values (#{sid,jdbcType=VARCHAR},  #{name,jdbcType=VARCHAR},#{sex,jdbcType=TINYINT},
      #{birth,jdbcType=DATE},#{nationCode,jdbcType=VARCHAR},#{graduateSchool,jdbcType=VARCHAR},
      #{stuPhoneNum,jdbcType=VARCHAR},#{idcardNum,jdbcType=VARCHAR},#{admissionNo,jdbcType=VARCHAR},#{creatorId,jdbcType=VARCHAR},
      #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP},#{delFlag,jdbcType=TINYINT}
      )
  

    
    insert into zsbm_student_parent (sid,type,name,dw,phone,modify_time) values
        
            (#{item.sid,jdbcType=VARCHAR},#{item.type,jdbcType=TINYINT},#{item.name,jdbcType=VARCHAR},
            #{item.dw,jdbcType=VARCHAR},#{item.phone,jdbcType=VARCHAR},#{item.modifyTime,jdbcType=TIMESTAMP})
        
  

    
    insert into zsbm_attach (sid,file_name,suffix,file_size,context_id,file_url,type) values
     
         (#{item.sid,jdbcType=VARCHAR},#{item.fileName,jdbcType=VARCHAR},#{item.suffix,jdbcType=VARCHAR},
         #{item.fileSize,jdbcType=VARCHAR},#{item.contextId,jdbcType=VARCHAR},#{item.fileUrl,jdbcType=VARCHAR}
         ,#{item.type,jdbcType=TINYINT}
      )
     
  

    
    insert into zsbm_attach(sid,file_name,suffix,file_size,context_id,
            file_url,type
      )
    values (#{sid,jdbcType=VARCHAR},#{fileName,jdbcType=VARCHAR},#{suffix,jdbcType=VARCHAR},
            #{fileSize,jdbcType=VARCHAR},#{contextId,jdbcType=VARCHAR},#{fileUrl,jdbcType=VARCHAR},#{type,jdbcType=TINYINT}
      )
  

    
        update zsbm_student
        
            
                name = #{name,jdbcType=VARCHAR},
            
            
                sex = #{sex,jdbcType=TINYINT},
            
            
                birth = #{birth,jdbcType=DATE},
            
            
                nation_code = #{nationCode,jdbcType=VARCHAR},
            
            
                graduate_school = #{graduateSchool,jdbcType=VARCHAR},
            
            
                stu_phone_num = #{stuPhoneNum,jdbcType=VARCHAR},
            
            
                ID_card_num = #{idcardNum,jdbcType=VARCHAR},
            
            
                admission_no = #{admissionNo,jdbcType=VARCHAR},
            
            
                modify_time = #{modifyTime,jdbcType=TIMESTAMP}
            
        
        where sid = #{sid,jdbcType=VARCHAR}
    


    
        
            update zsbm_student_parent
            
                
                    type = #{item.type,jdbcType=TINYINT},
                
                
                    name = #{item.name,jdbcType=VARCHAR},
                
                
                    dw = #{item.dw,jdbcType=VARCHAR},
                
                
                    phone = #{item.phone,jdbcType=VARCHAR},
                
                
                    modify_time = #{item.modifyTime,jdbcType=TIMESTAMP}
                
            
            where id = #{item.id,jdbcType=INTEGER}
        
    

    
        
        update zsbm_attach
            
                
                    file_name = #{item.fileName,jdbcType=VARCHAR},
                
                
                    suffix = #{item.suffix,jdbcType=VARCHAR},
                
                
                    file_size = #{item.fileSize,jdbcType=VARCHAR},
                
                
                    context_id = #{item.contextId,jdbcType=VARCHAR},
                
                
                    file_url = #{item.fileUrl,jdbcType=VARCHAR},
                
                
                    del_flag = #{item.delFlag,jdbcType=TINYINT},
                
                
                    type = #{item.type,jdbcType=TINYINT},
                
            
            where id = #{item.id,jdbcType=INTEGER}
        
    

    
        update zsbm_attach
        
            
                file_name = #{fileName,jdbcType=VARCHAR},
            
            
                suffix = #{suffix,jdbcType=VARCHAR},
            
            
                file_size = #{fileSize,jdbcType=VARCHAR},
            
            
                context_id = #{contextId,jdbcType=VARCHAR},
            
            
                file_url = #{fileUrl,jdbcType=VARCHAR},
            
            
                del_flag = #{delFlag,jdbcType=TINYINT},
            
        
        where id = #{id,jdbcType=INTEGER}
    

    
        update zsbm_attach set del_flag=1 where sid=#{sid} and type=0 and del_flag=0
    



5.使用此mybatis分页插件需要注意的地方
不要在xml中使用关联查询1对多的collection标签,这样会导致列表查询出来的数据真实性有问题,因为主表数据重复了,导致分页有问题,比如:


        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
            
            
            
            
        
        
            
            
            
            
            
            
            
        
        
            
            
            
            
            
            
            
            
        
    

你可能感兴趣的:(mybatis)