数据字典的好处很多比如:
1、可以减少使用表,来专门记录类型。
2、类型使用key检索,或者报表统计分析,在一定程度上相比汉字来讲,效率好得多。
3、使用缓存的数据字典、也可以减少不少的io操作。
等等、、、、
首先,库表设计就智者见智了、不多说、爱怎么设计就怎么设计。
完整的数据字典设计 ,需要
1、生成select 自定义标签。
2、list页面,或者get页面, 一个key转 value的标签
使用自定义标签,搭配上缓存的数据字典是最方便、最完美的解决办法,
接下来,就直接贴代码了。
一、数据字典缓存配置:
1、数据字典缓存监听器(在web容器启动成功的时候、进行缓存)
web.xml
1 <listener> 2 <description>初始化数据字典</description> 3 <listener-class>com.hotent.core.web.listener.DictionaryCacheListener</listener-class> 4 </listener>
2、DictionaryCacheListener
1 package com.*****.core.web.listener; 2 import javax.servlet.ServletContextEvent; 4 import org.springframework.web.context.WebApplicationContext; 5 import org.springframework.web.context.support.WebApplicationContextUtils; 7 import com.*****.platform.service.system.DictionaryService; 8 9 public class DictionaryCacheListener implements javax.servlet.ServletContextListener { 10 11 @Override 12 public void contextDestroyed(ServletContextEvent arg0) { 14 }
16 @Override 17 public void contextInitialized(ServletContextEvent arg0) { 18 19 System.out.println("++++++++++++++++++ 数据字典已缓存 +++++++++++++++++++++"); 20 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()); 21 DictionaryService dc = (DictionaryService) webApplicationContext.getBean("dictionaryService"); 22 dc.getCacheDic(); // 调用数据字典Manager的一个方法来缓存 24 } 25 26 }
3、保存缓存数据字典的BO //(也可以放在平台缓存的BO里面,那共用的缓存设计要考虑线程安全了,简单起见这么搞。)
import java.util.HashMap; import java.util.List; import java.util.Map; import com.hotent.platform.model.system.Dictionary; import com.hotent.platform.model.system.GlobalType; /** * 缓存数据字典 * @author miao * */ public class CacheDict { /** * 所有的数据字典类型 */ public static Map<String,GlobalType> allTypes = new HashMap<String, GlobalType>(); /** * 所有类型,对应的数据字典项 */ public static Map<String, List<Dictionary>> dictList = new HashMap<String,List<Dictionary>>(); /** * 类型与字典项 由List 转成Map(key,value) */ public static Map<String, Map<Long,String>> dictMap = new HashMap<String,Map<Long,String>>(); }