mybatis返回map類型數據空值字段不顯示的解決方案
更新時間:2022年03月10日 10:47:18 作者:生命不息_戰(zhàn)斗不止
這篇文章主要介紹了mybatis返回map類型數據空值字段不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
mybatis返回map數據空值字段不顯示
查詢sql添加每個字段的判斷空
IFNULL(rate,'') as rate
ResultType利用實體返回
不用map
springMVC+mybatis查詢數據
返回resultType=”map”時,如果數據為空的字段,則該字段省略不顯示,可以通過添加配置文件,規(guī)定查詢數據為空是則返回null。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL MAP Config 3.1//EN" ? ? "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> ? <settings> ? ? <setting name="callSettersOnNulls" value="true"/> ? </settings> </configuration>
spring-mybatis.xml
<!-- spring和MyBatis完美整合,添加mybatis的配置映射文件 --> ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ? ? <property name="dataSource" ref="dataSource"/> ? ? <property name="configLocation" value="classpath:mybatis-configuration.xml"/> ? ? <!-- 自動掃描mapping.xml文件 --> ? ? <property name="mapperLocations" value="classpath:mapping/*.xml"></property> ? </bean>
如果想要配置rate的默認值,例如“”字符串,則可以建立一個類,實現(xiàn)Mybatis的TypeHandler接口
public class EmptyStringIfNull implements TypeHandler<String> { ? ? @Override ? ? public String getResult(ResultSet rs, String columnName) throws SQLException { ? ? ?return (rs.getString(columnName) == null) ? "" : rs.getString(columnName);? ? ? } ? ? @Override ? ? public String getResult(ResultSet rs, int columnIndex) throws SQLException { ? ? ?return (rs.getString(columnIndex) == null) ? "" : rs.getString(columnIndex); ? ? } ? ? @Override ? ? public String getResult(CallableStatement cs, int columnIndex) ? throws SQLException { ? ? ?return (cs.getString(columnIndex) == null) ? "" : cs.getString(columnIndex); ? ? } ? ? @Override ? ? public void setParameter(PreparedStatement ps, int arg1, String str, JdbcType jdbcType) throws SQLException { }}
在sql.xml文件定義與使用如下如下
<resultMap id="find" type="java.util.LinkedHashMap"> ? ? <result property="name" column="name" /> ? ? <result property="phone" column="phone" /> ? ? <result property="rate" column="rate" typeHandler="com.mybatis.EmptyStringIfNull"/> </resultMap>
mybatis返回map空值未返回字段
mybatis 開啟CallSettersOnNulls
@Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { ? ? SqlSessionFactoryBean sqlSessionFactoryBean = new ?? ? SqlSessionFactoryBean(); ? ? sqlSessionFactoryBean.setDataSource(dataSource()); ? ? Configuration configuration = new .Configuration(); ? ? configuration.setCallSettersOnNulls(true);//map返回空字段消失問題 ? ? PageInterceptor pagePlugin = new PageInterceptor(); ? ? JalorResultSetInterceptor jalorResultSetPlugin = new JalorResultSetInterceptor(); ? ? ProgramInterceptor programPlugin = new ProgramInterceptor(); ? ? //添加插件 ? ? sqlSessionFactoryBean.setPlugins(new Interceptor[] {pagePlugin, jalorResultSetPlugin, programPlugin}); ? ? sqlSessionFactoryBean.setConfiguration(configuration); ? ? return sqlSessionFactoryBean.getObject(); }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java Swing組件下拉菜單控件JComboBox用法示例
這篇文章主要介紹了Java Swing組件下拉菜單控件JComboBox用法,結合具體實例形式分析了Swing組件下拉菜單控件JComboBox的具體定義、使用方法及相關使用注意事項,需要的朋友可以參考下2017-11-11詳解@ConfigurationProperties實現(xiàn)原理與實戰(zhàn)
這篇文章主要介紹了詳解@ConfigurationProperties實現(xiàn)原理與實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10