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)文章
詳解如何快速定位和解決JSON錯誤(以Protobuf的JsonFormat.ParseException為例)
在開發(fā)過程中,JSON數(shù)據(jù)的解析是一個常見的操作,尤其是在微服務(wù)架構(gòu)中,服務(wù)之間的通信通常依賴于JSON格式的數(shù)據(jù),然而,JSON數(shù)據(jù)的格式錯誤往往會導(dǎo)致解析失敗,進而引發(fā)系統(tǒng)異常,本文將以一個實際的錯誤案例為例,詳細講解如何快速定位和解決JSON解析錯誤2025-03-03
SpringBoot整合Redisson實現(xiàn)分布式鎖
本文主要介紹了SpringBoot整合Redisson實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Mybatis Order by動態(tài)參數(shù)防注入方式
這篇文章主要介紹了Mybatis Order by動態(tài)參數(shù)防注入方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

