<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      mybatis自動填充時間字段

      對于實體中的created_onupdated_on來說,它沒有必要被開發人員去干預,因為它已經足夠說明使用場景了,即在插入數據和更新數據時,記錄當前時間,這對于mybatis來說,通過攔截器是可以實現的,記得之前說過在jpa中實現的方法,主要通過jpa的注解實現的,因為今天的mybatis需要用到java的攔截器。

      定義兩個注解

      @Retention(RetentionPolicy.RUNTIME)
      @Target( {ElementType.FIELD})
      public @interface CreatedOnFuncation {
      
        String value() default "";
      }
      @Retention(RetentionPolicy.RUNTIME)
      @Target( {ElementType.FIELD})
      public @interface UpdatedOnFuncation {
      
        String value() default "";
      }
      

      使用這兩個注解

      @Getter
      @Builder(toBuilder = true)
      @ToString
      public class UserInfo {
        private Long id;
        private String name;
        private String email;
      
        @CreatedOnFuncation
        private LocalDateTime createdOn;
        @UpdatedOnFuncation
        private LocalDateTime updatedOn;
      }
      
      

      定義攔截器,重寫賦值的語句

      /**
       * 時間攔截器.
       */
      @EqualsAndHashCode(callSuper = true)
      @Data
      @Accessors(chain = true)
      @Intercepts({@Signature(
          type = org.apache.ibatis.executor.Executor.class,
          method = "update",
          args = {MappedStatement.class, Object.class})})
      public class CreateUpdateTimeInterceptor extends AbstractSqlParserHandler implements Interceptor {
      
        private Properties properties;
      
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
          MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
      
          // 獲取 SQL 命令
          SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
      
          // 獲取參數
          Object parameter = invocation.getArgs()[1];
      
          // 獲取私有成員變量
          Field[] declaredFields = parameter.getClass().getDeclaredFields();
          if (parameter.getClass().getSuperclass() != null) {
            Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields();
            declaredFields = ArrayUtils.addAll(declaredFields, superField);
          }
          // 是否為mybatis plug
          boolean isPlugUpdate = parameter.getClass().getDeclaredFields().length == 1
              && parameter.getClass().getDeclaredFields()[0].getName().equals("serialVersionUID");
      
          //兼容mybatis plus的update
          if (isPlugUpdate) {
            Map<String, Object> updateParam = (Map<String, Object>) parameter;
            Class<?> updateParamType = updateParam.get("param1").getClass();
            declaredFields = updateParamType.getDeclaredFields();
            if (updateParamType.getSuperclass() != null) {
              Field[] superField = updateParamType.getSuperclass().getDeclaredFields();
              declaredFields = ArrayUtils.addAll(declaredFields, superField);
            }
          }
          for (Field field : declaredFields) {
      
            // insert
            if (field.getAnnotation(CreatedOnFuncation.class) != null) {
              if (SqlCommandType.INSERT.equals(sqlCommandType)) {
                field.setAccessible(true);
                field.set(parameter, new Timestamp(System.currentTimeMillis()));
              }
            }
      
            // update
            if (field.getAnnotation(UpdatedOnFuncation.class) != null) {
              if (SqlCommandType.INSERT.equals(sqlCommandType)
                  || SqlCommandType.UPDATE.equals(sqlCommandType)) {
                field.setAccessible(true);
      
                //兼容mybatis plus的update
                if (isPlugUpdate) {
                  Map<String, Object> updateParam = (Map<String, Object>) parameter;
                  field.set(updateParam.get("param1"), new Timestamp(System.currentTimeMillis()));
                } else {
                  field.set(parameter, new Timestamp(System.currentTimeMillis()));
                }
              }
            }
          }
      
          return invocation.proceed();
        }
      
        @Override
        public Object plugin(Object target) {
          if (target instanceof org.apache.ibatis.executor.Executor) {
            return Plugin.wrap(target, this);
          }
          return target;
        }
      
        @Override
        public void setProperties(Properties prop) {
          this.properties = prop;
        }
      }
      

      添加測試用例

        @Test
        public void insert() {
          UserInfo userInfo = UserInfo.builder()
              .name("lind")
              .email("test@sina.com")
              .build();
          userInfoMapper.insert(userInfo);
          System.out.println("userinfo:" + userInfo.toString());
        }
      

      解決是我們所預想的,created_on和updated_on被自動賦上值了。

      userinfo:UserInfo
      (
      id=1085780948955959297, 
      name=lind, 
      email=test@sina.com, 
      createdOn=2019-01-17T14:08:45.665,
      updatedOn=2019-01-17T14:08:45.665
      )
      
      
      posted @ 2019-01-17 14:19  張占嶺  閱讀(16850)  評論(2)    收藏  舉報
      主站蜘蛛池模板: 亚洲熟妇乱色一区二区三区| 东乡县| 日韩乱码人妻无码中文字幕视频| 亚洲AV成人无码久久精品四虎| 伊人久久大香线蕉成人| 国产av国片精品一区二区| 妺妺窝人体色www聚色窝仙踪| 青青青青国产免费线在线观看| 国产综合色在线精品| 亚洲熟妇色xxxxx欧美老妇| 色狠狠综合天天综合综合| 少妇人妻偷人精品系列| 精品熟女日韩中文十区| 人妻丝袜无码专区视频网站| 亚洲熟妇乱色一区二区三区| 少妇极品熟妇人妻无码| 国产中文字幕一区二区| 麻豆亚洲精品一区二区| 在线视频中文字幕二区| 久久综合色一综合色88| 四虎国产精品成人免费久久| 国产午夜精品无码一区二区 | 农村老熟妇乱子伦视频| 99久久国产综合精品色| 国产播放91色在线观看| 国产精品aⅴ免费视频| 人妻综合专区第一页| 一区二区三区精品不卡| 国产成人高清亚洲一区91| 亚洲国产日韩精品一区二区三区| 亚洲av免费成人精品区| 91中文字幕在线一区| 上思县| 五月丁香六月综合缴情在线| 国内自拍av在线免费| 精品人妻系列无码天堂| 国产高在线精品亚洲三区| 亚洲欧洲精品日韩av| 中文字幕一区二区精品区| 宅男噜噜噜66在线观看| 亚洲 中文 欧美 日韩 在线|