settingbean-orm

setting javabean automatic mapping to database setting table

License

License

Categories

Categories

ORM Data
GroupId

GroupId

com.github.bingoohuang
ArtifactId

ArtifactId

settingbean-orm
Last Version

Last Version

0.0.9
Release Date

Release Date

Type

Type

jar
Description

Description

settingbean-orm
setting javabean automatic mapping to database setting table
Project URL

Project URL

http://github.com/bingoohuang/settingbean-orm
Source Code Management

Source Code Management

http://github.com/bingoohuang/settingbean-orm

Download settingbean-orm

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.bingoohuang/settingbean-orm/ -->
<dependency>
    <groupId>com.github.bingoohuang</groupId>
    <artifactId>settingbean-orm</artifactId>
    <version>0.0.9</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.bingoohuang/settingbean-orm/
implementation 'com.github.bingoohuang:settingbean-orm:0.0.9'
// https://jarcasting.com/artifacts/com.github.bingoohuang/settingbean-orm/
implementation ("com.github.bingoohuang:settingbean-orm:0.0.9")
'com.github.bingoohuang:settingbean-orm:jar:0.0.9'
<dependency org="com.github.bingoohuang" name="settingbean-orm" rev="0.0.9">
  <artifact name="settingbean-orm" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.bingoohuang', module='settingbean-orm', version='0.0.9')
)
libraryDependencies += "com.github.bingoohuang" % "settingbean-orm" % "0.0.9"
[com.github.bingoohuang/settingbean-orm "0.0.9"]

Dependencies

compile (12)

Group / Artifact Type Version
org.jooq : joor-java-8 jar 0.9.9
com.github.bingoohuang : westcache Optional jar 0.0.32
com.github.bingoohuang : java-utils jar 0.0.21
org.apache.commons : commons-lang3 jar 3.7
org.slf4j : slf4j-api jar 1.7.25
org.ow2.asm : asm jar 6.1.1
joda-time : joda-time jar 2.10
com.github.bingoohuang : eql jar 0.1.2
redis.clients : jedis jar 2.9.0
com.alibaba : fastjson jar 1.2.47
com.github.bingoohuang : westid jar 0.0.2
org.springframework : spring-context-support Optional jar 5.0.7.RELEASE

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.18.2

test (6)

Group / Artifact Type Version
org.springframework : spring-test jar 5.0.7.RELEASE
junit : junit jar 4.12
com.google.truth : truth jar 0.42
it.ozimov : embedded-redis jar 0.7.2
com.h2database : h2 jar 1.4.197
ch.qos.logback : logback-classic jar 1.2.3

Project Modules

There are no modules declared in this project.

settingbean-orm

Map setting javabean fields to database setting table rows.
Build Status Quality Gate Coverage Status Maven Central License

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.

image

Programming

  1. 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");  // 场馆可选主题色列表
    }
  2. 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());
            }
        }
    }
    
  3. Define the data access object

    @Eqler
    public interface XyzBeanDao extends SettingBeanDao {
    }
  4. 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
            }
    
        }
    }

Versions

Version
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1