How to change value (empty string) from query when using mybatis?

I am doing a project that uses Spring 3.1.1 and MyBatis3.0.

I am trying to change iBatis to MyBatis. However, I am struggling with resultmap.

When using iBatis, I can process the values ​​from the request, as shown below, using "nullValue".

<resultMap class="java.util.HashMap" id="ChannelData">
        <result property="id"       javaType="java.lang.String"         column="CHANNEL_ID" nullValue=""/>
        <result property="code"         column="SELECTSCOPE"        nullValue="Television"/>
</resultMap>

The problem is that there is no 'nullValue' in MyBatis. Also, if the column is "null", mybatis never populates this element. eg. if "SELECTSCOPE" is null, it brings {id=aaa}. I need such data → {id=aaa, code=''}. Is there any way to handle this?

PS

I request more than 20 columns. Some of them need "" when the value is null, others have their own default value for some string value. (If I use iBatis, "nullValue" is the magic keyword), I found some links that recommend using a custom type handler, but making the handler over 20 may cause future confusion for repair or maintenance. I need an easy way.

Thanks a lot: D

==================================================== ======================================

I found a way to bring some null values. It needs some configuration.

  • make config.xml, which should contain some information about MyBatis Config DTD and settings, as shown below
<?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>

{id="aaa", code = null}. . ? . "code" null, String "default". {id="aaa", code=null} {id="aaa",code="default"}. ?

~

+4
2

, . - . TypeHandlerClass, "org.apache.ibatis.type.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 {
    }
}; 

, resultMap 'typehandler', :

    <resultMap type="map" id="channel">
        <result property="id"   column="CHANNEL_ID" typeHandler="StringHandler"/>
        <result property="code" column="SELECTSCOPE"    typeHandler="StringHandler"/>
</resultMap>

. , defaultValue .java-. resultMap . , ?

'if else' java- , , . : D Thanx

+3

, nullValue .

HashMap, callSettersOnNulls, , , :

public class ChannelData {
    private String id;
    private String code = "default";
}

, SQL- , NVL/COALESCE:

SELECT channel_Id, NVL(selectScope, 'default') AS selectScope

, , , , .

( ), if-else ( , , ).

0

Source: https://habr.com/ru/post/1534952/


All Articles