Mybatis-plus之空值无法插入

当使用Mybatis-plus尝试插入包含空值的数据时,可能会遇到插入失败的异常。解决这个问题可以考虑两种方法:一是在yml配置文件中进行相应设置;二是通过Java配置文件进行配置调整。

Mybatis-plus之空值无法插入

异常如下

org.mybatis.spring.MyBatisSystemException: nested exception is 
org.apache.ibatis.type.TypeException: Could not set parameters for mapping: 
ParameterMapping{property='couldDismantle', mode=IN, javaType=class java.lang.Long, 
jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', 
expression='null'}. Cause: org.apache.ibatis.type.TypeException: JDBC requires that the 
JdbcType must be specified for all nullable parameters.

出现这种自生成代码的insert无法写入值为空的数据,有以下两种解决办法:

1.yml配置文件配置如下

mybatis:
  configuration:
    mapUnderscoreToCamelCase: true
    jdbc-type-for-null: 'null'
  mapper-locations: classpath*:mapper/*.xml

2.通过Java配置文件设置

@Configuration
@MapperScan(basePackages = {"com.XXXX.mapper"}, sqlSessionFactoryRef = "XXXXXX")
public class DbYYConfig {

    @Autowired
    @Qualifier("XXXX")
    private DataSource dataSourceDb2;

    @Bean
    public SqlSessionFactory sqlSessionFactoryDb2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceDb2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDb2() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDb2());
    }

}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值