demo:https://download.csdn.net/download/caidingnu/12064636
1、目录结构
2、Controller
package com.example.mybatisplustest.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplustest.entity.UniData;
import com.example.mybatisplustest.entity.User;
import com.example.mybatisplustest.mapper.PageDataMapper;
import com.example.mybatisplustest.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* desc: plus 分页方式
* author CDN
* create 2019-12-27 21:37
* version 1.0.0
*/
@RestController
public class PageDataController {
@Autowired
PageDataMapper pageDataMapper;
@Autowired
UserMapper userMapper;
/**
* desc: 注解方式(关联分页查询)
* param:
* return:
* author: CDN
* date: 2019/12/27
*/
@RequestMapping("/annotationsPage")
public Object annotationsPage() {
IPage page=new Page<>(1, 2);
Map map=new HashMap();
map.put("userId", 1);
IPage all = pageDataMapper.annotationsPage(page,map);
return all;
}
/**
* desc: provider (关联分页查询)
* param:
* return:
* author: CDN
* date: 2019/12/27
*/
@RequestMapping("/providerPage")
public Object providerPage() {
IPage page=new Page<>(1, 2);
Map map=new HashMap();
map.put("userId", 1);
IPage all = pageDataMapper.providerPage(page,map);
return all;
}
/**
* desc: xml方式(关联分页查询)
* param:
* return:
* author: CDN
* date: 2019/12/27
*/
@RequestMapping("/xmlPage")
public Object xmlPage() {
IPage page=new Page<>(1, 2);
Map map=new HashMap();
map.put("userId", 1);
IPage all = pageDataMapper.xmlPage(page,map);
return all;
}
/**
* desc: 常规方式(单表 user表)
* param:
* return:
* author: CDN
* date: 2019/12/27
*/
@RequestMapping("/commonPage")
public Object commonPage() {
IPage page=new Page<>(1, 2);
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.eq("user_id", 1);
IPage all = userMapper.selectPage(page,queryWrapper);
return all;
}
}
3、UniData 实体
package com.example.mybatisplustest.entity;
/**
* desc:
* author CDN
* create 2019-12-27 21:10
* version 1.0.0
*/
public class UniData {
private String name;
private String password;
private String nickName;
private Integer age;
private Integer roleId;
private String role;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
4、Mapper
package com.example.mybatisplustest.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplustest.entity.UniData;
import com.example.mybatisplustest.mapper.provider.UniDataProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.Map;
/**
* desc:
* author caidingnu
* create 2019/12/27
* version 1.0.0
*/
public interface PageDataMapper {
/**
* 注解方式分页
*
* @param page
* @param map
* @return
*/
@Select("SELECT user.name,user.password,USER.nick_name as nickName,USER.age,role.role_id as roleId,role.role " +
"FROM user INNER JOIN role ON `user`.user_id = role.role_id where user.user_id = #{map.userId}")
IPage annotationsPage(IPage page, @Param("map") Map map);
/**
* provider 方式分页
* ☆☆☆☆☆☆☆ 重点: page必须放在第一个参数,否则无法分页或者报错 ☆☆☆☆☆☆☆☆
* @param page
* @param map
* @return
*/
@SelectProvider(type = UniDataProvider.class, method = "providerPage")
IPage providerPage(IPage page, @Param("map") Map map);
/**
* xml 方式分页
* @param page
* @param map
* @return
*/
IPage xmlPage(IPage page, @Param("map") Map map);
}
5、UniDataProvider
package com.example.mybatisplustest.mapper.provider;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.mybatisplustest.entity.UniData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import java.util.Map;
/**
* desc:
* author CDN
* create 2019-12-27 22:20
* version 1.0.0
*/
public class UniDataProvider {
/**
* desc:
* param:
* return:
* author: CDN
* date: 2019/12/27
*/
public String providerPage(Map map) {
SQL sql = new SQL();
sql.SELECT("user.name,user.password,USER.nick_name as nickName,USER.age,role.role_id as roleId,role.role " +
"FROM user INNER JOIN role ON `user`.user_id = role.role_id where user.user_id = #{map.userId}");
return sql.toString();
}
}
6、PageDataMapper