首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >mp方法注入支持typeHandler

mp方法注入支持typeHandler

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

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

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

#{et.name,typeHandler=org.dromara.streamquery.stream.plugin.mybatisplus.JsonFieldHandlerTest$JsonFieldHandler} 即可直接指定typeHandler

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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里使用的:

代码语言: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;

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

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

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

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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
spring获取AliasFor增强的注解
此处是关于issue:https://gitee.com/dromara/stream-query/issues/I7BSNV
阿超
2023/06/23
2810
toParamStr支持数组
阿超
2024/03/23
1300
批量分页
今天分享一个装一个分批分页,传入页码、分页条数,然后查询分页条数+1条数据,这里的+1是为了判断还有下一页数据,然后查询到后,再按照传入的lambda条件筛选,筛选完后的数据添加进集合,添加到集合后如果满足当前分页所需条数,就返回数据
阿超
2024/01/01
2020
动态mapper优先级问题
https://gitee.com/VampireAchao/stream-query/issues/I6EJ27
阿超
2023/03/19
4210
Spring的LambdaSafe
今天看了下Spring的LambdaSafe类,它提供了一种安全的方式调用Lambda,例如不使用LambdaSafe时的问题
阿超
2024/08/23
1000
wrapper支持typeHandler
https://gitee.com/dromara/stream-query/pulls/340
阿超
2023/06/23
2530
mp自定义sql注入
以mysql语法INSERT INTO user_info (name,age,email) VALUES ( ?,?,? ),( ?,?,? )举例: 首先注入自定义策略 package io.gi
阿超
2022/08/21
3080
stream-query的BeanHelper拷贝支持Converter
这里可以自定义Converter,此处new CopyOption()是不带默认内置的Converter的,但是CopyOption.of()是带的
阿超
2024/01/23
1620
CompletableFuture事务处理
使用CompletableFuture进行异步任务编排时,可能会有事务的支持需求,我们这里可以使用之前我写的手动回滚、提交事务进行处理
阿超
2022/12/11
1.7K0
升级mp新版本后,service里removeByIds主键类型不一致报错
主键类型是Integer,使用service中removeByIds,传入List<String>报错
阿超
2023/01/07
6550
VIN解析
https://gitee.com/dromara/hutool/pulls/1005
阿超
2023/06/23
2390
MyBatis-Plus 之Service CRUD 接口
IService 接口: IService是MyBatis-Plus提供的通用Service接口,用于提供CRUD操作。它继承自Spring的org.springframework.data.service.CrudRepository接口,具有常见的CRUD方法。
默 语
2024/11/20
2840
MyBatis-Plus 之Service CRUD 接口
stream-query
弄了好几天,终于成功上传到maven中央仓库了 使用方式: <dependency> <groupId>io.github.vampireachao</groupId> <artifactId>stream-plugin-mybatis-plus</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>io.github.vampireachao</groupId> <arti
阿超
2022/08/21
7510
stream-query加入dromara开源组织
Stream-Query允许完全摆脱Mapper的Mybatis-Plus体验!可以使用类似“工具类”这样的静态函数进行数据库操作
阿超
2023/04/04
4490
stream-query加入dromara开源组织
mp wrapper小技巧
别让你的舌头超越你的思想。——第欧根尼 对于mp的wrapper,直接使用nested+or是可以正确执行的 例如: Db.list(Wrappers.lambdaQuery(UserInfo.class).or().eq(UserInfo::getName, "Jon")); 生成 2023-04-13 22:39:54.858 DEBUG 20668 --- [ main] o.d.s.s.p.m.m.UserInfoMapper.selectList : ==> Prepa
阿超
2023/04/17
2970
MyBatisPlus的SQL注入器批量插入更新方法
在前几天,我们使用了MyBatis plus的SQL注入器成功注入了我们想要的SQL写法。
半月无霜
2024/03/29
5860
MyBatisPlus的SQL注入器批量插入更新方法
mp对象集合in查询
简单说明一下:此处是将入参的list获取到类型,根据表字段和list进行遍历,获取lambda进行取值,然后进行in以及or查询
阿超
2023/02/28
7340
mp对象集合in查询
quartz工具类含倒计时
阿超
2023/12/19
2290
stream拓展
源码地址:https://gitee.com/VampireAchao/stream-query/blob/master/stream-core/src/main/java/io/github/vampireachao/stream/core/stream/Steam.java
阿超
2022/08/21
1.2K0
编写mybatis脱敏插件
首先贴成品链接:https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/275
阿超
2022/10/07
1.2K0
相关推荐
spring获取AliasFor增强的注解
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档