web项目设置全局变量
2015-12-27 16:57
281 查看
在某些场景中,运营人员需要自定义某些值来控制一些业务。而那些值一般活动于整个项目。
比如动态设置过滤值去过滤评分低的内容,
动态设置是否加载某平台的资源,
动态设置等等
下面内容是在公司项目中遇到,总结记录方便以后查询和帮助需要的朋友。
转载请注明来源:http://blog.csdn.net/qq_19558705
更多干货等你来拿 http://www.itit123.cn/
第一步:创建一个表:配置表configuration;和数据字典很相似。
第二步:创建实体类
因为value在表中对应类型是varchat;然而实际业务中还可能是整形或者是浮点型。
第三步:Dao层,使用的是springMVC框架
按照springMVC的规范,findByName便可以做到通过name找到对应的Configuration
第四步:Service层
第五步:在web项目中全局调用
以上是笔者在业务中遇到的情景,不一定满足所有,仅供参考,如有不懂和建议可以留言。博客原文来源:http://blog.csdn.net/qq_19558705 转载请注明来源
比如动态设置过滤值去过滤评分低的内容,
动态设置是否加载某平台的资源,
动态设置等等
下面内容是在公司项目中遇到,总结记录方便以后查询和帮助需要的朋友。
转载请注明来源:http://blog.csdn.net/qq_19558705
更多干货等你来拿 http://www.itit123.cn/
第一步:创建一个表:配置表configuration;和数据字典很相似。
DROP TABLE IF EXISTS `configuration`; CREATE TABLE `configuration` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8_bin NOT NULL, `value` varchar(100) COLLATE utf8_bin NOT NULL, `note` varchar(255) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; INSERT INTO `configuration` VALUES ('1', 'defaultNum', '50000', '运营人员可自定义的值');
第二步:创建实体类
package net.wecheer.entity; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Transient; import cn.weidrive.qc.annotation.QueryField; @Entity @Table(name = "configuration") public class Configuration extends IdEntity { private String name; // 配置名 private String value; // 配置值 private String note; // 注释说明 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Transient public int getIntValue(int defaultValue) { try { int iValue = Integer.parseInt(value); return iValue; } catch (NumberFormatException e) { return defaultValue; } } @Transient public float getFloatValue(float defaultValue) { try { float fValue = Float.parseFloat(value); return fValue; } catch (NumberFormatException e) { return defaultValue; } } }
因为value在表中对应类型是varchat;然而实际业务中还可能是整形或者是浮点型。
第三步:Dao层,使用的是springMVC框架
package net.wecheer.repository; import net.wecheer.entity.Configuration; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.PagingAndSortingRepository; public interface ConfigurationDao extends PagingAndSortingRepository<Configuration, Long>, JpaSpecificationExecutor<Configuration> { Configuration findByName(String name); }
按照springMVC的规范,findByName便可以做到通过name找到对应的Configuration
第四步:Service层
// 以下三个函数用于方便取得配置值的函数,分别对应字符串、整形和浮点型 public String getConfigValueByName(String name, String defaultValue) { Configuration config = configDao.findByName(name); if (null == config) return defaultValue; else return config.getValue(); } public int getConfigValueByName(String name, int defaultValue) { Configuration config = configDao.findByName(name); if (null == config) return defaultValue; else { try { int iValue = Integer.parseInt(config.getValue()); return iValue; } catch (NumberFormatException e) { return defaultValue; } } } public float getConfigValueByName(String name, float defaultValue) { Configuration config = configDao.findByName(name); if (null == config) return defaultValue; else { try { float fValue = Float.parseFloat(config.getValue()); return fValue; } catch (NumberFormatException e) { return defaultValue; } } }如果你想要long型和double型可以自行添加,注意这里的一个技巧,缺省的时候有一个默认值。
第五步:在web项目中全局调用
@Autowired private ConfigurationService configService; int defaultNum= configService.getConfigValueByName("defaultNum", 50000);
以上是笔者在业务中遇到的情景,不一定满足所有,仅供参考,如有不懂和建议可以留言。博客原文来源:http://blog.csdn.net/qq_19558705 转载请注明来源