mybatisplus的分页、关键字、连表查询
直接看代码
1.controller
/**
* 分页查询数据
*
* @param query 查询对象
* @return PageList 分页对象
*/
@RequestMapping(value = "/json",method = RequestMethod.POST)
public PageList<Brand> json(@RequestBody BrandQuery query)
{
   return brandService.queryPage(query);
}
2.service层
@Autowired
private BrandMapper brandMapper;
@Override
public PageList<Brand> queryPage(BrandQuery query) {
long total = 0L;
total = brandMapper.queryPageCount(query);//获取总数据
if (total == 0) {
return new PageList<>();
} else {
List<Brand> list = brandMapper.queryPageList(query);
return new PageList<>(total, list);
}
}
3.mapper
/**
 * 分页数据总的条数
 * @param query
 * @return
 */
long queryPageCount(BrandQuery query);
/**
 * 当前页的分页数据
 * @param query
 * @return
 */
List<Brand> queryPageList(BrandQuery query);
4.映射到BrandMapper.xml中
   <resultMap id="BrandMap" type="Brand">
        <!--封装基本属性-->
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="englishName" property="englishName" />
        
        <!--封装关联属性-->
        <association property="productType" javaType="ProductType">
            <id column="pid" property="id" />
            <result column="pname" property="name" />
        </association>
    </resultMap>
    <!--分页数据总的条数-->
    <select id="queryPageCount" resultType="long" parameterType="BrandQuery">
        SELECT
        count(b.id)
        FROM
        t_brand b
        LEFT JOIN t_product_type p ON b.productTypeId = p.id
        <include refid="whereSql"/>
    </select>
    <!--当前页的分页数据-->
    <select id="queryPageList" parameterType="BrandQuery" resultMap="BrandMap">
        SELECT
        b.id,
        b.name,
        b.englishName
        FROM
        t_brand b
        LEFT JOIN t_product_type p ON b.productTypeId = p.id
        <include refid="whereSql"/>
        limit #{start},#{rows}
    </select>
    <!--带条件查询-->
    <sql id="whereSql">
        <where>
            <if test="keyword!=null and keyword!=''">
                b.name like concat('%',#{keyword},'%') or b.description like concat('%',#{keyword},'%')
            </if>
        </where>
    </sql>
 
                    
                
                
            
        
浙公网安备 33010602011771号