mybatis语句(一)java枚举、遍历list、遍历map查询

11 篇文章 0 订阅
9 篇文章 0 订阅

1.java枚举对应字段查询

 public static String findRecordByUserIdAndBizType() {
            return "SELECT * FROM "+TABLE+" WHERE userId = #{userId} and bizType = #{coinBizType, typeHandler=" + EnumTypeHandler.class.getCanonicalName() + "} order by createdTime desc";
        }

2.遍历list

public static String findRecordByUserIdAndBizType() {
            return "<script>"
                    + "SELECT * FROM "+TABLE+" WHERE userId = #{userId}"
                    +"and bizType IN "
                    + "<foreach item='item' index='index' collection='coinBizTypes' open='(' separator=',' close=')'>"
                    + "#{item, typeHandler=" + EnumTypeHandler.class.getCanonicalName() + "}"
                    + "</foreach>"
                    + "</script>";
        }
public static String findByAppModuleAndSendEndTimeAfterNowItemIds() {
            return "<script>"
                    + "SELECT * FROM "+TABLE+" WHERE appName = #{appName} AND module = #{module} AND sendEndTime > #{now} and active = 1 "
                    +"and itemId IN "
                    + "<foreach item='item' index='index' collection='itemIds' open='(' separator=',' close=')'>"
                    + "#{item}"
                    + "</foreach>"
                    + "</script>";
        }

3.遍历map作为条件查询
参考:参考文章

@Override
    public List<ExPacketRecordV2> findByUserAndPeriodMap(String userId, String appName, Map<String, String> searchTypeTimeMap) {
        return mapper.findByUserAndPeriodMap(userId, appName, searchTypeTimeMap);
    }
    
 @ResultMap("SkuRecordResultMap")
        @SelectProvider(type = ExchangePacketSqlProvider.class, method = "findByUserAndPeriodMap")
        List<ExPacketRecordV2> findByUserAndPeriodMap( @Param("userId")String userId,@Param("appName") String appName,
                                                       @Param("searchMap") Map<String, String> searchTypeTimeMap);

sql:
/**
         * select userId,skuId,amount,coin,periodType from ex_packet_record_v2 where userId='2088312991026016' and appName='shell'  and active = 0
         * and  (( periodType='YEARLY' and createdTime>'2020-01-01' ) or ( periodType='DAILY' and createdTime>'2020-11-02' ) or ( periodType='MONTHLY' and createdTime>'2020-11-01' ) or ( periodType='GLOBALLY' and createdTime>'1978-08-23' ))
         * @return
         */
        public static String findByUserAndPeriodMap() {
            return "<script>"+
                    "select userId,skuId,amount,coin,periodType from " + TABLE_RECORD +
                    " where userId=#{userId} and appName=#{appName}  and active = 1 and " +
                    "<foreach collection='searchMap.keys' separator='or' item='key' index='index' open='(' close=')'> " +
                    " ( periodType=#{key} and createdTime>#{searchMap[${key}]} )" +
                    " </foreach> "
                    + "</script>";
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值