CompanyWhere 源码:
package com.easy.xdo; import lombok.Data; @Data public class CompanyWhere extends CompanyDO{ /** * page -1 默认不分页 */ private int page; private int rows; private int startrow; private String suffix; }
CompanyParam 源码:
package com.easy.xdo; import lombok.Data; @Data public class CompanyParam { private CompanyDO xdo; private CompanyWhere where; }
EmployeDO源码:
package com.easy.xdo; import lombok.Data; @Data public class EmployeDO { //演示员工类 private String id; private String name; private String age; private String companyid; }
CompanyController源码:
package com.easy.controller; import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.easy.service.CompanyService; import com.easy.util.PageUtil; import com.easy.xbo.PageBO; import com.easy.xdo.CompanyParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import io.swagger.annotations.ApiOperation; @Api("API-公司接口") @RestController @RequestMapping("api/v1/company") public class CompanyController { @Autowired private CompanyService companyService; @ApiOperation(value = "分页查询") @PostMapping("/page") public PageBO page() throws Exception{ CompanyParam param=new CompanyParam(); return PageUtil.getPageBOData(companyService,param); } }
CompanyService 源码:
package com.easy.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.easy.dao.CompanyDAO; import com.easy.xdo.CompanyDO; import com.easy.xdo.CompanyParam; public class CompanyService { @Autowired private CompanyDAO companyDAO; public List<CompanyDO> selectPage(CompanyParam param){ return companyDAO.selectList(param); } public int selectCount(CompanyParam param){ return companyDAO.selectCount(param); } }
xml源码
<?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.easy.dao.CompanyDAO"> <resultMap id="BaseResultMap" type="com.easy.xdo.CompanyDO"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="address" property="address" /> <collection property="employes" ofType="com.easy.xdo.EmployeDO"> <id column="eid" property="id" /> <result column="ename" property="name"/> <result column="age" property="age"/> </collection> </resultMap> <sql id="sqlBase"> c.id,c.name,c.address,e.id as eid,e.name as ename,e.age </sql> <sql id="WhereModelSql"> <if test="where != null"> <where> <if test="where.id != null and where.id != ''"> AND c.id=#{where.id} </if> <if test="where.name != null and where.name != ''"> AND c.name=#{where.name} </if> <if test="where.address != null and where.address != ''"> AND c.address=#{where.address} </if> </where> </if> </sql> <select id="selectList" resultMap="BaseResultMap" parameterType="com.easy.xdo.CompanyParam"> select <include refid="sqlBase" /> from company c left join employe e on c.id=e.companyid where c.id in (select page.id from (select id from company <include refid="WhereModelSql" /> limit #{where.startrow},#{where.rows}) page) </select> <select id="selectCount" resultType="java.lang.Integer" parameterType="com.easy.xdo.CompanyParam"> select count(c.id) from company <include refid="WhereModelSql" /> </select> </mapper>
这里特殊说明一下in语句子语句中不能使用limit,但是孙语句中可以使用,很蛋疼所以多包了一层,sql语句这里大家可以根据自己的情况随意写,只要最后传参进去出来的数据准确就ok了