一个列表为另一个列表的子集的SQL如何编写,及在MyBatis中如何处理

需求是这样的,一个字段它在创建的时候是允许多选的,然后再分页的时候,用户传递的条件它也是多选的,所以我们需要判断一个集合中得元素是否全部属于另一个集合。

举例来说,就是判断["A", "B"]是否属于["A", "B", "C", "D"],在PG中,该需求可以通过如下SQL实现:

1
2
3

select * from t_trend_model where company_view_allow::jsonb ?& array['83','42'];

但是该SQL是不能直接移植到MyBatis中使用了,问题一是&符号要转义,问题二是?要写成??,代码如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

    <select id="pageTrendModel" resultType="com.sdstc.tmp.server.entity.TrendModel">
        select * from t_trend_model trm

        <!-- 登录状态,且搜索条件中包含收藏 -->
        <if test="loginFlag == 1 and condition.collect == 1">
            join t_favorite tf on trm.id = tf.trend_model_id
        </if>

        <!-- 登录状态,且搜索条件中包含浏览 -->
        <if test="loginFlag == 1 and condition.viewed == 1">
            join t_viewed tv on trm.id = tv.trend_model_id
        </if>
        where trm.is_delete = 0

        <!-- 登录状态,且搜索条件中包含收藏 -->
        <if test="loginFlag == 1 and condition.collect == 1">
            and tf.is_delete = 0
            and tf.creator = #{userId}
        </if>

        <!-- 登录状态,且搜索条件中包含浏览 -->
        <if test="loginFlag == 1 and condition.viewed == 1">
            and tv.is_delete = 0
            and tv.creator = #{userId}
        </if>

        <!-- 关键字 -->
        <if test="@org.apache.commons.lang3.StringUtils@isNotBlank(condition.keyword)">
            <bind name="keyword" value="'%' + condition.keyword + '%'"/>
            and (trm.theme like #{keyword} or trm.design_points like #{keyword})
        </if>

        <!-- 趋势主题 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.themes)">
            AND trm.theme in
            <foreach collection="condition.themes" open="(" close=")" item="themes" separator=",">
                #{theme}
            </foreach>
        </if>

        <!-- 适用品牌 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.brands)">
            AND trm.brand in
            <foreach collection="condition.brands" open="(" close=")" item="brand" separator=",">
                #{brand}
            </foreach>
        </if>

        <!-- 允许预览 -->
        <if test="loginFlag == 1 and condition.allowView == 1">
            AND (trm.personal_allow_view = 1 or trm.company_view_allow::jsonb ??&amp; array[#{tenantId}])
        </if>

        <!-- 发布状态 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.publishStatus)">
            AND trm.publish_status in
            <foreach collection="condition.publishStatus" open="(" close=")" item="publishStatusItem" separator=",">
                #{publishStatusItem}
            </foreach>
        </if>

        <!-- 型体号 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.modelNumbers)">
            AND trm.model_number in
            <foreach collection="condition.modelNumbers" open="(" close=")" item="modelNumber" separator=",">
                #{modelNumber}
            </foreach>
        </if>

        <!-- 鞋底 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.soles)">
            AND trm.sole in
            <foreach collection="condition.soles" open="(" close=")" item="sole" separator=",">
                #{sole}
            </foreach>
        </if>

        <!-- 跟型 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.heelTypes)">
            AND trm.heel_type in
            <foreach collection="condition.heelTypes" open="(" close=")" item="heelType" separator=",">
                #{heelType}
            </foreach>
        </if>

        <!-- 跟高 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.heelHeights)">
            AND trm.heel_height in
            <foreach collection="condition.heelHeights" open="(" close=")" item="heelHeight" separator=",">
                #{heelHeight}
            </foreach>
        </if>

        <!-- 款式类型 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.shapeTypes)">
            AND trm.shape_type in
            <foreach collection="condition.shapeTypes" open="(" close=")" item="shapeType" separator=",">
                #{shapeType}
            </foreach>
        </if>

        <!-- 流行趋势 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.trendTags)">
            AND trm.trend_tags::jsonb ??&amp;
            <foreach collection="condition.trendTags" open="array[" close="]" item="trendTag" separator=",">
                #{trendTag}
            </foreach>
        </if>

        <!-- 流行地区 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.regions)">
            AND trm.regions::jsonb ??&amp;
            <foreach collection="condition.regions" open="array[" close="]" item="region" separator=",">
                #{region}
            </foreach>
        </if>

        <!-- 场景风格 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.styles)">
            AND trm.styles::jsonb ??&amp;
            <foreach collection="condition.styles" open="array[" close="]" item="style" separator=",">
                #{style}
            </foreach>
        </if>

        <!-- 面料元素 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.materialElements)">
            AND trm.material_elements::jsonb ??&amp;
            <foreach collection="condition.materialElements" open="array[" close="]" item="materialElement"
                     separator=",">
                #{materialElement}
            </foreach>
        </if>

        <!-- 装饰元素 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.decorativeElements)">
            AND trm.decorative_elements::jsonb ??&amp;
            <foreach collection="condition.decorativeElements" open="array[" close="]" item="decorativeElement"
                     separator=",">
                #{decorativeElement}
            </foreach>
        </if>

        <!-- 工艺 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.crafts)">
            AND trm.crafts::jsonb ??&amp;
            <foreach collection="condition.crafts" open="array[" close="]" item="craft"
                     separator=",">
                #{craft}
            </foreach>
        </if>

        <!-- 适用季节 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.seasons)">
            AND trm.seasons::jsonb ??&amp;
            <foreach collection="condition.seasons" open="array[" close="]" item="season"
                     separator=",">
                #{season}
            </foreach>
        </if>

        <!-- 色系 -->
        <if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(condition.colors)">
            AND trm.colors::jsonb ??&amp;
            <foreach collection="condition.colors" open="array[" close="]" item="color"
                     separator=",">
                #{color}
            </foreach>
        </if>

        <!-- 是否推荐 -->
        <if test="condition.isRecommend != null">
            AND trm.is_recommend = #{condition.isRecommend}
        </if>

        <!-- 设计师Id -->
        <if test="@org.apache.commons.lang3.StringUtils@isNotBlank(condition.designerId)">
            AND trm.designer_id = #{condition.designerId}
        </if>
    </select>

参考资料

  1. PostgreSQL JSONB 使用入门
  2. Mybatis特殊字符转义以及sql中带有问号(?)的处理方式