目录
1.查询结果段代码
<!--多张表连接查询,字段起别名-->
<sql id="resultList">
SELECT
a.id AS id,
c.id AS unitId,
c.name AS unitName,
b.title AS title,
isnull(a.price)!=1 as hasPrice,
a.modifier AS modifier,
a.modify_time AS modifyTime
FROM
table_a a
LEFT JOIN table_b b ON a.info_id = b.id
LEFT JOIN table_c c ON b.unit_id = c.id
</sql>
-
(1)字段起别名
如上段代码,三张表做关联查询,给c表的name字段起别名为unitName;给a表的modify_time字段起别名为modifyTime; 给isnull(a.price)!=1的结果命名为hasPrice。
-
(2)字段为null,则结果为false,否则为true
如上段代码,其中isnull(a.price)!=1 as hasPrice 表示如果a表的price字段有数据则结果为1,如果a表的price字段为null则结果为0。结果映射到java的布尔类型属性上,1就为true,0就为false;
代码执行逻辑分析:isnull(a.price)只有两种结果。结果为1,表示a表的price为空,结果为0,表示a表的price不为空。再判断isnull(a.price)的结果是否不等于1,相当于给isnull(a.price)的结果取反,即得到预期的结果。
2.查询条件段代码
<include refid="resultList"/>
<where>1=1
<if test="startDate != null and endDate != null">
and ((start_date BETWEEN #{startDate} AND #{endDate})
OR (end_date BETWEEN #{startDate} AND #{endDate})
OR (start_date < #{startDate} AND end_date > #{endDate}))
</if>
<if test="unitId != null and unitId !=''">
and c.id=#{unitId}
</if>
<if test="title != null and title !=''">
and b.title like concat('%', #{title}, '%')
</if>
/*默认排序:未配置价格的显示在前,已配置显示在后;再根据更新时间倒序排序;最后根据交易单元排序;*/
<if test="sortParam == null or sortParam.sortName == null">
order by isnull(a.price) desc,a.modify_time desc,c.sort asc
</if>
<if test="sortParam != null">
<if test="sortParam.sortName == 'unitName'">
/*根据拼音排序*/
order by CONVERT(c.abbreviation USING gbk)
</if>
<if test="sortParam.sortName == 'title'">
order by b.title
</if>
<if test="sortParam.sortName == 'hasPrice'">
order by isnull(a.price)!=1
</if>
<if test="sortParam.sortName == 'modifier'">
order by a.modifier
</if>
<if test="sortParam.sortName == 'modifyTime'">
order by a.modify_time
</if>
<if test="sortParam.orderType == 1">
asc
</if>
<if test="sortParam.orderType == 2">
desc
</if>
</if>
</where>
查询条件段完整段代码如上。
-
(1)查询日期范围与条件参数日期范围有交集的数据
需求:查询数据库表字段start_date和end_date日期范围内有落在条件参数[startDate,endDate]之间的日期的数据;
<if test="startDate != null and endDate != null">
and ((start_date BETWEEN #{startDate} AND #{endDate})
OR (end_date BETWEEN #{startDate} AND #{endDate})
OR (start_date < #{startDate} AND end_date > #{endDate}))
</if>
代码分析:查询出[start_date,end_date]和[startDate,endDate]有交集的数据;其中<表示小于号<,>表示大于号;
-
(2)根据条件动态排序
需求:如果排序条件参数为空,则自定义默认的排序;否则,根据条件参数名来指定对应的表对应的字段做排序。代码如下:
/*默认排序:未配置价格的显示在前,已配置显示在后;再根据更新时间倒序排序;最后根据交易单元排序;*/
<if test="sortParam == null or sortParam.sortName == null">
order by isnull(a.price) desc,a.modify_time desc,c.sort asc
</if>
<if test="sortParam != null">
<if test="sortParam.sortName == 'unitName'">
/*根据拼音排序*/
order by CONVERT(c.abbreviation USING gbk)
</if>
<if test="sortParam.sortName == 'title'">
order by b.title
</if>
<if test="sortParam.sortName == 'hasPrice'">
order by isnull(a.price)!=1
</if>
<if test="sortParam.sortName == 'modifier'">
order by a.modifier
</if>
<if test="sortParam.sortName == 'modifyTime'">
order by a.modify_time
</if>
<if test="sortParam.orderType == 1">
asc
</if>
<if test="sortParam.orderType == 2">
desc
</if>
</if>
-
(3)根据字段拼音排序
<if test="sortParam.sortName == 'unitName'">
/*根据拼音排序*/
order by CONVERT(c.abbreviation USING gbk)
</if>
在MySQL数据库中,使用UTF-8编码时,默认的排序规则(如utf8_general_ci)并不支持按拼音顺序排序中文字符。常规做法是利用CONVERT
函数将字符集转换为GBK,从而实现拼音排序。