项目优化-不变数据放在配置文件中
2016-10-31 00:00
351 查看
场景
项目中,大多数不经常变化但经常用到的数据,常常在下拉列表中使用的数据,放在数据库中是非常不好的。比如文件类型:(主要、普通);会议密级(机密、公开);会议缓急:(一般、紧急)。对于这些数据一般不会发生变化,但在页面中又经常使用,那这些数据放到数据库中,每次使用时,都需要创建连接去查询,极其占用数据库资源。
解决方法
当做静态资源,使用Constants类保存、使用java util集合类保存、等等、也可以写在spring配置文件中,接下来,就说一说如何做
Spring配置文件管理常量数据
在Controller中看到,方法上都使用了@ResponseBody注解,也就是说,返回给前台的数据都自动封装成了json数据,这样结合easyui或者jqueryUI的一些下拉列表控件就变得非常容易:
(easyui的下拉列表的data是json数组:)
项目中,大多数不经常变化但经常用到的数据,常常在下拉列表中使用的数据,放在数据库中是非常不好的。比如文件类型:(主要、普通);会议密级(机密、公开);会议缓急:(一般、紧急)。对于这些数据一般不会发生变化,但在页面中又经常使用,那这些数据放到数据库中,每次使用时,都需要创建连接去查询,极其占用数据库资源。
解决方法
当做静态资源,使用Constants类保存、使用java util集合类保存、等等、也可以写在spring配置文件中,接下来,就说一说如何做
Spring配置文件管理常量数据
package com.shark.util; public class SelectBean { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="selectBean" class="com.shark.util.SelectBean" /> <!-- 月完成中的完成情况分类 --> <bean id="statusData" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="进行中" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="推迟" /> </bean> <bean parent="selectBean"> <property name="key" value="3" /> <property name="value" value="已完成" /> </bean> </list> </constructor-arg> </bean> <!-- 文件资料库中的 文件分类 --> <bean id="fileType" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="党中央下发" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="局内文件" /> </bean> </list> </constructor-arg> </bean> <!-- 会议类别 --> <bean id="meetingType" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="局党组会议" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="局长办公会议" /> </bean> <bean parent="selectBean"> <property name="key" value="3" /> <property name="value" value="专题会议" /> </bean> </list> </constructor-arg> </bean> <!-- 重要会议【缓急】 --> <bean id="urgency" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="一般" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="特急" /> </bean> </list> </constructor-arg> </bean> <!-- 重要会议【密级】 --> <bean id="dense" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="机密" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="公开" /> </bean> </list> </constructor-arg> </bean> <!-- 文件类型 --> <bean id="importType" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="主要" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="普通" /> </bean> </list> </constructor-arg> </bean> <!-- 批示类别 --> <bean id="instructionClass" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="中央领导批示件" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="委领导批示件" /> </bean> <bean parent="selectBean"> <property name="key" value="3" /> <property name="value" value="局领导批示件" /> </bean> <!-- <bean parent="selectBean"> <property name="key" value="4" /> <property name="value" value="办公厅领导批示件" /> </bean> --> </list> </constructor-arg> </bean> <!-- 决策类别 --> <bean id="decisionCategory" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="中央重大决策部署" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="政府工作报告" /> </bean> </list> </constructor-arg> </bean> <!-- 督办类别 --> <bean id="superviseCategory" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="中办督查事项" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="国办督查事项" /> </bean> <bean parent="selectBean"> <property name="key" value="3" /> <property name="value" value="委办督查事项" /> </bean> </list> </constructor-arg> </bean> <!-- 常务委员 --> <bean id="standingCommittee" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="习近平" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="李克强" /> </bean> <bean parent="selectBean"> <property name="key" value="3" /> <property name="value" value="俞正声" /> </bean> <bean parent="selectBean"> <property name="key" value="4" /> <property name="value" value="张高丽" /> </bean> <bean parent="selectBean"> <property name="key" value="5" /> <property name="value" value="汪洋" /> </bean> <bean parent="selectBean"> <property name="key" value="6" /> <property name="value" value="马凯" /> </bean> <bean parent="selectBean"> <property name="key" value="7" /> <property name="value" value="其他中央领导" /> </bean> </list> </constructor-arg> </bean> <!-- 商谈答复类别 --> <bean id="discussReply" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="会见" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="调研" /> </bean> </list> </constructor-arg> </bean> <!-- 专项督办报告 --> <bean id="specialSupervision" class="java.util.ArrayList"> <constructor-arg> <list> <bean parent="selectBean"> <property name="key" value="1" /> <property name="value" value="委领导批示件" /> </bean> <bean parent="selectBean"> <property name="key" value="2" /> <property name="value" value="局领导批示件" /> </bean> </list> </constructor-arg> </bean> </beans>
package com.shark.controller; import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.shark.util.SelectBean; /** * 系统的公共类,主要处理<b>下拉列表</b>里的数据 * * @author lianze * @version JDK1.6 * @Date 2016-04-25 */ @Controller @RequestMapping("commonDataController") public class CommonDataController { @Autowired private ArrayList<SelectBean> statusData; @Autowired private ArrayList<SelectBean> meetingType; @Autowired private ArrayList<SelectBean> fileType; @Autowired private ArrayList<SelectBean> urgency; @Autowired private ArrayList<SelectBean> dense; @Autowired private ArrayList<SelectBean> importType; @Autowired private ArrayList<SelectBean> instructionClass; @Autowired private ArrayList<SelectBean> decisionCategory; @Autowired private ArrayList<SelectBean> superviseCategory; @Autowired private ArrayList<SelectBean> standingCommittee; @Autowired private ArrayList<SelectBean> specialSupervision; @Autowired private ArrayList<SelectBean> discussReply; /** * 月完成中的完成情况分类 * * @return */ @ResponseBody @RequestMapping("getStatusData.do") public ArrayList<SelectBean> getStatusData() { return statusData; } /** * 会议类别 * * @return */ @ResponseBody @RequestMapping("getMeetingType.do") public ArrayList<SelectBean> getMeetingType() { return meetingType; } /** * 文件资料库中的 文件分类 * * @return */ @ResponseBody @RequestMapping("getFileType.do") public ArrayList<SelectBean> getFileType() { return fileType; } /** * 重要会议【缓急】 * * @return */ @ResponseBody @RequestMapping("getUrgency.do") public ArrayList<SelectBean> getUrgency() { return urgency; } /** * 重要会议【密级】 * * @return */ @ResponseBody @RequestMapping("getDense.do") public ArrayList<SelectBean> getDense() { return dense; } /** * 重要程度 * * @return */ @ResponseBody @RequestMapping("getImportType.do") public ArrayList<SelectBean> getImportType() { return importType; } /** * 批示类别 * * @return */ @ResponseBody @RequestMapping("getInstructionClass.do") public ArrayList<SelectBean> getInstructionClass() { return instructionClass; } /** * 决策类别 * * @return */ @ResponseBody @RequestMapping("getDecisionCategory.do") public ArrayList<SelectBean> getDecisionCategory() { return decisionCategory; } /** * 督办类别 * * @return */ @ResponseBody @RequestMapping("getSuperviseCategory.do") public ArrayList<SelectBean> getSuperviseCategory() { return superviseCategory; } /** * 常务委员 * * @return */ @ResponseBody @RequestMapping("getStandingCommittee.do") public ArrayList<SelectBean> getStandingCommittee() { return standingCommittee; } /** * 专项督报 * * @return */ @ResponseBody @RequestMapping("getSpecialSupervision.do") public ArrayList<SelectBean> getSpecialSupervision() { return specialSupervision; } /** * 商谈答复类别 * * @return */ @ResponseBody @RequestMapping("getdiscussReply.do") public ArrayList<SelectBean> getdiscussReply() { return discussReply; } }
在Controller中看到,方法上都使用了@ResponseBody注解,也就是说,返回给前台的数据都自动封装成了json数据,这样结合easyui或者jqueryUI的一些下拉列表控件就变得非常容易:
<div style="padding: 10px"> <label for="typeId">文件类型:</label> <input class="easyui-combobox" class="easyui-validatebox" name="typeId" id="typeId" style="width: 180px" data-options="url:'${pageContext.request.contextPath}/commonDataController/getFileType.do',valueField:'key',textField:'value',panelHeight:'auto', required:'true',missingMessage:'必须选择类型'" /> </div>
(easyui的下拉列表的data是json数组:)
![](https://static.oschina.net/uploads/space/2016/1031/200644_6HKP_2475326.png)
相关文章推荐
- android的数据与访问(1)-我的app配置参数文件放在哪儿?
- Grails 项目配置数据连接的配置文件的编写 Grails连接Oracle grails连接Mysql 的配置文件
- Ocelot简易教程(六)之重写配置文件存储方式并优化响应数据
- 项目中配置文件优化方案
- 项目加载时,配置文件读取参数并把参数放在缓存中。
- Tomcat优化发布项目流程(项目/配置文件文件分离)
- SSH框架,AjaxJson显示数据,项目框架搭建;注意action跳转不用叹号,springMVC和springAnnotation要修改spring配置文件
- java读取项目配置文件中配置的数据
- web项目xml配置文件获取数据 和TDES 加码
- 使用XML作为项目的配置文件使用,并解析之,获得数据作为链接数据库的参数
- winds引导配置数据文件包含的os项目无效
- 使用XML作为项目的配置文件使用,并解析之,获得数据作为链接数据库的参数
- 项目中配置文件优化方案
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题
- 基于maven的spring boot 项目porm文件配置(含定时器,数据抓取,分词器依赖配置)
- 大数据Spark“蘑菇云”项目实战第63课: 广告点击系统高可用性和性能优化 checkpoint wal driver高可用 并行度配置
- 离线数据分析项目 bf_Transformer 配置文件
- 将 Log4j 配置文件放在项目外面
- winds引导配置数据文件包含的os项目无效
- 如何获取web项目hibernate.cfg.xml配置文件中的数据