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





