springboot+MyBatis返回Map时值为null的字段也会丢失

本文详细介绍了如何在MyBatis中配置SqlSessionFactoryBean,包括设置数据源、拦截器、插件以及自定义Configuration参数,如callSettersOnNulls。通过具体代码示例,展示了如何整合MyBatis与Spring框架,确保数据操作的正确性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:https://my.oschina.net/zhangguangxi/blog/1825505
mybatis版本:3.4.6
SqlSessionFactoryBean版本 :1.3.2
在这里插入图片描述

在你的数据源配置里加上下面代码
//------------------------------------------------加入的代码开始------------------------------------------------
加入的代码
//------------------------------------------------加入的代码结束------------------------------------------------

//设置默认属性

 @Bean(name = "baseSqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    Interceptor[] plugins =  new Interceptor[]{pageHelper()};
    bean.setPlugins(plugins);
    //------------------------------------------------加入的代码开始------------------------------------------------
    org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
    configuration.setCallSettersOnNulls(true);
           bean.setConfiguration(configuration);
    //------------------------------------------------加入的代码结束------------------------------------------------
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/base/*.xml"));
    return bean.getObject();
}

经过测试问题解决。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值