100 Star 318 Fork 151

Mirren / Spring-generator

 / 详情

查询列表按时时间排序时返回结果没有排序

已完成
创建于  
2019-12-04 16:19

在生成的mapper中,select操作对分页进行优化时使用了inner join,这里如果使用非连接属性进行排序,如果驱动表是左表,那么排序就会失效。

	<select id="selectStorageInfo" parameterType="com.common.Assist" resultMap="result_StoreStorageInfo_Map">
		select
		<if test="distinct !=null">${distinct}</if>
		<choose>
			<when test="resultColumn!=null">${resultColumn}</when>
			<otherwise>
				<include refid="store_storage_info_Column" />
			</otherwise>
		</choose>
		from store_storage_info
		<if test="require!=null">
			<include refid="assist" />
		</if>
		<if test="order !=null">${order}</if>
		<choose>
			<when test="startRow !=null and rowSize !=null">LIMIT #{startRow},#{rowSize} </when>
			<when test="rowSize !=null">LIMIT #{rowSize}</when>
		</choose>
	</select>

评论 (3)

zhuyuheng 创建了任务

原谅我看了几遍还是没有看明白你的意思,只指生成的语句错误还是别的,我应该怎么做呢,因为这里并没有表连接

是我表述的太抽象了,我再具体一点:

<select id="selectStorageInfo" parameterType="com.common.Assist" resultMap="result_StoreStorageInfo_Map">
		select
		<if test="distinct !=null">${distinct}</if>
		<choose>
			<when test="resultColumn!=null">${resultColumn}</when>
			<otherwise>
				<include refid="store_storage_info_Column" />
			</otherwise>
		</choose>
		from store_storage_info
		<if test="require!=null">
			<include refid="assist" />
		</if>
		<if test="order !=null">${order}</if>
		<choose>
			<when test="startRow !=null and rowSize !=null">LIMIT #{startRow},#{rowSize} </when>
			<when test="rowSize !=null">LIMIT #{rowSize}</when>
		</choose>
	</select>

表中有一个字段是update_time,查询时根据update_time desc排序
这个xml转换成sql以后是:

select table.id, table.foreignId, table.update_time
from 
table 
inner join 
(select id from table where foreignId = 123 order by update_time desc limit 0,10) 
as tmp on table.id = tmp.id

通过这条sql进行查询时,子查询中的结果是根据update_time进行排序的,而inner join后排序的方式却不确定了,如果是按左边的table表作为驱动表则是按照table的默认方式进行排序,如果是以tmp临时表为驱动则是我们所期望的按照update_time来排序。我这里插入了5条数据,通过以上sql查询后的结果如下:
输入图片说明
查询结果并没有按照update_time desc进行排序。

@zhuyuheng 正常来说inner join 里面排序了外面就排序了,如果不起作用的话你有两种方式可以处理,
1: 这个inner join是用来优化数据多时分页优化的,如果你认为没有必要可以在模板中取消这个inner join
2: 外面也加多一个order

Mirren 任务状态待办的 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
674926 mirren 1578930549
Java
1
https://gitee.com/mirren/Spring-generator.git
git@gitee.com:mirren/Spring-generator.git
mirren
Spring-generator
Spring-generator

搜索帮助