spring boot 中mybatis plus的分页方式

spring boot 中mybatis plus的分页方式 ,总结了四种,三种是MP基于xml形式分页,第四种是基于MP的AR模式。

service如下:

/**
     *
     * 功能描述: MP基于xml形式分页--方式1
     *
     * @param:
     * @return:
     * @auther: renchengpeng
     * @date: 2019/8/16 10:44
     */
    @Override
    public BcsPage queryLoginListMpOne(VipLogin record, BcsPage bcsPage) throws CoreException {

        VipLoginEntity vipLoginEntity = CommonUtils.objectToObj(record, VipLoginEntity.class);
        //MP基于xml形式分页--方式1
        BcsPage vipLoginEntityBcsPage = (BcsPage)vipLoginMapper.queryLoginListMpOne(bcsPage, vipLoginEntity);

        System.out.println(vipLoginEntityBcsPage.getRecords().size()+"===========MP基于xml形式分页--方式1===============");
        vipLoginEntityBcsPage.getRecords().stream().forEach(item -> log.info("==========item:" + JSON.toJSONString(item)));

        vipLoginEntityBcsPage.setRecords(CommonUtils.mapList(vipLoginEntityBcsPage.getRecords(), VipLogin.class));
        return vipLoginEntityBcsPage;
    }

    /**
     * 功能描述: MP基于xml形式分页--方式2
     *
     * @param record
     * @param bcsPage
     * @param:
     * @return:
     * @auther: renchengpeng
     * @date: 2019/8/16 10:44
     */
    @Override
    public BcsPage queryLoginListMpTwo(VipLogin record, BcsPage bcsPage) throws CoreException {
        VipLoginEntity vipLoginEntity = CommonUtils.objectToObj(record, VipLoginEntity.class);
        //MP基于xml形式分页--方式2
        BcsPage vipLoginEntityBcsPage = (BcsPage)vipLoginMapper.queryLoginListMpTwo(bcsPage, vipLoginEntity);

        System.out.println(vipLoginEntityBcsPage.getRecords().size()+"==========MP基于xml形式分页--方式2================");
        vipLoginEntityBcsPage.getRecords().stream().forEach(item -> log.info("==========item:" + JSON.toJSONString(item)));

        vipLoginEntityBcsPage.setRecords(CommonUtils.mapList(vipLoginEntityBcsPage.getRecords(), VipLogin.class));
        return vipLoginEntityBcsPage;
    }

    /**
     * 功能描述: MP基于xml形式分页--方式3
     *
     * @param record
     * @param bcsPage
     * @param:
     * @return:
     * @auther: renchengpeng
     * @date: 2019/8/16 10:44
     */
    @Override
    public BcsPage queryLoginListMpThree(VipLogin record) throws CoreException {
        //改种方式不能用CommonUtils.objectToObj
        VipLoginEntity vipLoginEntity = JSON.parseObject(JSON.toJSONString(record), VipLoginEntity.class);
        //MP基于xml形式分页--方式2
        BcsPage vipLoginEntityBcsPage = (BcsPage)vipLoginMapper.queryLoginListMpThree(vipLoginEntity);

        System.out.println(vipLoginEntityBcsPage.getRecords().size()+"==========MP基于xml形式分页--方式3================");
        vipLoginEntityBcsPage.getRecords().stream().forEach(item -> log.info("==========item:" + JSON.toJSONString(item)));

        vipLoginEntityBcsPage.setRecords(CommonUtils.mapList(vipLoginEntityBcsPage.getRecords(), VipLogin.class));
        return vipLoginEntityBcsPage;
    }

    /**
     * 功能描述: 基于AR模式分页
     *
     * @param record
     * @param:
     * @return:
     * @auther: renchengpeng
     * @date: 2019/8/16 11:23
     */
    @Override
    public BcsPage queryLoginListMpAr(VipLogin record, BcsPage bcsPage) throws CoreException {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("loginName", "telephone");
        wrapper.eq(true, "AGENT_MER_SEQ", "0");
        BcsPage vipLoginEntityBcsPage = (BcsPage)record.selectPage(bcsPage, wrapper);

        System.out.println(vipLoginEntityBcsPage.getRecords().size()+"==========MP基于AR模式分页================");
        vipLoginEntityBcsPage.getRecords().stream().forEach(item -> log.info("==========item:" + JSON.toJSONString(item)));

        vipLoginEntityBcsPage.setRecords(CommonUtils.mapList(vipLoginEntityBcsPage.getRecords(), VipLogin.class));
        return vipLoginEntityBcsPage;
    }

mapper接口:

/**
     *
     * 功能描述: MP基于xml形式分页--方式1
     *
     * @param: IPage<VipLoginEntity> iPage
     * @param: VipLoginEntity record ---实体类,建议加上@Param("vip"),重命名,xml里面获取实体类里面的字段用注解里面的值
     * @return: IPage
     * @auther: renchengpeng
     * @date: 2019/8/16 10:30
     */
    IPage<VipLoginEntity> queryLoginListMpOne(IPage<VipLoginEntity> iPage, @Param("vip") VipLoginEntity record);

    /**
     *
     * 功能描述: MP基于xml形式分页--方式2
     *
     * @param: IPage<VipLoginEntity> iPage
     * @param: VipLoginEntity record ---实体类,不加@Param("vip"),xml里面获取实体类里面的字段用param2
     * @return: IPage
     * @auther: renchengpeng
     * @date: 2019/8/16 10:30
     */
    IPage<VipLoginEntity> queryLoginListMpTwo(IPage<VipLoginEntity> iPage, VipLoginEntity record);

    /**
     *
     * 功能描述: MP基于xml形式分页--方式3
     *
     * @param:  VipLoginEntity record ---实体类,BcsPage通过实体类继承
     * @return:
     * @auther: renchengpeng
     * @date: 2019/8/16 11:12
     */
    IPage<VipLoginEntity> queryLoginListMpThree(VipLoginEntity record);

