Mybatis返回?cái)?shù)組的兩種實(shí)現(xiàn)方式
更新時(shí)間:2025年03月27日 09:57:50 作者:Aa_duidui
這篇文章主要介紹了Mybatis返回?cái)?shù)組的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(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 Socket編程心跳包創(chuàng)建實(shí)例解析
這篇文章主要介紹了Java Socket編程心跳包創(chuàng)建實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
spring security獲取用戶信息為null或者串值的解決
這篇文章主要介紹了spring security獲取用戶信息為null或者串值的解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
nacos一直頻繁的打印日志get changegroupkeys問題
這篇文章主要介紹了nacos一直頻繁的打印日志get changegroupkeys問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot @Import與@Conditional注解使用詳解
在了解spring boot自動配置原理前,再來了解下兩個(gè)注解@Import注解和@Conditional注解,@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊bean2022-10-10
java如何根據(jù)提供word模板導(dǎo)出word文檔詳解
在日常的開發(fā)工作中,我們時(shí)常會遇到導(dǎo)出Word文檔報(bào)表的需求,比如公司的財(cái)務(wù)報(bào)表、醫(yī)院的患者統(tǒng)計(jì)報(bào)表、電商平臺的銷售報(bào)表等等,這篇文章主要給大家介紹了關(guān)于java如何根據(jù)提供word模板導(dǎo)出word文檔的相關(guān)資料,需要的朋友可以參考下2023-09-09
Java項(xiàng)目實(shí)現(xiàn)五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了Java項(xiàng)目實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05

