前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >spring获取AliasFor增强的注解

spring获取AliasFor增强的注解

作者头像
阿超
发布于 2023-06-23 06:24:44
发布于 2023-06-23 06:24:44
23500
代码可运行
举报
文章被收录于专栏:快乐阿超快乐阿超
运行总次数:0
代码可运行

无论何时,别让你自己卷进去反对他人。——歌德

此处是关于issuehttps://gitee.com/dromara/stream-query/issues/I7BSNV

这里使用的一个自定义的@Table注解+@AliasFor来增强@TableName

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package issue.org.dromara.streamquery.gitee.issue17BSNV;

import com.baomidou.mybatisplus.annotation.TableName;
import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@TableName
public @interface Table {
  @AliasFor(annotation = TableName.class, attribute = "value")
  String value() default "";
}

然后是

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package issue.org.dromara.streamquery.gitee.issue17BSNV;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import org.dromara.streamquery.stream.plugin.mybatisplus.engine.mapper.IGenerateMapper;

import java.time.LocalDateTime;

@Data
@Table(value = "user_info")
public class UserInfoWithTableAnnotation implements IGenerateMapper {
  private static final long serialVersionUID = -7219188882388819210L;

  @TableId(value = "id", type = IdType.AUTO)
  private Long id;

  private String name;
  private Integer age;
  private String email;

  @TableLogic(value = "'2001-01-01 00:00:00'", delval = "NOW()")
  private LocalDateTime gmtDeleted;
}

使用时:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package issue.org.dromara.streamquery.gitee.issue17BSNV;

import org.dromara.streamquery.stream.core.collection.Lists;
import org.dromara.streamquery.stream.plugin.mybatisplus.Database;
import org.dromara.streamquery.stream.plugin.mybatisplus.annotation.AbstractMybatisPlusTestApplication;
import org.dromara.streamquery.stream.plugin.mybatisplus.engine.annotation.EnableMybatisPlusPlugin;
import org.dromara.streamquery.stream.plugin.mybatisplus.engine.mapper.IMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import java.util.Arrays;
import java.util.List;

/**
 * @author VampireAchao
 * @since 2023/6/8
 */
@EnableAutoConfiguration
@EnableMybatisPlusPlugin
class RevisionTest extends AbstractMybatisPlusTestApplication {

  @Test
  void testExecute() {
    UserInfoWithTableAnnotation entity = new UserInfoWithTableAnnotation();
    entity.setName("cat");
    entity.setAge(20);
    entity.setEmail("myEmail");
    UserInfoWithTableAnnotation userInfo = new UserInfoWithTableAnnotation();
    userInfo.setName("ruben");
    List<UserInfoWithTableAnnotation> list = Arrays.asList(userInfo, entity);
    long effectRows = Database.execute(UserInfoWithTableAnnotation.class,
            (IMapper<UserInfoWithTableAnnotation> m) -> m.saveOneSql(list));
    Assertions.assertEquals(2, effectRows);
    Assertions.assertEquals(7, Database.count(UserInfoWithTableAnnotation.class));

    Assertions.assertEquals(
            0L, Database.execute(UserInfoWithTableAnnotation.class,
                    (IMapper<UserInfoWithTableAnnotation> m) -> m.saveOneSql(Lists.empty())));
  }
}

此时发现并没有映射上,这里找表名没有找到。。。

