<resultMap id="menuQuery" type="com.wlpy.common.entity.Menu">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="parentId" jdbcType="VARCHAR" property="parentId" />
<result column="menuName" jdbcType="VARCHAR" property="menuName" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="type" jdbcType="VARCHAR" property="type" />
<result column="perms" jdbcType="VARCHAR" property="perms" />
<collection property="menuList" select="lowMenu" column="id" ofType="com.wlpy.common.entity.Menu">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="parentId" jdbcType="VARCHAR" property="parentId" />
<result column="menuName" jdbcType="VARCHAR" property="menuName" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="type" jdbcType="VARCHAR" property="type" />
<result column="perms" jdbcType="VARCHAR" property="perms" />
</collection>
</resultMap>
<select id="findAllMenu" parameterType="java.util.Map" resultMap="menuQuery">
SELECT
m.id,
m.parent_id AS parentId,
m.menu_name AS menuName,
m.url,
m.type,
m.perms
FROM
menu AS m
WHERE
m.available = 1
AND m.parent_id = 0
</select>
<select id="lowMenu" parameterType="java.util.Map" resultMap="menuQuery">
SELECT
m.id,
m.parent_id AS parentId,
m.menu_name AS menuName,
m.url,
m.type,
m.perms
FROM
menu AS m
WHERE
m.available = 1
AND m.parent_id = #{id} 父级ID
</select>
我看sql也不复杂,是查询慢吗,看下索引使用情况。
执行计划:explain sql语句
可以换一种方法设计表,把主键设置为varchar类型,第一级用2位(如11),第二级4位(如1101),依次类推,查询的时候模糊查询条件实现。
sql没什么问题,也没有优化的地方了,输出一下执行sql查询需要多久,如过是sql问题影响时间只能添加索引了。
你的树有几层 为何重复调用menuQuery
楼上那个表设计你可以参考下,插叙效率起飞,直接从递归变成like