如题

  1. 能用 *ByExample 解决的问题,决不要自造轮子。
  2. 通过 MBG 生成的 mapper 代码示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public interface CrmOrderMapper {
    long countByExample(CrmOrderExample example);

    int deleteByExample(CrmOrderExample example);

    int deleteByPrimaryKey(Long orderID);

    int insert(CrmOrder record);

    int insertSelective(CrmOrder record);

    List<CrmOrder> selectByExample(CrmOrderExample example);

    CrmOrder selectByPrimaryKey(Long orderID);

    int updateByExampleSelective(@Param("record") CrmOrder record, @Param("example") CrmOrderExample example);

    int updateByExample(@Param("record") CrmOrder record, @Param("example") CrmOrderExample example);

    int updateByPrimaryKeySelective(CrmOrder record);

    int updateByPrimaryKey(CrmOrder record);
    }
  3. 调用示例,重点看代码里的备注。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    //下面通过 Criteria 组织多个 and 条件,相比直接 Example.or().and*().and*() 的方式,更加灵活,可以对查询条件进行判断,如果不满足条件则不添加某个 and 条件。
    TradeSmsFlowStepExample tradeSmsFlowStepExample = new TradeSmsFlowStepExample();
    TradeSmsFlowStepExample.Criteria tradeSmsFlowStepExampleCriteria = tradeSmsFlowStepExample.createCriteria();
    tradeSmsFlowStepExampleCriteria.andStepTypeEqualTo(searchStepType);
    tradeSmsFlowStepExampleCriteria.andTidEqualTo(shopOrderSmsInfoView.getTid());
    tradeSmsFlowStepExampleCriteria.andShopIDEqualTo(shopOrderSmsInfoView.getShopID());
    List<TradeSmsFlowStep> tradeSmsFlowSteps = tradeSmsFlowStepMapper.selectByExample(tradeSmsFlowStepExample);

    //用多and表达式 or().and*().and*()
    ShopConfigExample shopConfigExample = new ShopConfigExample();
    shopConfigExample.or().andIsNotifyBalanceLessEqualTo(YesOrNoEnum.yes.getCode()).andIsNotifyBeforeExpireEqualTo(YesOrNoEnum.yes.getCode());
    //生成的sql语句如下:
    select ID, shopID, mobile, email, isNotifyBalanceLess, balanceLessValue, isNotifyBeforeExpire, beforeDaysExpire, isNotifyUrgeEffect, isNotifyTrafficLess, trafficLessValue, isNotifyNewPromotion, created, modified from crmShopConfig WHERE ( isNotifyBalanceLess = ? and isNotifyBeforeExpire = ? )

    //通过多个Criteria变量实现or逻辑
    //如上,先定义一个 Criteria 变量:tradeSmsFlowStepExampleCriteria,设置好查询条件。如果还有or的逻辑,则再定义另外一个Criteria变量tradeSmsFlowStepExampleCriteriaOr,再设置查询条件,用下面的语法,即可实现or逻辑。
    tradeSmsFlowStepExample.or(tradeSmsFlowStepExampleCriteriaOr);

    //通过多个Example.or()语句实现or逻辑
    ShopConfigExample shopConfigExample = new ShopConfigExample();
    shopConfigExample.or().andIsNotifyBalanceLessEqualTo(YesOrNoEnum.yes.getCode());
    shopConfigExample.or().andIsNotifyBeforeExpireEqualTo(YesOrNoEnum.yes.getCode());
    //生成的sql语句如下:
    select ID, shopID, mobile, email, isNotifyBalanceLess, balanceLessValue, isNotifyBeforeExpire, beforeDaysExpire, isNotifyUrgeEffect, isNotifyTrafficLess, trafficLessValue, isNotifyNewPromotion, created, modified from crmShopConfig WHERE ( isNotifyBalanceLess = ? ) or( isNotifyBeforeExpire = ? )