您的位置:首页 > 编程语言 > Java开发

项目优化-不变数据放在配置文件中

2016-10-31 00:00 351 查看
场景

项目中,大多数不经常变化但经常用到的数据,常常在下拉列表中使用的数据,放在数据库中是非常不好的。比如文件类型:(主要、普通);会议密级(机密、公开);会议缓急:(一般、紧急)。对于这些数据一般不会发生变化,但在页面中又经常使用,那这些数据放到数据库中,每次使用时,都需要创建连接去查询,极其占用数据库资源。

解决方法

当做静态资源,使用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数组:)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring
相关文章推荐