java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis-plus之空值无法插入

Mybatis-plus之空值无法插入问题及解决

作者:IT小土豆

这篇文章主要介绍了Mybatis-plus之空值无法插入问题及解决过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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());
    }

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文