settingbean-orm
Map setting javabean fields to database setting table rows.
Example
Create Table SQL.
The table name can be customized, but the fields are required fixed as the following:
-- mysql
DROP TABLE IF EXISTS `t_setting`;
CREATE TABLE `t_setting` (
`NAME` varchar(100) NOT NULL COMMENT '设置名',
`VALUE` varchar(100) NULL COMMENT '设置值',
`TITLE` varchar(100) NOT NULL COMMENT '设置标题,用于页面展示',
`EDITABLE` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否可以通过页面编辑',
`SPEC` varchar(100) DEFAULT NULL COMMENT '取值校验规则,目前支持@Digits @Min(1) @Max(100) @Regex等',
`UPDATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`CREATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`NAME`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '设置表';
Demo table rows.
Programming
-
Define Javabean for setting.
The bean's each field represent a setting for the system which can be mapping to the setting table row.
public class XyzSetting { private int maxSubscribesPerMember; private boolean allowQueuing; @SettingField(name = "CANCEL_SUBSCRIPTION_MIN_BEFORE_HOURS", format = SettingValueFormat.HumanTimeDuration, timeUnit = TimeUnit.MINUTES) private int cancelSubscriptionMinBeforeMinutes = 0; // 取消预约最少提前的小时数 @SettingField(ignored = true) private String cancelSubscriptionMinBeforeReadable; // 取消预约最少提前的小时数 @SettingField(format = SettingValueFormat.SimpleList) private List<String> themes = ImmutableList.of("#333"); // 场馆可选主题色列表 }
-
Define the setting service
@Component public class XyzSettingService extends SettingServiceable<XyzSetting> { @Autowired XyzBeanDao xyzBeanDao; @Autowired XyzSettingServiceClearCache xyzSettingServiceClearCache; @Override public Class<XyzSetting> getBeanClass() { return XyzSetting.class; } @Override public String getSettingTable() { return "X_SETTING"; } @Override public SettingBeanDao getSettingBeanDao() { return xyzBeanDao; } @Override public void clearSettingsCache() { // 不能从自身调用,否则方法代理不起作用,所以需要借道另外的类来完成 xyzSettingServiceClearCache.clearSettingsCache(); } @WestCacheable(manager = "redis") @Override public XyzSetting getSettingBean() { return getUncachedSettingBean(); } // 本类纯粹是为了完成清除缓存功能 @Component static class XyzSettingServiceClearCache { @Autowired XyzSettingService xyzSettingService; void clearSettingsCache() { WestCacheConnector.clearCache(() -> xyzSettingService.getSettingBean()); } } }
-
Define the data access object
@Eqler public interface XyzBeanDao extends SettingBeanDao { }
-
Use the setting bean where required
@Service public class MyService { @Autowired XyzSettingService xyzSettingService; public void addQueueing() { val settings = xyzSettingService.getSettingBean(); if (settings.isAllowQueuing()) { // do queueing hrere } } }