MyBatis: Map String to boolean

Julián Martínez :

I have booleans inserted in my database as Y/N. When I try to map the result to a boolean java type, it always set it to a false in any case in my pojo.

Is there any way to map String to boolean? Here's my code:

<resultMap id="getFlag" type="MyPojo">
    <result property="myFlag" column="MY_FLAG"/>
</resultMap>
hakamairi :

What you need is a typeHandler for you Y/N Boolean type: (more explained here) Actual Handler:

public class YesNoBooleanTypeHandler extends BaseTypeHandler<Boolean> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, convert(parameter));
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return convert(rs.getString(columnName));
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return convert(rs.getString(columnIndex));
    }

    @Override
    public Boolean getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return convert(cs.getString(columnIndex));
    }

    private String convert(Boolean b) {
        return b ? "Y" : "N";
    }

    private Boolean convert(String s) {
        return s.equals("Y");
    }

}

Your usage:

<result property="myFlag" column="MY_FLAG" javaType="java.lang.Boolean" jdbcType="VARCHAR" typeHandler="com.foo.bar.YesNoBooleanTypeHandler" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reversing a HashMap from Map<String, Boolean> to Map<Boolean, List<String>>

Map<String, Map<String, Boolean>> myMap = new HashMap<String,HashMap<String,Boolean>>();

Map<String, Long> to Map<String, Boolean> if long is > X with Stream

Coupling FirebaseRecyclerViewAdapter to a Boolean/String Map.Entry

How to fill this HashMap Map<Integer, Map<String, Boolean>> pr

error TS2345: Argument of type 'Map<String, boolean>' is not ass ignable to parameter of type 'IterableShim<[String, boolean]>'

Reducing/Folding a List of Strings to a Map[String,Boolean] in Scala

How to get elements from text and set it on Map<String, boolean> on JAVA

Gson for mixed values (boolean, int, String) from a Map

Why MyBatis can't map the JSONB type to a Map<String, Object> or to JsonNode?

Get a boolean of a map

Map boolean values to strings

Map a tinyint as boolean hibernate

How to map an int to a boolean

Boolean map in flutter

Mybatis: how to check if a row exists and return a boolean?

Why can't I use categories: Map<String, Boolean> when I do a deep copy with kotlin?

Cucumber 3+ - UndefinedDataTableTypeException: Can't convert DataTable to Map<String, Boolean>

Scala - How to handle the Non string values ( Date, Double, Boolean) while building Json by looping with Map and Object Array

How can I break a string with <some value> tags into a Map collection and assigned it boolean values if it is or not?

MyBatis get resultMap as String

How to map multiple beans in Mybatis?

Java Map did not accept "boolean"

Array map values and return boolean

Convert boolean to string in DataFrame

Convert string to boolean for checkbox

Mapstruct mapping Boolean to String

parse string with condition to boolean

python - compare string with boolean