Mybatis-generator生成Example使用心得

1.Generator插件生成Mapper和Example文件介绍
1-1.Mapper文件
mapper层接口中主要提供一些关于主键和通过Example类作为参数的方法,如下:

1-2.Example文件
mybatis-generator (逆向工程)会生成实例以及实例对应的Example,其主要作用就是能够自己组装sql语句中where后面的条件。

xxxExample example = new xxxExample();
example.selectProperties(...properties).setDistinct(true);
example.createCriteria().andIn("aac147",licenceIdList);

2.使用中遇到的问题

2-1.or()和createcriteria的区别?

createcriteria,当没有规则时,则加入到现有规则,但有规则时,不再加入到现有规则,只是返回创建的规则,如源码所示:

public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
}

 

or,创建的规则,加入到规则集中,并且是or的关系,如源码所示:

public Criteria or() {
    Criteria criteria = createCriteriaInternal();
    oredCriteria.add(criteria);
    return criteria;
}

2-2.selectByExample() 和 selectByExampleWithBLOGs()的区别?

selectByExampleWithBLOGs这个方法是我在开发过程中遇到问题后,才发现有这样一个方法。
当时是因为我表中有个字段类型为text长字段,使用selectByExample查询时,基本字段都能查找到,除了这个text类型的字段一直为空。

1.两个方法的返回的resultMap不同
selectByExample 方法返回:BaseResultMap
selectByExampleWithBLOBs 方法返回:ResultMapWithBLOBs
ResultMapWithBLOBs 定义时,继承了BaseResultMap,并且拥有自己特殊的字段,该字段通常是longvarchar类型

2.使用场景不同
若检索大字段时,则需要使用selectByExampleWithBLOBs ,一般情况则使用selectByExample 即可。

2-3.Mybatis逆向工程如何多表查询?

mybatisGenerator生成的Example都是用于单表操作的,如果需要进行多表查询,就和mybatis进行多表查询一样,在xml中自己写sql。


参考链接:https://blog.csdn.net/qq_43318965/article/details/106665863

posted @ 2025-06-16 13:59  思凡念真  阅读(165)  评论(0)    收藏  举报