Mybatis annotation how to return hashmap key item if value is null

1.4k Views Asked by At

springboot mybatis example:

@Select({"select id,xx from table where id=#{id}"})
Map<String,Object> queryById(@Param("id") Long id);

if table xx is null, the returned map does not contains xx key.

I search for a long time, mapper.xml could configure <setting name="callSettersOnNulls" value="true"/> could solve my problem, but I do not use xml configuration mode, how to config param callSettersOnNulls using mybatis annotation??

2

There are 2 best solutions below

0
On

i say no, callSettersOnNulls now only support the global configuration, it can't support on specific method

see the source code of mybatis:

org.apache.ibatis.executor.resultset.DefaultResultSetHandler
applyPropertyMappings() or applyAutomaticMappings()

enter image description here

2
On

I've had the same problem right now. I don't solve it yet

Answering the question

how to config param callSettersOnNulls using mybatis annotation

, the configuration would be something like this.

import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisConfiguration {

    @Bean
    ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {

            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                configuration.setCallSettersOnNulls(true);
            }
        };
    }
}