How can MyBatis return a list of a single field?

In MyBatis, ResultMap can be used to return a list of a field. Here is an example:

Firstly, in the mapper xml file, define a ResultMap to specify the fields to be returned.

<resultMap id="fieldListMap" type="java.lang.String">
    <id property="fieldName" column="field_name"/>
</resultMap>

Then use ResultMap in the SQL statement to return the list of fields.

<select id="getFieldList" resultMap="fieldListMap">
    SELECT field_name
    FROM your_table
</select>

Finally, in the Java code, call this query and retrieve the returned list.

List<String> fieldList = sqlSession.selectList("getFieldList");

This way, you can obtain the list of returned fields.

 

More tutorials

How can Mybatis return fields from multiple tables?(Opens in a new browser tab)

What is the purpose of selectone in mybatis?(Opens in a new browser tab)

How can MyBatis query the CLOB field?(Opens in a new browser tab)

What is the usage of selectOne in MyBatis?(Opens in a new browser tab)

How to handle a null return from selectOne in MyBatis?(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *