您的位置:首页 > 其它

功能设计源码以及思路-字典篇

2014-06-19 20:18 218 查看
功能说明:在项目中,将一些简单的需要高频率的使用的字段加载在缓存中,减少与数据库的连接!
具体设计:
总体分为两块:
第一块是数据的加载:
1,首先是xml

<servlet>
<servlet-name>dhservlet</servlet-name>
<servlet-class>***.***.cache.servlet.DictionaryHelperServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

2然后是该指定类:
public class DictionaryHelperServlet extends HttpServlet{
/**
* 系统启动初始化加载字典缓存
*/
@Override
public void init() throws ServletException {
DictionaryHelper dictionaryHelper = new DictionaryHelper();
dictionaryHelper.joinCache();
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
}

3然后在调用数据库中的数据存入缓存中
public class DictionaryHelper {
private int t = 0;
/**
* 加入缓存
*/
public void joinCache(){
Map map = getDictCacheMap();
LoginCache.put(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER, map, 3600);
}

public static String toJson(String dictId){
DictionaryHelper dh = new DictionaryHelper();
return StringUtil.getSelectJson(dh.getDictById(dictId), false);
}

/**
* 重新封装字典
* @return
*/
private Map getDictCacheMap(){
Map<String,List<DictDataInfo>> retMap = new HashMap<String, List<DictDataInfo>>();
List<DictManagerInfo> dmList = getDictionaryHelper();
List<DictDataInfo> ddList = getDictInfoList();
if(dmList != null){
List<DictDataInfo> retList = null;
for (int i = 0; i < dmList.size(); i++) {
DictManagerInfo managerInfo = dmList.get(i);
for(int j = 0; j < ddList.size(); j++){
DictDataInfo dataInfo = ddList.get(j);
if(managerInfo.getDict_code().equals(dataInfo.getDict_code())){
retList = retMap.get(managerInfo.getDict_code());
if(retList == null){
retList = new ArrayList<DictDataInfo>();
}
retList.add(dataInfo);
System.out.println("a"+managerInfo.getDict_code());
retMap.put(managerInfo.getDict_code(), retList);
}
}
}
}
return retMap;
}

/**
* 取所有父字典信息
* @return
*/
private List<DictManagerInfo> getDictionaryHelper(){
CompService com = BaseDAOFactory.getCompService();
Map dictmap = (Map)com.invoke("DictManagerData", "getDictManagerLists");
String rnt = "";
if(dictmap!=null){
rnt = StringUtil.toString(dictmap.get("rnt"));
}
List<DictManagerInfo> list = null;
if("1".equals(rnt)){
list = (List<DictManagerInfo>) dictmap.get("dmList");
}
return list;
}

/**
* 取所有子字典信息
* @return
*/
private List<DictDataInfo> getDictInfoList(){
CompService com = BaseDAOFactory.getCompService();
Map dictmap = (Map)com.invoke("DictManagerData", "getAllDictDataInfo");
String rnt = "";
if(dictmap!=null){
rnt = StringUtil.toString(dictmap.get("rnt"));
}
List<DictDataInfo> list = null;
if("1".equals(rnt)){
list = (List<DictDataInfo>) dictmap.get("ddList");
}
return list;

}

/**
* 取字典缓存
* @return
*/
private Map getDictCacheList(){
Map dictmap = (Map) LoginCache.get(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER);
if(dictmap == null){
joinCache();
t ++;
if(t < 3){
return getDictCacheList();
}
}

return dictmap;
}

/**
* 查询指定表的信息,返回的两列数据作为菜单的optionValue 和optionText
* @param tableTag 要查询的表的别名
* @param optionValue optionValue的列名
* @param optionText optionText的列名
* @param filterSql 其他约束条件 缺省值是“1=1”
* @return
*/
public List<CustomDictInfo> getCustonDict(String tableTag,String optionValue,String optionText,String filterSql){
List<CustomDictInfo> list = null;

if(tableTag!=null && !"".equals(tableTag)){
if(filterSql==null || "".equals(filterSql)){
filterSql = " 1=1 ";
}
CompService com = BaseDAOFactory.getCompService();
com.addParameter(tableTag);
com.addParameter(optionValue);
com.addParameter(optionText);
com.addParameter(filterSql);
Map dictmap = (Map)com.invoke("DictionaryHelperData", "getCustomDictList");

String rnt = "";
if(dictmap!=null){
rnt = StringUtil.toString(dictmap.get("rnt"));
}
if("1".equals(rnt)){
list = (List<CustomDictInfo>) dictmap.get("cdList");
}
}

return list;
}

/**
* 根据父字典ID查询子项列表
* @param dictId
* @return
*/
public List<DictDataInfo> getDictById(String dictId){
Map dictmap = getDictCacheList();
List<DictDataInfo> dataInfos = (List<DictDataInfo>) dictmap.get(dictId);
return dataInfos;
}

/**
* 根据父字典ID和子字典ID查询文本内容
* @param dictId
* @param id
* @return
*/
public String getDictTextById(String dictId,String id){
Map dictmap = getDictCacheList();
List<DictDataInfo> dataInfos = (List<DictDataInfo>) dictmap.get(dictId);
String text = "";
if(dataInfos != null){
for (int i = 0; i < dataInfos.size(); i++) {
DictDataInfo dataInfo = dataInfos.get(i);
if(id.equals(dataInfo.getDict_small_code())){
text = dataInfo.getDict_text();
break;
}
}
}
return text;
}

/**
* 根据父字典ID和子字典文本内容查询子字典ID
* @param dictId
* @param id
* @return
*/
public String getDictIdByText(String dictId,String text){
Map dictmap = getDictCacheList();
List<DictDataInfo> dataInfos = (List<DictDataInfo>) dictmap.get(dictId);
String id = "";
if(dataInfos != null){
for (int i = 0; i < dataInfos.size(); i++) {
DictDataInfo dataInfo = dataInfos.get(i);
if(text.equals(dataInfo.getDict_text())){
id = dataInfo.getDict_code();
break;
}
}
}
return id;
}
/**
* 加入缓存
* @return
*/
public void setDictInfo(DictDataInfo dataInfo){
if(dataInfo != null){
Map dictmap = getDictCacheList();
String dictid = dataInfo.getParient_id();
String id = dataInfo.getDict_code();
List<DictDataInfo> dataInfos = (List<DictDataInfo>) dictmap.get(dictid);
if(dataInfos != null){
boolean b = true;
for (int i = 0; i < dataInfos.size(); i++) {
DictDataInfo info = dataInfos.get(i);
if(id.equals(info.getDict_code())){
b = false;
break;
}
}
if(b){
dataInfos.add(dataInfo);
}
LoginCache.remove(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER);
LoginCache.put(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER, dictmap, 3600);
}
}
}

/**
* 修改缓存字典内容
* @param dataInfo
*/
public void modifyDictInfo(DictDataInfo dataInfo){
if(dataInfo != null){
Map dictmap = getDictCacheList();
String dictid = dataInfo.getParient_id();
String id = dataInfo.getDict_code();
List<DictDataInfo> dataInfos = (List<DictDataInfo>) dictmap.get(dictid);
if(dataInfos != null){
boolean b = false;
for (int i = 0; i < dataInfos.size(); i++) {
DictDataInfo info = dataInfos.get(i);
if(id.equals(info.getDict_code())){
dataInfos.remove(i);
b = true;
break;
}
}
if(b){
dataInfos.add(dataInfo);
}
LoginCache.remove(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER);
LoginCache.put(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER, dictmap, 3600);
}
}
}

/**
* 删除缓存字典内容
* @param dataInfo
*/
public void removeDictInfo(String dict_id,String id){
if(dict_id != null && !"".equals(dict_id) && id != null && !"".equals(id)){
Map dictmap = getDictCacheList();
List<DictDataInfo> dataInfos = (List<DictDataInfo>) dictmap.get(dict_id);
if(dataInfos != null){
for (int i = 0; i < dataInfos.size(); i++) {
DictDataInfo info = dataInfos.get(i);
if(id.equals(info.getDict_code())){
dataInfos.remove(i);
break;
}
}
LoginCache.remove(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER);
LoginCache.put(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER, dictmap, 3600);
}
}
}

public static void main(String[] args) {
DictionaryHelper dh = new DictionaryHelper();
DictDataInfo dataInfo = new DictDataInfo();
dataInfo.setParient_id("anticoagulant");
dataInfo.setDict_text("sssssssssssssssssss");
dataInfo.setDict_code("sda");
dh.setDictInfo(dataInfo);

List<DictDataInfo> list = dh.getDictById("anticoagulant");
for (int i = 0; i < list.size(); i++) {
DictDataInfo d = list.get(i);
System.out.println(d.getDict_text());
}

String text = dh.getDictIdByText("anticoagulant", "无肝素");
System.out.println("text:"+text);

String id = dh.getDictTextById("sys_sex", "1");
System.out.println("id:"+id);
}
}

public class LoginCache {
private static CacheManager singleton = CacheManager.getInstance();
/**
* 存入缓存
* @param cacheName 主缓存名称
* @param key 子缓存主键名称
* @param value 需要缓存的数据
* @param timeToIdleSeconds 缓存时间
*/
public static synchronized void put(String cacheName, Object key, Object value, int timeToIdleSeconds){
Cache cache = null;
if(StringHelper.isEmpty(cacheName)){
return;
}
if(key == null){
return;
}
if(!singleton.cacheExists(cacheName)){
singleton.addCache(cacheName);
cache = singleton.getCache(cacheName);
}
cache = singleton.getCache(cacheName);

Element e = new Element(key, value);
e.setTimeToIdle(timeToIdleSeconds);
cache.put(e);

}

/**
* 根据主键缓存获取全部缓存内容
* @param cacheName 主键
* @return
*/
public static synchronized List<Map> getAllUserMessage(String cacheName){
List<Map> userlist = null;
try{
Cache cache = null;
if(StringHelper.isEmpty(cacheName)){
return userlist;
}
if(!singleton.cacheExists(cacheName))
return null;
cache = singleton.getCache(cacheName);
List list = cache.getKeys();
if(list != null && list.size() > 0){
userlist = new ArrayList<Map>();
int s = list.size();
for (int j = 0; j < s; j++) {
String key = (String)list.get(j);
Element e = cache.get(key);
if(e != null){
Map user = (Map)e.getObjectValue();
if(user != null)
userlist.add(user);
}
}

}
}catch(Exception e){
e.printStackTrace();
}
return userlist;
}

/**
* 删除子项缓存
* @param cachename 主键缓存名称
* @param key 子项缓存名称
*/
public static synchronized void remove(String cachename, Object key){
try{
if(StringHelper.isEmpty(cachename)){
return;
}
if(!singleton.cacheExists(cachename)){
return;
}
Cache cache = singleton.getCache(cachename);
cache.remove(key);
}catch(Exception e){
e.printStackTrace();
}
}

/**
* 获取缓存
* @param cachename 主键缓存名称
* @param key 子项缓存名称
* @return
*/
public static Object get(String cachename,Object key){
Object o = null;
if(!singleton.cacheExists(cachename)){
return o;
}
Cache cache = singleton.getCache(cachename);
if(!cache.isKeyInCache(key)){
return o;
}
Element e = cache.get(key);
if(e != null)
o = e.getObjectValue();
return o;
}

/**
* 获取缓存子项
* @param cachename 主键缓存名称
* @return
*/
public static Ehcache getCache(String cachename){
Cache cache = null;
if(!singleton.cacheExists(cachename)){
return cache;
}
cache = singleton.getCache(cachename);
return cache;
}
}

4在项目应用中,调用该类的changeDictValWithBeanList()方法,将数据库中的比如(1,2,3等数字)转化成对应的字典数据

/**
* 测试处理Bean多列字典的问题
* @author DongChao
*
*/
public class DictionaryUtil {

/**
* 依次将beanList中的项目(beanParams)用指定的字典(dictNames)替换
* beanParams 的顺序要和dictNames一致
* @param beanList
* @param beanParams
* @param dictNames
* @return
*/
public static List changeDictValWithBeanList(List beanList,String [] beanParams,String[] dictNames){

DictionaryHelper dh = new DictionaryHelper();

if(beanList!=null && beanList.size()>0){// 入股list为空,无法处理,原样返回list

if(beanParams==null){//如果参数名为空则无法处理,原样返回list
return beanList;
}if(dictNames==null){//如果字典为空则无法处理,原样返回list
return beanList;
}
if(beanParams.length!=dictNames.length){//bean的内容和字典数量不匹配则无法处理,原样返回list
return beanList;
}

HashMap paramsAndDicts = StringArrToHashMap(beanParams,dictNames);

for(int i=0;i<beanList.size();i++){
Object bean = beanList.get(i);
PropertyDescriptor[] props = null;
try {
props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (props != null) {

for(int j=0;j<props.length;j++){
String name = props[j].getName();//bean中的成员变量的名字
//Arrays.sort(beanParams);
String dictName = (String)paramsAndDicts.get(name);

if(dictName!=null && !"".equals(dictName)){

//取出原始值
try {
String oriaValue = props[j].getReadMethod().invoke(bean).toString() ;

//将value转换成字典的文本值
//System.out.println(dictName+"=="+oriaValue);
String dictText=dh.getDictTextById(dictName, oriaValue);
//将转换后的文本写会到Bean中
Method methodSet=props[j].getWriteMethod();
methodSet.invoke(bean, dictText);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}

}
}

return beanList;
}

private static HashMap StringArrToHashMap(String [] beanParams,String[] dictNames){
HashMap map = new HashMap();
for(int i=0;i<beanParams.length;i++){
map.put(beanParams[i], dictNames[i]);
}
return map ;
}

}

5.实际的调用的例子:
DictionaryUtil dt = new DictionaryUtil();
String []beanParams = new String []{"sex","blood_type","rh_blood_type","charge_type","insurance_type","patient_state"};
String []dictNames = new String []{"sys_sex","sys_blood_type","sys_rh_blood_type","sys_charge_type","sys_insurance_type","sys_patient_state"};
list = dt.changeDictValWithBeanList(list, beanParams, dictNames);//获取缓存中对应的数据值
DictionaryHelper dh = new DictionaryHelper();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: