mybatisカスタムプラグインデータ辞書の処理


データ辞書テーブルプラグインgithubアドレス
1、プラグインの機能説明:
1、            ,              ,             。             。
2、    ,             ,          "MAN",       , “MAN”  “ ”。     ,        ,    ,    。        ,       ,      ,    
3、         ,     ,    ,           

2、統合手順
2.1、mybatisプラグインを登録する
2.1.1.pom導入依存パッケージ
このプロジェクトをローカルにパッケージ化し、使用するプロジェクトに導入します.

    org.wit.ctw.plugin
    spring-dictionary
    1.0.0-RELEASE


2.1.2、注釈説明
@Inherited
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Dictionary {
    String table() default "";
    String property() default "";
    String column() default "";
    String value() default "";
    String[] conditions() default {""};
}

2.1.2.1、table
         ,   ,          ,    ,    。    ,       (        )

2.1.2.2、property
        ,   sex=MAN  ,   ,       ,   sex= ,        ,                  ,
 property=sexLabel,   sex=MAN,sexLabel= , sexLabel            ,     

2.1.2.3、column、value
              

2.1.2.4、conditions
  ,        ,             ,               ,         ,          。
    : ON===>   (type=user_status)
    : ON===>   (type=friend_status)
        ,                  ,      condition={"type = user_status"},       ,    and  

2.1.3、Interceptorプラグインの登録
2.1.3.1、構成表sys_config構造の説明
CREATE TABLE `sys_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `value` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `type` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `sort` int(11) DEFAULT NULL,
  `parent_id` int(11) DEFAULT '0',
  `app_id` varchar(255) COLLATE utf8_bin DEFAULT '',
  `create_user_id` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `update_user_id` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `remarks` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `del_flag` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

2.1.3.2、ユーザーテーブルapp_user構造の説明
CREATE TABLE `app_user` (
  `id` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `create_time` datetime DEFAULT NULL
)

2.1.3.3、プラグインをspringコンテナに登録する
column:DatabaseTableのcolumnは注記で使用されるすべての列です.一般的に辞書表にはkey,valueしか必要ありません.複数のフィールドで一意の辞書項目を決定する必要がある場合は、複数のcolumnを設定する必要があります.
@Bean
public DictionaryInterceptor dictionaryInterceptor(DataSource dataSource) {
    // config    
    DatabaseTable config = new DatabaseTable();
    String[] configColumn = new String[]{"label", "value", "type"};
    String[] configCondition = new String[]{"1 = 1"};
    String configTableName = "sys_config";
    config.setColumn(configColumn);
    config.setConditions(configCondition);
    config.setTableName(configTableName);
    // user    
    DatabaseTable user = new DatabaseTable();
    String[] userColumn = new String[]{"id", "name"};
    String userTableName = "app_user";
    user.setColumn(userColumn);
    user.setTableName(userTableName);
    DatabaseLoader dbLoader = new DatabaseLoader(dataSource);
    dbLoader.addDictionaryTable(config);
    dbLoader.addDictionaryTable(user);
    DictionaryInterceptor interceptor = new DictionaryInterceptor(dbLoader);
    return interceptor;
}

3、辞書プラグインの使用
3.1、注釈タグ属性
使用注記後1、createUserId、updateUserIdは元のユーザidからユーザ名2、stateは元の状態「ON」から「進行中」に変更
public class Habit {

    @Dictionary(table ="app_user", column = "name", value = "id")
    private String createUserId;
    
    /**
     *        ,         ,     
     */
    @Dictionary(table ="app_user", column = "name", value = "id")
    private String updateUserId;

    private String stateLabel;

    // table   ,          ,property        ,column      column label, value      value value, conditions        
    //@Dictionary(table ="sys_config", property = "stateLabel", column = "label", value = "value", conditions = {"type = habit_state"})
    @Dictionary(property = "stateLabel")
    private String state;
}

3.2、プラグインの使用効果
プラグインを使用する前
{
	"flag": true,
	"data": {
		"id": "d99839ee821511ea94d354ee75dd4fca",
		"habitName": "",
		"startDate": "2020-04-19",
		"endDate": "2020-05-10",
		"clockStartTime": "22:00",
		"clockEndTime": "23:30",
		"publishTime": "2020-04-19 16:14:33",
		"createTime": "2020-04-19 16:14:33",
		"createUserId": "1",
		"updateTime": "2020-04-19 16:14:33",
		"updateUserId": "1",
		"stateLabel": null,
		"state": "ON",
		"records": []
	},
	"message": "      "
}

プラグイン使用後1
  
@Dictionary(table ="app_user", column = "name", value = "id")
private String createUserId;

@Dictionary(table ="app_user", column = "name", value = "id")
private String updateUserId;

//    ,          ,        ,      column label,       value value,         
//@Dictionary(table ="sys_config", property = "stateLabel", column = "label", value = "value", conditions = {"type = habit_state"})
@Dictionary(property = "stateLabel")
private String state;
{
	"flag": true,
	"data": {
		"id": "d99839ee821511ea94d354ee75dd4fca",
		"habitName": "",
		"startDate": "2020-04-19",
		"endDate": "2020-05-10",
		"clockStartTime": "22:00",
		"clockEndTime": "23:30",
		"publishTime": "2020-04-19 16:14:33",
		"createTime": "2020-04-19 16:14:33",
		"createUserId": "vax",
		"updateTime": "2020-04-19 16:14:33",
		"updateUserId": "vax",
		"stateLabel": "   ",
		"state": "ON",
		"records": []
	},
	"message": "      "
}

プラグイン使用後2
@Dictionary(table ="app_user", column = "name", value = "id")
private String createUserId;

@Dictionary(table ="app_user", column = "name", value = "id")
private String updateUserId;

@Dictionary
private String state;
{
	"flag": true,
	"data": {
		"id": "d99839ee821511ea94d354ee75dd4fca",
		"habitName": "    ",
		"startDate": "2020-04-19",
		"endDate": "2020-05-10",
		"clockStartTime": "22:00",
		"clockEndTime": "23:30",
		"publishTime": "2020-04-19 16:14:33",
		"createTime": "2020-04-19 16:14:33",
		"createUserId": "vax",
		"updateTime": "2020-04-19 16:14:33",
		"updateUserId": "vax",
		"stateLabel": null,
		"state": "   ",
		"records": []
	},
	"message": "      "
}