Vue版本解决首页加载慢的问题 平均10s
收藏
解决初次加载慢的问题:
经过测试,发现主要是getMenus方法里的 获取字典方法慢
数据库放在服务器上 没更改之前的时间平均10s左右
修改方法如下:
1:在DictValueMapper里添加以下代码:
public List<DictValue> getDictValueByType();
2:在DictValueMapper.xml里添加以下代码:
<select id="getDictValueByType" resultType="DictValue">
SELECT
<include refid="dictValueColumns"/>
FROM
sys_dict_value a
<where>
a.dict_type_id = #{dict_type_id}
<!-- 变量名 dict_type_id 对应上文的 dict_type_id -->
<!-- 如果上文中 collection只传一个参数column="id",只要类型匹配,在这里随便写个变量名就可以取到值 #{xyz} -->
</where>
</select>
3:在DictTypeMapper添加以下代码:
public List<DictType> getDict();
4:在DictTypeMapper.xml添加一下代码:
<resultMap id="BaseResultMap" type="com.jeeplus.modules.sys.entity.DictType" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="VARCHAR" />
<collection property="dictValueList" javaType="java.util.ArrayList" ofType="com.jeeplus.modules.sys.entity.DictValue"
select="com.jeeplus.modules.sys.mapper.DictValueMapper.getDictValueByType"
column="{dict_type_id=id}" />
<!-- dict_type_id是定义的变量名, id是主表的字段id/sort 多个参数 column="{dict_type_id=id,xx=xx}",
先查出主表的结果, 然后主表记录数是几 就执行几次 collection 的select,
javaType写不写都行 mybatis会自行匹配,
select的值: 对应xml的namespace + 对应xml中的代码片段的id,
column作为select语句的参数传入,如果只传一个参数id可以简写: column="id" -->
</resultMap>
<sql id="dictTypeJoins">
</sql>
<select id="getDict" resultMap="BaseResultMap">
select * from sys_dict_type
<where>
</where>
</select>
5:修改DictUtils中的getDictList方法:
public static Map<String, List<DictValue>> getDictMap(){
@SuppressWarnings("unchecked")
Map<String, List<DictValue>> dictMap = (Map<String, List<DictValue>>)CacheUtils.get(CACHE_DICT_MAP);
if (dictMap==null){
dictMap = Maps.newHashMap();
//原来的代码
// for (DictType dictType : dictTypeService.findList(new DictType())){
// List<DictValue> dictList = dictMap.get(dictType.getType());
// dictType = dictTypeService.get(dictType.getId());
// if (dictList != null){
// dictList.addAll(dictType.getDictValueList());
// }else{
// dictMap.put(dictType.getType(), Lists.newArrayList(dictType.getDictValueList()));
// }
// }
//更改之后
List<DictType> dict = dictTypeService.getDict();
for (DictType dictType:
dict) {
dictMap.put(dictType.getType(),dictType.getDictValueList());
}
CacheUtils.put(CACHE_DICT_MAP, dictMap);
}
return dictMap;
}
总结:改成 嵌套查询 优化最终时间 2.46s左右
-
2020-05-01 10:01:25
最后修改:2020-06-04 18:29:10
我这个用的是嵌套查询的 一种方式 还有一种方式 只需要一条sql查询 但是不知道为什么子表数据一直都是一条数据,望大家搞搞 那个搞出来估计就2s左右就可以了
-
2020-05-01 10:01:25
最后修改:2020-06-04 18:29:10
我这个用的是嵌套查询的 一种方式 还有一种方式 只需要一条sql查询 但是不知道为什么子表数据一直都是一条数据,望大家搞搞 那个搞出来估计就2s左右就可以了
-
2020-05-03 22:24:28
最后修改:2020-05-05 10:01:28
@兔子321 修改4处的getDict与BaseResultMap,可实现一条sql查询
[pre]
<resultMap id="BaseResultMap" type="com.jeeplus.modules.sys.entity.DictType" >
<id column="type_id" property="id" jdbcType="VARCHAR" />
<result column="type_type" property="type" jdbcType="VARCHAR" />
<collection property="dictValueList" javaType="java.util.ArrayList" ofType="com.jeeplus.modules.sys.entity.DictValue">
<id column="id" property="id"></id>
<result column="label" property="label"></result>
<result column="value" property="value"></result>
<result column="sort" property="sort"></result>
<result column="dict_type_id" property="dictType.id"></result>
</collection>
</resultMap>
<select id="getDict" resultMap="BaseResultMap">
select
t.id as type_id,
t.type as type_type,
v.*
from sys_dict_type t
LEFT JOIN sys_dict_value v ON t.id = v.dict_type_id
<where>
</where>
</select>
[/pre]
-
2020-05-09 11:05:46
求代码粘全啊!