Mybatis返回?cái)?shù)組的兩種實(shí)現(xiàn)方式
更新時(shí)間:2025年03月27日 09:57:50 作者:Aa_duidui
這篇文章主要介紹了Mybatis返回?cái)?shù)組的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Mybatis返回?cái)?shù)組的兩種方式
mysql沒有數(shù)組這種類型,我們可以以數(shù)組格式的字符串加入到數(shù)據(jù)庫,返回值是數(shù)組
1.Mapper.xml 返回?cái)?shù)組
<resultMap type="返回實(shí)體類" id="result" >
<result property="實(shí)體類字段名" 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 也可以是實(shí)體類 -->
<select id="selectPictureById" parameterType="Long" resultMap="PictureResult">
select pricture from xm_picture where id = #{id}
</select>2.Mapper.java 返回?cái)?shù)組 @Select注解
@Select("<script>" +
" select picture from xm_picture where id = #{id} " +
"</script>")
@Results({@Result(property="實(shí)體類字段名",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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中不定長(zhǎng)參數(shù)的實(shí)例用法
在本篇文章里小編給大家分享的是關(guān)于java中不定長(zhǎng)參數(shù)的使用方法以及相關(guān)代碼內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2020-02-02
詳解如何快速定位和解決JSON錯(cuò)誤(以Protobuf的JsonFormat.ParseException為例)
在開發(fā)過程中,JSON數(shù)據(jù)的解析是一個(gè)常見的操作,尤其是在微服務(wù)架構(gòu)中,服務(wù)之間的通信通常依賴于JSON格式的數(shù)據(jù),然而,JSON數(shù)據(jù)的格式錯(cuò)誤往往會(huì)導(dǎo)致解析失敗,進(jìn)而引發(fā)系統(tǒng)異常,本文將以一個(gè)實(shí)際的錯(cuò)誤案例為例,詳細(xì)講解如何快速定位和解決JSON解析錯(cuò)誤2025-03-03
Spring自動(dòng)配置之condition條件判斷上篇
這篇文章主要為大家介紹了SpringBoot condition條件判斷功能的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
SpringBoot整合Redisson實(shí)現(xiàn)分布式鎖
本文主要介紹了SpringBoot整合Redisson實(shí)現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式
這篇文章主要介紹了Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

