1. 程式人生 > 其它 >利用Myabtisplus的IPage外掛進行分頁

利用Myabtisplus的IPage外掛進行分頁

1、建立實體類   Entity.java

@Data
@Accessors(chain = true)
@ApiModel
@TableName("pg_table")
@ToString(callSuper = true)
public class Entity{
    private Integer id;

    private String rjsyZl;

    private String rjsy;
    private String num;

 @TableField(exist = false) 
  private Long dialogThreeCurrent;
@TableField(exist
= false)
  private Integer dialogThreeSize; }

2、建立PageMapper.xml

<select id="query" resultType="com.ustcinfo.starter.crj.entity.Entity">
        SELECT
        '合計' AS rjsy,
        COUNT(1) AS num
        FROM pg_table
        WHERE DATE_FORMAT(DATE_ADD(create_date,INTERVAL -1 DAY),'%Y-%m-%d')  = DATE_FORMAT( #{createDate},'%Y-%m-%d')
        
<if test="cz!='全部'" > AND cz=#{cz} </if> <if test="gj!=null and gj!=''" > AND gj=#{gj} </if> UNION (SELECT IFNULL(rjsy_zl,'其他')AS rjsy ,COUNT(1) AS num FROM pg_table     WHERE DATE_FORMAT(DATE_ADD(create_date,INTERVAL -1 DAY),'%Y-%m-%d') = DATE_FORMAT( #{createDate},'%Y-%m-%d')     
<if test="cz!='全部'" >       AND cz=#{cz}     </if>     <if test="gj!=null and gj!=''" >       AND gj=#{gj}     </if>
    GROUP BY rjsy_zl) ORDER BY num DESC </select>

3、建立PageMapper.java

@Repository
public interface PageMapper extends BaseDao<Entity> {
    IPage<Entity> query(@Param("page") Page page,@Param("cz") String cz, @Param("gj")String gj,@Param("createDate")String createDate); }

4、建立PageService.java

public interface PageService extends ISuperService<Entity> {
    IPage<Entity> getTlsy(JSONObject json);
}

5、建立PageServiceImpl.java

@Service
public class PageServiceImpl extends ServiceImpl<PageMapper, Entity> implements PageService{
    protected static Logger logger = LoggerFactory.getLogger(PageServiceImpl.class);

    @Autowired
    private PageMapper pageMapper;
    @Override
    public IPage<Entity> getTlsy(JSONObject json) {
    //接受前端傳過來的引數 String s1
= json.getString("personType"); String s2 = json.getString("gjdq"); String s3 = json.getString("endTime"); String s4 = json.getString("dialogThreeCurrent"); String s5 = json.getString("dialogThreeSize"); Page<Entity> page = new Page<>(Long.parseLong(s4), Integer.parseInt(s5)); return PageMapper.query(page,s1,s2,s3);
  }
}

6、建立PageController.java

@RestController
@RequestMapping("/pgfx")
public class PageController{
    @Autowired
     PageService pageService;
@PostMapping(value="/getpage",produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Ipage分頁", notes = "Ipage分頁", produces = MediaType.APPLICATION_JSON_VALUE)
public R<IPage<Entity>> getTlsy(@RequestBody JSONObject json){
    IPage<Entity> tlsy = pageService.getTlsy(json);
        return success(tlsy);
  }
}