Mybatis返回數(shù)組的兩種實現(xiàn)方式
更新時間:2025年03月27日 09:57:50 作者:Aa_duidui
這篇文章主要介紹了Mybatis返回數(shù)組的兩種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Mybatis返回數(shù)組的兩種方式
mysql沒有數(shù)組這種類型,我們可以以數(shù)組格式的字符串加入到數(shù)據(jù)庫,返回值是數(shù)組
1.Mapper.xml 返回數(shù)組
<resultMap type="返回實體類" id="result" > <result property="實體類字段名" column="mysql字段名" typeHandler="處理類"/> </resultMap> <select id="Mapper.java的方法名" parameterType="傳參類型" resultMap="resultMap的id"> select pricture from xm_picture </select>
例如:
<resultMap type="co.yixiang.modules.service.dto.PictureDto" id="PictureResult" > <result property="pictureArr" column="picture" typeHandler="co.yixiang.utils.mybatis.JsonStringArrayTypeHandler"/> </resultMap> <!-- parameterType 也可以是實體類 --> <select id="selectPictureById" parameterType="Long" resultMap="PictureResult"> select pricture from xm_picture where id = #{id} </select>
2.Mapper.java 返回數(shù)組 @Select注解
@Select("<script>" + " select picture from xm_picture where id = #{id} " + "</script>") @Results({@Result(property="實體類字段名",column="數(shù)據(jù)庫字段名",typeHandler= 處理類.class)}) PictureDto selectById(Long id);
例如:
@Select("<script>" + " select picture from xm_picture where id = #{id} " + "</script>") @Results({@Result(property="pictureArr",column="picture",typeHandler= JsonStringArrayTypeHandler.class)}) PictureDto selectById(Long id);
處理類代碼
import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @MappedJdbcTypes({JdbcType.VARCHAR}) 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 { ps.setString(i, toJson(parameter)); } @Override public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException { return this.toObject(rs.getString(columnName)); } @Override public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return this.toObject(rs.getString(columnIndex)); } @Override public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return this.toObject(cs.getString(columnIndex)); } private String toJson(String[] params) { try { return mapper.writeValueAsString(params); } catch (Exception e) { e.printStackTrace(); } return "[]"; } private String[] toObject(String content) { if (content != null && !content.isEmpty()) { try { return (String[]) mapper.readValue(content, String[].class); } catch (Exception e) { throw new RuntimeException(e); } } else { return null; } } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Socket編程心跳包創(chuàng)建實例解析
這篇文章主要介紹了Java Socket編程心跳包創(chuàng)建實例解析,具有一定借鑒價值,需要的朋友可以參考下2017-12-12spring security獲取用戶信息為null或者串值的解決
這篇文章主要介紹了spring security獲取用戶信息為null或者串值的解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03nacos一直頻繁的打印日志get changegroupkeys問題
這篇文章主要介紹了nacos一直頻繁的打印日志get changegroupkeys問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot @Import與@Conditional注解使用詳解
在了解spring boot自動配置原理前,再來了解下兩個注解@Import注解和@Conditional注解,@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊bean2022-10-10java如何根據(jù)提供word模板導(dǎo)出word文檔詳解
在日常的開發(fā)工作中,我們時常會遇到導(dǎo)出Word文檔報表的需求,比如公司的財務(wù)報表、醫(yī)院的患者統(tǒng)計報表、電商平臺的銷售報表等等,這篇文章主要給大家介紹了關(guān)于java如何根據(jù)提供word模板導(dǎo)出word文檔的相關(guān)資料,需要的朋友可以參考下2023-09-09