xml:

 <!--MP基于xml形式分页-方式1-->
    <select id="queryLoginListMpOne" resultMap="BaseResultMap">
        SELECT VL.*
        FROM
        VIP_LOGIN VL
        LEFT JOIN VIP_MANAGE VM ON VL.LOGINNAME = VM.LOGINNAME
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="vip.loginName != null and vip.loginName != ''">AND VL.LOGINNAME=#{vip.loginName}</if>
            <if test="vip.telephone != null and vip.telephone != ''">AND VL.TELEPHONE =#{vip.telephone}</if>
            <if test="vip.approvalPhone != null and vip.approvalPhone != ''">AND VL.APPROVAL_PHONE=#{vip.approvalPhone}</if>
            <if test="vip.agentMerSeq != null and vip.agentMerSeq != ''">AND VL.AGENT_MER_SEQ=#{vip.agentMerSeq}</if>
        </trim>
        GROUP BY VL.LOGINNAME
    </select>

    <!--MP基于xml形式分页-方式2-->
    <select id="queryLoginListMpTwo" resultMap="BaseResultMap">
        SELECT VL.*
        FROM
        VIP_LOGIN VL
        LEFT JOIN VIP_MANAGE VM ON VL.LOGINNAME = VM.LOGINNAME
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="param2.loginName != null and param2.loginName != ''">AND VL.LOGINNAME=#{param2.loginName}</if>
            <if test="param2.telephone != null and param2.telephone != ''">AND VL.TELEPHONE =#{param2.telephone}</if>
            <if test="param2.approvalPhone != null and param2.approvalPhone != ''">AND VL.APPROVAL_PHONE=#{param2.approvalPhone}</if>
            <if test="param2.agentMerSeq != null and param2.agentMerSeq != ''">AND VL.AGENT_MER_SEQ=#{param2.agentMerSeq}</if>
        </trim>
        GROUP BY VL.LOGINNAME
    </select>

    <!--MP基于xml形式分页-方式3-->
    <select id="queryLoginListMpThree" resultMap="BaseResultMap">
        SELECT VL.*
        FROM
        VIP_LOGIN VL
        LEFT JOIN VIP_MANAGE VM ON VL.LOGINNAME = VM.LOGINNAME
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="loginName != null and loginName != ''">AND VL.LOGINNAME=#{loginName}</if>
            <if test="telephone != null and telephone != ''">AND VL.TELEPHONE =#{telephone}</if>
            <if test="approvalPhone != null and approvalPhone != ''">AND VL.APPROVAL_PHONE=#{approvalPhone}</if>
            <if test="agentMerSeq != null and agentMerSeq != ''">AND VL.AGENT_MER_SEQ=#{agentMerSeq}</if>
        </trim>
        GROUP BY VL.LOGINNAME
    </select>

第四种,基于MP的AR模式,dto的配置需要继承com.baomidou.mybatisplus.extension.activerecord.Model,并且mapper接口需要继承com.baomidou.mybatisplus.core.mapper.BaseMapper。其中 BaseMapper的泛型必须关联实体。
ps:
1.实体里面如果有@TableName(“VIP_LOGIN”),则,以注解的内容里面的值到数据库里面查找,否则以实体的类名当做表名查找。
2.在解析mapper时候,如果mapper接口继承了baseMapper,则会注入动态sql。
在MybatisMapperAnnotationBuilder的parse方法中,

public void parse() {
        String resource = type.toString();
        if (!configuration.isResourceLoaded(resource)) {
            loadXmlResource();
            configuration.addLoadedResource(resource);
            final String typeName = type.getName();
            assistant.setCurrentNamespace(typeName);
            parseCache();
            parseCacheRef();
            SqlParserHelper.initSqlParserInfoCache(type);
            Method[] methods = type.getMethods();
            for (Method method : methods) {
                try {
                    // issue #237
                    if (!method.isBridge()) {
                        parseStatement(method);
                        SqlParserHelper.initSqlParserInfoCache(typeName, method);
                    }
                } catch (IncompleteElementException e) {
                    // TODO 使用 MybatisMethodResolver 而不是 MethodResolver
                    configuration.addIncompleteMethod(new MybatisMethodResolver(this, method));
                }
            }
            // TODO 注入 CURD 动态 SQL , 放在在最后, because 可能会有人会用注解重写sql
            if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) {
            	**//这里会判断mapper接口如果继承com.baomidou.mybatisplus.core.mapper.BaseMapper,则进行注入动态sql**
                GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type);
            }
        }
        parsePendingMethods();
    }

inspectInject 方法代码如下,

	public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
	        Class<?> modelClass = extractModelClass(mapperClass);//获取BaseMapper<VipLogin>的泛型,即关联的实体。
	        if (modelClass != null) {
	            String className = mapperClass.toString();
	            Set<String> mapperRegistryCache = GlobalConfigUtils.getMapperRegistryCache(builderAssistant.getConfiguration());
	            if (!mapperRegistryCache.contains(className)) {
	                List<AbstractMethod> methodList = this.getMethodList();
	                if (CollectionUtils.isNotEmpty(methodList)) {
	                    TableInfo tableInfo = TableInfoHelper.initTableInfo(builderAssistant, modelClass);
	                    // 循环注入自定义方法
	                    methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass, tableInfo));
	                } else {
	                    logger.debug(mapperClass.toString() + ", No effective injection method was found.");
	                }
	                mapperRegistryCache.add(className);
	            }
	        }
	    }

获取BaseMapper的泛型,即关联的实体。如果没有关联,则不进行初始化TableInfo,无法进行CRUD。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值