Two ways to return arrays by Mybatis
MySQL does not have an array type. We can add strings in array format to the database, and the return value is an array
Return to the array
<resultMap type="Return to entity class" > <result property="Entity Class Field Name" column="mysql field name" typeHandler="Processing Class"/> </resultMap> <select parameterType="Transferred Parameter Type" resultMap="resultMap id"> select pricture from xm_picture </select>
For example:
<resultMap type="" > <result property="pictureArr" column="picture" typeHandler=""/> </resultMap> <!-- parameterType It can also be an entity class --> <select parameterType="Long" resultMap="PictureResult"> select pricture from xm_picture where id = #{id} </select>
Return array @Select annotation
@Select("<script>" + " select picture from xm_picture where id = #{id} " + "</script>") @Results({@Result(property="Entity Class Field Name",column="Database field name",typeHandler= Processing class.class)}) PictureDto selectById(Long id);
For example:
@Select("<script>" + " select picture from xm_picture where id = #{id} " + "</script>") @Results({@Result(property="pictureArr",column="picture",typeHandler= )}) PictureDto selectById(Long id);
Process class code
import ; import ; import ; import ; import ; import ; import ; import ; @MappedJdbcTypes({}) public class JsonStringArrayTypeHandler extends BaseTypeHandler<String[]> { private static final ObjectMapper mapper = new ObjectMapper(); @Override public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException { (i, toJson(parameter)); } @Override public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException { return ((columnName)); } @Override public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return ((columnIndex)); } @Override public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return ((columnIndex)); } private String toJson(String[] params) { try { return (params); } catch (Exception e) { (); } return "[]"; } private String[] toObject(String content) { if (content != null && !()) { try { return (String[]) (content, String[].class); } catch (Exception e) { throw new RuntimeException(e); } } else { return null; } } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.