SpringBoot整合mybatis查询数据库结果中的字段为null时不返回问题解决方法

多数据源情况:

直接在配置文件application.properties中增加字段配置信息:

对于多数据源的情况,需要在数据源config配置类中文件中SqlSessionFactory bean中增加一段代码:

org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setCallSettersOnNulls(true);
factoryBean.setConfiguration(configuration);

例如:

SpringBoot整合mybatis查询数据库结果中的字段为null时不返回问题解决方法

 

 

 

package com.picc.config;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @author
 */
@Configuration
@MapperScan(basePackages = "xxx.common.dao.informix", sqlSessionFactoryRef = "gp_khyxSqlSessionFactory")
public class PrimaryConfig {
    @Value("${spring.datasource.primary.driver-class-name}")
    private String drivername;
    @Value("${spring.datasource.primary.url}")
    private String url;
    @Value("${spring.datasource.primary.password}")
    private String password;
    @Value("${spring.datasource.primary.username}")
    private String username;
    @Value("${spring.datasource.primary.publicKey}")
    private String publicKey;


    @Primary
    @Bean(name = "gp_khyxDataSource")
    public DataSource primaryDataSource() {
//        DriverManagerDataSource dataSource
//                = new DriverManagerDataSource();
        DruidDataSource dataSource = new DruidDataSource();
        try {
            dataSource.setValidationQuery("SELECT current FROM sysmaster:sysshmvals");
            dataSource.setDriverClassName(drivername);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            //dataSource.setPassword(password);
            dataSource.setPassword(ConfigTools.decrypt(publicKey, password));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }


    @Bean(name = "gp_khyxJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("gp_khyxDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "gp_khyxSqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("gp_khyxDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);

        org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
        configuration.setCallSettersOnNulls(true);
        bean.setConfiguration(configuration);


        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/informix/*.xml"));
        return bean.getObject();
    }

    @Bean("gp_khyxSqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate sqlsessiontemplate(
            @Qualifier("gp_khyxSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

 

不是多数据源情况

直接在配置文件application.properties中增加字段配置信息:

Mybatis.configuration.call-setters-on-nulls:true

yml:

mybatis:
configuration:
call-setters-on-nulls: true



参考原文:https://blog.csdn.net/STIll_ly/article/details/103993486

SpringBoot整合mybatis查询数据库结果中的字段为null时不返回问题解决方法

上一篇:MySQL 优化:MySQL Where 执行顺序


下一篇:sql server 中文like %%查不出数据问题/中文查询无效