用别名来简化工具类

其实别名这个技术挺鸡肋的,如果你使用了别名,Idea的代码跳转就无法实现。但是如果为一些常用的工具类设置别名,可以让mapper.xml文件看上去更优雅一点。

我们项目保留了一份MyBatis的配置文件,所以正好可以用来配置工具类的别名:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="cacheEnabled" value="true"/>  <!-- 全局映射器启用缓存 -->
        <setting name="useGeneratedKeys" value="true"/>  <!-- 允许 JDBC 支持自动生成主键 -->
        <setting name="defaultExecutorType" value="REUSE"/> <!-- 配置默认的执行器 -->
        <setting name="logImpl" value="SLF4J"/> <!-- 指定 MyBatis 所用日志的具体实现 -->
        <!-- <setting name="mapUnderscoreToCamelCase" value="true"/>  驼峰式命名 -->
    </settings>

    <typeAliases>
        <typeAlias type="org.apache.commons.collections4.CollectionUtils" alias="CollectionUtils"/>
        <typeAlias type="org.apache.commons.lang3.StringUtils" alias="StringUtils"/>
    </typeAliases>

</configuration>

在Mapper中的用法如下:

  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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

<mapper namespace="com.sdstc.tmp.server.mapper.TrendModelMapper">


    <select id="selectForUpdateById" resultType="com.sdstc.tmp.server.entity.TrendModel">
        select id, favorites_count, browse_count
        from t_trend_model
        where is_delete = '0'
          and id = #{id} for update
    </select>

    <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}
            and tv.org_id = #{tenantId}
        </if>

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

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

        <!-- 趋势主题(like查询) -->
        <if test="@StringUtils@isNotBlank(condition.theme)">
            <bind name="theme" value="'%' + condition.theme + '%'"/>
            AND trm.theme like #{theme}
        </if>

        <!-- 适用品牌 -->
        <if test="@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 personalLogin">
            AND (trm.personal_allow_view = 1 or trm.personal_allow_used = 1)
        </if>
        <!-- 允许预览,企业身份登录 -->
        <if test="loginFlag == 1 and condition.allowView == 1 and !personalLogin">
            AND (trm.company_allow_view::jsonb ??| array[#{tenantId}] or trm.company_allow_used::jsonb ??| array[#{tenantId}])
        </if>

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

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

        <!-- 型体号(like查询) -->
        <if test="@StringUtils@isNotBlank(condition.modelNumber)">
            <bind name="modelNumber" value="'%' + condition.modelNumber + '%'"/>
            AND trm.model_number like #{modelNumber}
        </if>

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

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

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

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

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

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

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

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

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

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

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

        <!-- 色系 -->
        <if test="@CollectionUtils@isNotEmpty(condition.colors)">
            AND trm.colors::jsonb ??|
            <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="@StringUtils@isNotBlank(condition.designerId)">
            AND trm.designer_id = #{condition.designerId}
        </if>

        <if test="@CollectionUtils@isNotEmpty(orderItems)">
            order by
            <foreach collection="orderItems" item="orderItem"
                     separator=",">
                ${orderItem.column} <if test="not orderItem.asc">desc</if>
            </foreach>
        </if>
    </select>
</mapper>