那么如何将age字段映射到map中呢。提供两种解决方法:
使用Mybatis config配置
创建configuration.xml
配置Mybatis的SqlSessionFactoryBean
1 2 3 4 5 6 | < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource" /> < property name = "configLocation" value = "classpath:/META-INF/spring/configuration.xml" /> < property name = "mapperLocations" value = "classpath:/META-INF/spring/mybatis/modelMap/*.xml" /> </ bean > |
在这种配置中,age将以null值映射到map中。
如果想要配置age的默认值,则可以建立一个类,实现Mybatis的TypeHandler接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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) <span style= "line-height:1.5;font-size:9pt;" > throws SQLException {</span><span style= "line-height:1.5;font-size:9pt;" > }</span><span style= "line-height:1.5;font-size:9pt;" >}</span> |
继续在resultMap中使用,即可配置age的默认值(上述代码中age的默认值为"")
网上有些资料中提到可以使用 defaultValue 和 nullValue的配置,但是这中配置是ibatis的用法,在Mybatis中已经移除。
联系客服