栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

tkmybties批量插入insertList报错主键不能为空解决办法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tkmybties批量插入insertList报错主键不能为空解决办法

最近刚接触tkmybties,今天调试接口突然发现批量插入报错,控制台日志显示主键id为空,这就很奇怪了,主键id是String类型并且赋值的UUID,直接上报错图,9个字段愣是给我整成了8个,并且主键id没了,ps:使用mysql数据库..

上mapper代码:

public interface HerosEntityMapper extends MyMapper{

}

public interface MyMapper extends Mapper, MySqlMapper {

}

RegisterMapper
public interface MySqlMapper extends InsertListMapper, InsertUseGeneratedKeysMapper {

}

@RegisterMapper
public interface InsertListMapper {
    @Options(
        useGeneratedKeys = true
    )
    @InsertProvider(
        type = SpecialProvider.class,
        method = "dynamicSQL"
    )
    int insertList(List var1);
}

查询资料发现insertList使用条件:实体必须包含"id"属性并且必须是自增列

要想批量新增主键是String的处理办法:

public interface HerosEntityMapper extends MyMapper{
    int insertList(@Param("list") List list);
}



    INSERT INTO t_heroes(
        id, a, b, c, d, e
    )VALUES
    
        (
        #{ele.id,jdbcType=CHAR},
        #{ele.a,jdbcType=CHAR},
        #{ele.b,jdbcType=VARCHAR},
        #{ele.c,jdbcType=DECIMAL},
        #{ele.d,jdbcType=VARCHAR},
        #{ele.e,jdbcType=TIMESTAMP}
        )
    

也可以单个插入

herosEntityMapper.insertSelective(herosEntity);
最后说个重点,使用tkmybties在启动类上的注解
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("com.xxxx.xxxx.mapper")

mybatis注解,注意看两者的区别

import org.mybatis.spring.annotation.MapperScan;
@MapperScan("com.xxxx.xxxx.mapper")
pom中的变化,多了两个tk.mybatis的引用

	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	2.1.1


	tk.mybatis
	mapper-spring-boot-starter
	2.1.5


	tk.mybatis
	mapper
	4.0.4

tkmybties刚接触还不太会用,感觉坑还有点多..

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号