Mybatis操作数据库取不到带下划线的字段的值

 

在Mybatis设置config.xml中添加

<setting name="mapUnderscoreToCamelCase" value="true"/>

就好了,即设置开启自动驼峰命名转换。

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
出现这种情况可能是因为MybatisPlus默认开启了驼峰命名转换,而您的表中的字段名是下划线命名的,导致MybatisPlus找不到字段。您可以试试以下方法解决该问题: 1. 在实体类对应字段上添加@TableField注解,指定该字段名对应的数据库字段名,例如: ```java @TableField(value = "del_flag") private Integer delFlag; ``` 2. 在MybatisPlus的全局配置中,禁用驼峰命名转换,例如: ```java @Configuration @MapperScan("com.example.mapper") public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { return new MybatisPlusInterceptor(); } @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory()); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); sqlSessionFactory.setTypeAliasesPackage("com.example.entity"); GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setDbConfig(new GlobalConfig.DbConfig().setCapitalMode(false)); sqlSessionFactory.setGlobalConfig(globalConfig); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(false); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setPlugins(new Interceptor[]{ new PaginationInterceptor(), new OptimisticLockerInterceptor(), new MybatisPlusInterceptor() }); return sqlSessionFactory.getObject(); } } ``` 配置中的`configuration.setMapUnderscoreToCamelCase(false)`即为禁用驼峰命名转换的配置。 希望以上方法可以帮到您。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值