(1)longBlod最大保存4G的數據,可以用來存取大文件的字符或者其他類型文件的二進制流等等,現在的需求是存取一個大小几十兆的json數據,現在使用的手段是,將json數據直接保存到數據庫,並可以進行查看和修改,因此採用mysql裏的longblob類型進行存取
(2)將數據庫字段類型設置爲longblob,下面是navicat中我的設置
(3)這裏推薦大家使用mybatis的generator自動生成實體具體使用方法
https://blog.csdn.net/zhaoyy0513/article/details/103306605
(4)對應的實體屬性,自動生成也好,自己寫也好都是數組
(5)對應的mapping.xml
(6)獲取並傳遞給前臺
@Override
public void getContentById(Integer layoutId, HttpServletResponse response) throws IOException {
byte[] content = siteLayoutDao.getLayoutById(layoutId);
// 創建字節數組輸出流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 將GZIP寫入到輸出流
GZIPOutputStream gout = new GZIPOutputStream(bos);
// 將要傳遞的content寫入到Gzip
gout.write(content);
gout.close();
byte[] dest =bos.toByteArray();
response.setHeader("Content-Encoding", "gzip");
response.setContentType("application/json;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(dest);
outputStream.close();
}
(7)前臺解析,因爲數據過大,所以採用切割加載的策略
// 請求content
$.ajax({
type: "get",
contentType: "application/json;charset=utf-8",
dataType: 'text',
async: true, //默認配置,設爲異步的也就是可以同時執行兩個請求
url: "/mac/sites/1/site-layouts/getLayoutContentById?layoutId=25",
success: function (res) {
const len = res.length;
// 定義分段次數
const time = 10;
// 定義每次分段的長度
const everySize = len / time;
console.log("len:" + len);
console.log("everySize:" + everySize);
let i = 1;
let interVal = setInterval(function () {
console.log("i:" + i);
const infos = $("#last #getInfo");
infos.append(res.substring((i - 1) * everySize, everySize * i) + '<br/>');
i++;
// 如果變量i大於我們設定的分段次數,就清除Interval
if (i > time) {
clearInterval(interVal);
}
}, 1000);
},
error: function () {
alert("獲取失敗");
}
});