于是我进行了处理

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
String originalTableName = tableInfo.getTableName();
  Annotation[] annotations = tableInfo.getEntityType().getAnnotations();
  for (Annotation annotation : annotations) {
    if (annotation.annotationType().isAnnotationPresent(TableName.class)) {
      Map<String, Object> annotationAttributes =
          AnnotationUtils.getAnnotationAttributes(annotation);
      TableName synthesizedTableName =
          AnnotationUtils.synthesizeAnnotation(
              annotationAttributes, TableName.class, tableInfo.getEntityType());
      String tableNamePropertyName = LambdaHelper.getPropertyName(TableInfo::getTableName);
      String tableNameValue = synthesizedTableName.value();
      ReflectHelper.setFieldValue(tableInfo, tableNamePropertyName, tableNameValue);
      Map<String, TableInfo> tableNameInfoCache =
          ((SerSupp<Map<String, TableInfo>>)
                  () ->
                      SerFunc.<Object, Map<String, TableInfo>>cast()
                          .apply(
                              ReflectHelper.accessible(
                                      TableInfoHelper.class.getDeclaredField(
                                          "TABLE_NAME_INFO_CACHE"))
                                  .get(null)))
              .get();
      tableNameInfoCache.remove(originalTableName);
      tableNameInfoCache.put(tableNameValue, tableInfo);
      break;
    }

完整代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dromara.streamquery.stream.plugin.mybatisplus.engine.handler;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.handlers.PostInitTableInfoHandler;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
import org.dromara.streamquery.stream.core.lambda.LambdaHelper;
import org.dromara.streamquery.stream.core.lambda.function.SerFunc;
import org.dromara.streamquery.stream.core.lambda.function.SerSupp;
import org.dromara.streamquery.stream.core.reflect.ReflectHelper;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * @author VampireAchao
 * @since 2023/3/20 18:10
 */
public class JsonPostInitTableInfoHandler implements PostInitTableInfoHandler {

  /**
   * 参与 TableInfo 初始化
   *
   * @param tableInfo TableInfo
   * @param configuration Configuration
   */
  @Override
  public void postTableInfo(TableInfo tableInfo, Configuration configuration) {
    PostInitTableInfoHandler.super.postTableInfo(tableInfo, configuration);
    String originalTableName = tableInfo.getTableName();
    Annotation[] annotations = tableInfo.getEntityType().getAnnotations();
    for (Annotation annotation : annotations) {
      if (annotation.annotationType().isAnnotationPresent(TableName.class)) {
        Map<String, Object> annotationAttributes =
            AnnotationUtils.getAnnotationAttributes(annotation);
        TableName synthesizedTableName =
            AnnotationUtils.synthesizeAnnotation(
                annotationAttributes, TableName.class, tableInfo.getEntityType());
        String tableNamePropertyName = LambdaHelper.getPropertyName(TableInfo::getTableName);
        String tableNameValue = synthesizedTableName.value();
        ReflectHelper.setFieldValue(tableInfo, tableNamePropertyName, tableNameValue);
        Map<String, TableInfo> tableNameInfoCache =
            ((SerSupp<Map<String, TableInfo>>)
                    () ->
                        SerFunc.<Object, Map<String, TableInfo>>cast()
                            .apply(
                                ReflectHelper.accessible(
                                        TableInfoHelper.class.getDeclaredField(
                                            "TABLE_NAME_INFO_CACHE"))
                                    .get(null)))
                .get();
        tableNameInfoCache.remove(originalTableName);
        tableNameInfoCache.put(tableNameValue, tableInfo);
        break;
      }
    }

    for (TableFieldInfo fieldInfo : tableInfo.getFieldList()) {
      if (fieldInfo.getTypeHandler() == null) {
        continue;
      }
      if (tableInfo.getResultMap() == null) {
        return;
      }
      ResultMap resultMap = configuration.getResultMap(tableInfo.getResultMap());
      for (ResultMapping resultMapping : resultMap.getResultMappings()) {
        TypeHandler<?> handler = resultMapping.getTypeHandler();
        if (handler instanceof AbstractJsonFieldHandler) {
          AbstractJsonFieldHandler<?> typeHandler = (AbstractJsonFieldHandler<?>) handler;
          typeHandler.setTableInfo(tableInfo);
          typeHandler.setFieldInfo(fieldInfo);
        }
      }
    }
  }
}

然后通过AnnotationUtils.*synthesizeAnnotation*解决了这个问题

完整代码:

https://gitee.com/dromara/stream-query/commit/f4c15fdca66c5e6037644b0888e2b756eb5db25a

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
碎片化学习前端知识
针对对象:前端初学者,初级程序员 前言 既要完成工作又想在工作中得到提升,初期的时候进步很快。但是慢慢的就会进入舒适区,怎么才能不在舒适区中陷入泥潭。 想学习,但是时间不够,时间太散,项目一个接一个,而且做的都是重复复用的工作,没时间造轮子,只有一个一个的读人家的 api 或者 google 解决问题,进而利用碎片化的时间进行一定的学习和补足自己啦? 作为一个前端的初学者我一直有这样的疑问和困惑,怎么能在这种环境下提高自己,怎么利用碎片化的时间学习。 要有个目的 学习知识的目的就是赚钱,给家人和自己带
西南_张家辉
2021/02/02
3990
这3个原则可以提高你的工作效率
做一件事情之前,最重要的是明白这件事情对你的意义有多大,这样你才有动力和意愿去付诸实践。这部分我们会聊聊为什么管理好工作任务,让你可以超过同龄人。
猴子数据分析
2022/12/12
3560
这3个原则可以提高你的工作效率
如何长时间高效学习?
熟悉我的人,知道我同时可以干很多事情。所以,经常会有人问我,有什么好的习惯、高效的学习方法,可以提高效率?
猴子数据分析
2020/12/02
7060
碎片化、干货、速成…这类流行词正在让你慢慢变傻
作者 CDA 数据分析师 生活在这个快捷的时代,很多人都热衷于“快餐式”生活。 于是“碎片化”“干货”“速成”“公开课”等逐渐成为流行热词。 而碎片化学习的最大危害是让人们把“知道”当做“懂得”。 网上有个段子关于这群热衷干货喜欢走捷径的人: “如果你每天还在看耶鲁公开课,上3W咖啡听创业讲座,知乎果壳关注无数,36氪每日必读,对马云的创业史了如指掌,对张小龙的贪嗔痴如数家珍,喜欢罗振宇胜过乔布斯,逢人便谈互联网思维……那你应该还在每天挤地铁。” 学习本质是获取信息,知识系统全面 在没有一个整体框架
CDA数据分析师
2018/02/26
7890
碎片化、干货、速成…这类流行词正在让你慢慢变傻
好书 | 《碎片化时代:重新定义互联网+商业新常态 》
书名:《碎片化时代:重新定义互联网+商业新常态 》 原书名:The Great Fragmentation: And Why the Future of All Business is Small 著
CDA数据分析师
2018/02/23
1.1K0
好书 | 《碎片化时代:重新定义互联网+商业新常态 》
如何系统学习数据分析?
很多学习数据分析的同学也都有这样一种困惑“为什么学了那么多工具,还是不会数据分析?”,原因无外乎两个,一是只学到了碎片的知识,没有建立知识之间的连接,无法形式自己的知识体系,二是缺乏实践,导致无法形成
小莹莹
2018/04/23
8280
如何系统学习数据分析?
001 | 如何高效学习
就像建一栋大厦一样,需要先打地基,地基打得好,大厦才够稳。在进阶全栈的路上,同样也需要先打好地基,所以我会将基础软技能板块排在专栏提纲的最前面。先帮你们打好基础,后面的学习才会事半功倍。而基础中的基础,就是学习力,所以第一个要讲的,就是如何提高大家的学习力。另外,我会在后面的内容中,给你们提供不断练习基础技能的机会,帮助你们不断在实践中提高自己的各项技能。
Keegan小钢
2018/08/10
5350
如何高效读懂一本书
(2)特别交代作者所在时代的特点、弊病,有针对性地叙述作者的经历,总结作者通过此事是战胜困难还是被打败,然后分析造成最后结局的关键要素,思考解决问题的方式、手段是什么
yeedomliu
2021/09/08
7170
优势资本-吴克忠:通证颠覆资本下,产业重构的区块链思维
所以我一直说我们做股权投资是十年一遇的机会,并购是30年一遇的机会,中国原来真正的并购从来没出现过,中国现在的并购都是假并购,都是炒市值的并购,都是有套利的空间。我15倍收的到二级市场就是30倍,以对赌形式,三年以后,如果对赌输了,你就要赔我。从来没有真正产业的,所以都是假并购。中国还没有开始,所以这是30年一遇的机会。中国100年一遇的机会是互联网带来的机会。而区块链带来的机会,我觉得是500年一遇的机会,因为他改变的不是生产力,它改变的是生产关系。
辉哥
2018/08/10
5220
优势资本-吴克忠:通证颠覆资本下,产业重构的区块链思维
微信小程序凭什么能成为企业、商家流量变现竞争新宠?
PC互联网时代的商业模式是通过入口级产品获取用户,把控网络流量,最后通过流量变现来赢利。
场景录小程序
2018/07/27
4040
微信小程序凭什么能成为企业、商家流量变现竞争新宠?
微信小程序凭什么能成为企业、商家流量变现竞争新宠?
PC互联网时代的商业模式是通过入口级产品获取用户,把控网络流量,最后通过流量变现来赢利。
速成应用小程序开发平台
2018/07/19
4980
金融科技的碎片化思考(上)
从事金融科技行业已久,也一直想写写金融科技相关的文字,又惶恐间,深觉如此大的话题hold不住而贻笑大方。偶然翻开束之高阁多年的《蚂蚁金服-从支付宝到新金融生态圈》,惊喜之余亦将自己碎片化的那点浅识愚见串联起不少。行文仓促,些许是经历,些许是总结,些许是念头,唯恐扭头就忘,权当流水记账给自己看也好。
曲水流觞
2020/07/13
5600
金融科技的碎片化思考(上)
高效学习的 36 种思维
2008 年,我正在读研究生,再过一年即将毕业,那时实践能力比较弱,对未来很迷茫,不知道自己该干什么。
IT阅读排行榜
2019/11/22
7230
高效学习的 36 种思维
Keep不甘做“工具人”
后半年来,在路边做游泳、健身、瑜伽推广的人员越来越多,几乎在每一个人流量较大的街区,都会见到很多这些在线下推广健身业务的人。这也从一个侧面表现出,在经历了疫情影响的阵痛期后,健身行业也逐渐回到了原来的发展轨道上。
刘旷
2021/01/18
2900
如何避免一步步走向平庸?
没钱没资源,想做点事的人 怎么走向阶段性成功? 知识星球首著《绝非偶然》 给出了 21 种接地气的方法论 为庆祝《绝非偶然》 即将在韩国翻译出版 和大家分享一下书中精彩段落✨ ▊ 《绝非偶然》精华内容摘录 1. 我给不少人提过有关写东西的建议,建议他们在刚开始写的时候不用总写大话题,可以从小的「点」写起,锻炼自己写东西的感觉和能力。 这也正是我一直抱定的目的,写出来的东西要对他人有参考价值,而写作者可以从分享的过程中收获快乐。 2. 在现实生活里,我内心比较封闭,并不擅长与人交流,但是个性又很鲜明,所
博文视点Broadview
2023/05/19
1980
如何避免一步步走向平庸?
002 | 我是如何学习区块链的
前几天我们已经学了如何学习的“道”和“术”,学完之后就应该落地到实践上,通过不断地实践练习,才能将这些 知识资源 转化为我们的 知识资本。如果你看完前面的文章后,觉得讲得真好,然后缺乏思考缺乏行动,然后就没有然后了。为了更好地指导你们如何实践,本篇文章我将与你分享我是如何将知识资源转化为我的知识资本的。
Keegan小钢
2018/08/10
2K1
002 | 我是如何学习区块链的
一文看懂腾讯培训如何与互联网共舞
对于腾讯来说,业务和资金都不是最重要的,业务可以拓展,可以更换,资金可以吸收,可以调整,而人才却是最不可轻易替代的,是最宝贵的财富。——马化腾 作为目前中国最大的互联网综合服务提供商之一的腾讯
用户1756920
2018/06/20
1.1K0
Project CHIP试图解决物联网设备和网络碎片化问题
去年12月,Zigbee联盟发起了“Project Connected Home over IP(基于IP的家庭互联项目)”(CHIP),其宏伟目标是实现即插即用型消费物联网设备。 由亚马逊,苹果和谷歌领导的Project CHIP计划于今年晚些时候发布规格和开源代码。现在,最初的媒体和分析师的报道已经尘埃落定,CHIP项目就值得我们仔细研究,因为大型、知名的公司只有真正相信自己能够实现目标,才能够围绕这样一个大胆的目标团结起来。
用户4122690
2020/07/17
1.2K0
Project CHIP试图解决物联网设备和网络碎片化问题
互动百科被315点名了,知识产品如何优雅地赚钱?
2016年,知识经济很火。分答、知乎Live等平台先后入局,最近就连那个一直低调潜行的豆瓣也推出了内容付费产品:豆瓣时间,用时髦的话说内容付费俨然已成互联网风口。 一项互联网业务到了风口,就可能被315点名。 在一年一度的“公关节”315晚会上知识分享平台互动百科就被点名了,理由是因为它的付费服务:企业缴纳费用之后具有更强的编辑权限,甚至可以发布虚假信息,来龙去脉不必赘述,但“互动百科”这家百科知识平台为什么会采取“付费”模式,却值得深究。 百科是互联网最古老的内容形态之一。说到百科,人们首先会想到2001
罗超频道
2018/04/26
9360
互动百科被315点名了,知识产品如何优雅地赚钱?
陆奇的创业方法论:潮流、挑战和机遇
创业很难,挑战很多,往往九死一生,但创业机会也很大,如何拥抱机会,应对挑战是创业者们关心的话题。
AI科技大本营
2020/01/16
7260
陆奇的创业方法论:潮流、挑战和机遇
推荐阅读
相关推荐
碎片化学习前端知识
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档