{"id":21915741,"url":"https://github.com/admin4j/admin4j-dict","last_synced_at":"2026-02-10T01:33:05.304Z","repository":{"id":204378565,"uuid":"710208860","full_name":"admin4j/admin4j-dict","owner":"admin4j","description":"字典转化","archived":false,"fork":false,"pushed_at":"2024-05-10T10:14:40.000Z","size":89,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-02T13:43:03.210Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/admin4j.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-10-26T08:36:17.000Z","updated_at":"2024-05-10T10:14:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"acb7f38f-b6c9-45d4-b0e7-0321f530c995","html_url":"https://github.com/admin4j/admin4j-dict","commit_stats":null,"previous_names":["admin4j/admin4j-dict"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/admin4j/admin4j-dict","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/admin4j%2Fadmin4j-dict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/admin4j%2Fadmin4j-dict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/admin4j%2Fadmin4j-dict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/admin4j%2Fadmin4j-dict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/admin4j","download_url":"https://codeload.github.com/admin4j/admin4j-dict/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/admin4j%2Fadmin4j-dict/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29288467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-09T21:57:15.303Z","status":"ssl_error","status_checked_at":"2026-02-09T21:57:11.537Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T19:13:58.100Z","updated_at":"2026-02-10T01:33:05.250Z","avatar_url":"https://github.com/admin4j.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# admin4j-dict 字典转化\n\n在实际开发中，列表查询比较烦的是需要查询他的关联字段的名称，比如客户列表查询，需要显示可的所属用户的用户名，而我们再表中已绑定了用户ID。\n\n- 原始json\n\n```json\n[\n  {\n    \"consumerId\": 1,\n    \"consumerName\": \"刘备\",\n    \"userId\": \"100\"\n  }\n]\n```\n\n- 处理之后的json\n\n```json\n[\n  {\n    \"consumerId\": 1,\n    \"consumerName\": \"刘备\",\n    \"userId\": \"100\",\n    \"userName\": \"刘邦\"\n  }\n]\n```\n\n解决方法如下：\n\n1. 添加冗余用户名称字段\n2. 使用join 查询\n3. 先查列表后单独查名称，再进行赋值\n\n第一种方案是最简单的，但他缺点是\n\n1. 冗余字段要求不可变， 如果冗余字段原始值发生变化，那他需要一定的机制，去更新。如用户名变化，同时也要更新其他表的冗余字段\n2. 多余需要显示字段较多的情况下，需要冗余多个字段。比如上方的 `userId`,`createBy`,`updateBy`的等\n\n第二种方案缺点是\n\n1. 性能不高。当表比较大，或者连表比较多是，表现出性能不佳。\n2. 需要显示多个名称时。比如上方的 `userId`,`createBy`,`updateBy`的等，需要连多张表\n\n第三种方案缺点是\n\n1. 操作复杂\n\n# admin4j-dict\n\n通过使用注解的方式，实现第三种方式。避免效率冗余和join效率的各大问题\n\n# Usage\n\n导入 pom\n\n```xml\n\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.admin4j.dict\u003c/groupId\u003e\n    \u003cartifactId\u003edict-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e0.8.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n```\n@Data\npublic class Consumer {\n\n    private Integer consumerId;\n\n    private String consumerName;\n    /**\n     * 归属于哪个用户\n     */\n    @Dict(dictStrategy = SqlDictProvider.DICT_STRATEGY, dictType = \"user\")\n    @DictSql(labelField = \"user_name\", codeField = \"user_id\", whereSql = \"del_flag = 0\")\n    @DictJson\n    @ExcelProperty(converter = EnhanceConverter.class)\n    private Integer userId;\n    /**\n     * 由哪个用户创建\n     */\n    @Dict(dictStrategy = SqlDictProvider.DICT_STRATEGY, dictType = \"user\")\n    @DictSql(labelField = \"user_name\", codeField = \"user_id\", whereSql = \"del_flag = 0\")\n    @DictJson(fieldName = \"createByName\")\n    @ExcelProperty(converter = EnhanceConverter.class)\n    private Integer createBy;\n    /**\n     * 由哪个用户更新\n     */\n    @Dict(dictStrategy = SqlDictProvider.DICT_STRATEGY, dictType = \"user\")\n    @DictSql(labelField = \"user_name\", codeField = \"user_id\", whereSql = \"del_flag = 0\")\n    @DictJson\n    @ExcelProperty(converter = EnhanceConverter.class)\n    private Integer updateBy;\n}\n```\n\n具体示例 查看 [`dict-spring-boot-example`](https://github.com/admin4j/admin4j-dict/tree/master/dict-spring-boot-example)\n\n## 配置方式一： 纯注解\n\n```java\n@Dict(dictStrategy = SqlDictProvider.DICT_STRATEGY, dictType = \"user\")\n@DictSql(labelField = \"user_name\", codeField = \"user_id\", whereSql = \"del_flag = 0\")\n@DictJson\nprivate Integer userId;\n```\n\n### @Dict 定义字典策略\n\n- dictStrategy 字典策略 当前为 sql\n- dictType 字典类型 指 表名 或者表code\n\n### @DictSql 定义sql 如何读取\n\n- labelField 显示的字段名\n- codeField 字典值字段名\n- whereSql 查询条件\n\n### @DictJson 定义Jackson 的 json 序列化\n\n## 配置方式二： yaml 配置\n\n`@DictSql` 配置有点繁琐和重复，使用 yaml 可以省略`@DictSql` 配置\n\n```yaml\nadmin4j:\n  dict:\n    sql-dict-map:\n      user:\n        codeFiled: user_id\n        labelFiled: user_name\n        whereSql: del_flag = 0\n```\n\n## 配置方式三： 自定义配置\n\n实现 `SqlDictService` 接口实现自定义的读取sql,参考 `PropertiesSqlDictService`\n\n## 开始批量字典查询\n\n默认情况下 字典查询都是按照查询出来的数组一个个查询，如下\n\n```\nc.a.d.e.mapper.ConsumerMapper.list       : ==\u003e  Preparing: select * from consumer;\nc.a.d.e.mapper.ConsumerMapper.list       : ==\u003e Parameters: \nc.a.d.e.mapper.ConsumerMapper.list       : \u003c==      Total: 3\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 100(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 101(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 102(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 101(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 100(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 102(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e  Preparing: SELECT user_name as `label` from user where user_id = ? AND del_flag = 0 LIMIT 1\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : ==\u003e Parameters: 100(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabel    : \u003c==      Total: 1\n```\n\n### 参考 [DictResponseBodyAdvice](https://github.com/admin4j/admin4j-dict/blob/master/dict-spring-boot-example/src/main/java/com/admin4j/dict/example/DictResponseBodyAdvice.java) 开始缓存\n\n开始缓存之后，会变得使用批量查询，提供查询效率\n\n```\nc.a.d.e.mapper.ConsumerMapper.list       : ==\u003e  Preparing: select * from consumer;\nc.a.d.e.mapper.ConsumerMapper.list       : ==\u003e Parameters: \nc.a.d.e.mapper.ConsumerMapper.list       : \u003c==      Total: 3\nc.a.d.p.s.i.m.SqlDictMapper.dictLabels   : ==\u003e  Preparing: select user_name as `label`, user_id as `code` from user where user_id in ( ? , ? , ? ) AND del_flag = 0\nc.a.d.p.s.i.m.SqlDictMapper.dictLabels   : ==\u003e Parameters: 100(Integer), 101(Integer), 102(Integer)\nc.a.d.p.s.i.m.SqlDictMapper.dictLabels   : \u003c==      Total: 3\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadmin4j%2Fadmin4j-dict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadmin4j%2Fadmin4j-dict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadmin4j%2Fadmin4j-dict/lists"}