zoukankan      html  css  js  c++  java
  • 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>
     
  • 相关阅读:
    踩坑:windows系统下,nodejs版本管理器无法使用n来管理
    华为,小米部分机型微信浏览器rem不适配的解决方案
    百度地图滚轮缩放时产生位置偏移 问题
    baidu-aip-SDK node.js 身份证识别
    iconfont 怎么在项目中使用图标库
    前端实用功能:选项卡切换
    input复选框操作的部分高频率使用代码
    HTML标签的命名/CSS标准化命名大全
    如何在网页中设置一个定时器计算时间?
    H5JS二维动画制作!two.js的基本操作class2
  • 原文地址:https://www.cnblogs.com/wgyi140724-/p/10634145.html
Copyright © 2011-2022 走看看