mp方法注入支持typeHandler

简介: mp方法注入支持typeHandler

java

画虎画皮难画骨,知人知面不知心。——佚名

今天处理了mp中自定义注入方法的typeHandler逻辑,只需要

#{et.name,typeHandler=org.dromara.streamquery.stream.plugin.mybatisplus.JsonFieldHandlerTest$JsonFieldHandler}

即可直接指定typeHandler

<script>
    <if test="list != null and !list.isEmpty()">
UPDATE user_info SET name=case id
<foreach collection="list" item="et">
<choose>
<when test="et != null and et.name != null">
when #{et.id} then #{et.name,typeHandler=org.dromara.streamquery.stream.plugin.mybatisplus.JsonFieldHandlerTest$JsonFieldHandler}
</when>
<otherwise>when #{et.id} then name</otherwise>
</choose>
</foreach>end
 WHERE 
id IN
(<foreach collection="list" item="et" separator=",">
#{et.id}
</foreach>)
</if>
</script>

例如我此处stream-query里使用的:

/*
 * 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;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.val;
import org.apache.ibatis.session.SqlSessionFactory;
import org.dromara.streamquery.stream.core.collection.Lists;
import org.dromara.streamquery.stream.core.lambda.function.SerSupp;
import org.dromara.streamquery.stream.plugin.mybatisplus.engine.handler.AbstractJsonFieldHandler;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * @author VampireAchao
 * @since 2023/4/14 13:57
 */
@MybatisPlusTest
class JsonFieldHandlerTest {
  @BeforeEach
  void init(@Autowired SqlSessionFactory sqlSessionFactory) {
    Database.buildMapper(sqlSessionFactory.getConfiguration(), UserInfoWithJsonName.class);
  }
  @Test
  void test() {
    val user =
        new UserInfoWithJsonName() {
          {
            setName(
                new Name() {
                  {
                    setUsername("VampireAchao");
                    setNickname("阿超");
                  }
                });
          }
        };
    Database.saveFewSql(Lists.of(user));
    Database.updateFewSql(Lists.of(user));
    val dbUser = Database.getById(user.getId(), UserInfoWithJsonName.class);
    Assertions.assertEquals("VampireAchao", dbUser.getName().getUsername());
    Assertions.assertEquals("阿超", dbUser.getName().getNickname());
  }
  public static class JsonFieldHandler extends AbstractJsonFieldHandler {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected Object parse(String json, TableInfo tableInfo, TableFieldInfo fieldInfo) {
      Class<?> fieldType = fieldInfo.getField().getType();
      return ((SerSupp<Object>) (() -> objectMapper.readValue(json, fieldType))).get();
    }
    @Override
    @SneakyThrows
    protected String toJson(Object obj, TableInfo tableInfo, TableFieldInfo fieldInfo) {
      return objectMapper.writeValueAsString(obj);
    }
  }
  @Data
  @TableName(value = "user_info", autoResultMap = true)
  static class UserInfoWithJsonName {
    private Long id;
    @TableField(typeHandler = JsonFieldHandler.class)
    private Name name;
  }
  @Data
  static class Name {
    private String username;
    private String nickname;
  }
}

对应链接:https://gitee.com/dromara/stream-query/blob/main/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/JsonFieldHandlerTest.java

相关文章
|
6月前
|
IDE Java 开发工具
@Builder这个注解的作用
@Builder这个注解的作用
215 0
|
2月前
|
Python
扩展类的 property
扩展类的 property
26 0
|
4月前
|
Java Spring 容器
重写并自定义依赖的原生的Bean方法
重写并自定义依赖的原生的Bean方法
29 5
|
11月前
|
JSON 数据格式
mp中typeHandler自动获取字段类型
mp中typeHandler自动获取字段类型
116 2
|
6月前
|
存储 Java 数据库连接
MyBatis的类型处理器TypeHandler与自定义实现
MyBatis的类型处理器TypeHandler与自定义实现
516 0
|
6月前
|
Java 数据库连接 mybatis
Mybatis查找结果注入bean时出现类型转换错误
Mybatis查找结果注入bean时出现类型转换错误
30 0
|
Java 数据格式 Spring
SpringBoot中@Value注解注入List或Map数据格式出现异常
在做一个小demo的时候、做的例子是我想在程序运行时将一些数据放入到配置类中的属性中、我想到可以通过yaml配置的数据映射到实体类中的属性中、我在想通过这种形式能不能映射。
|
Java Spring
静态工具类注入mapper对象
项目中需要在一个utils工具类中,调用mapper对象进行查询并进行排序后选出数值最小的数值,然而静态方法里面直接注入会报空指针的错误。现在总结一下解决办法。
732 0
|
Java Spring 容器
Spring-方法注入lookup、方法替换MethodReplacer接口
Spring-方法注入lookup、方法替换MethodReplacer接口
136 0