mysql如何儲存JSON型別資料實戰
阿新 • • 發佈:2021-02-05
技術標籤:筆記
經理:我有個欄位是JSON型別的,你把我前臺傳過去的JSON存進去!
小編: 好 沒問題 上程式碼
實體類及對映Vo 專案的查詢,返回,新增,編輯均使用Vo當入參,回參
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("test")
public class Test implements Serializable {
private static final long serialVersionUID = 1L;
//ID
@TableId("ID")
private String id;
//AAA
@TableField("AAA")
private String aaa;
}
@Data
public class TestSQLVo {
private static final long serialVersionUID = 1L;
//ID
private String id;
//AAA
private String aaa;
}
@Data
public class TestAddVo {
private static final long serialVersionUID = 1L;
//AAA
@NotEmpty(message = "AAA不能為空")
private String aaa;
}
@Data
public class TestEditVo {
private static final long serialVersionUID = 1L;
//ID
@NotEmpty(message = "ID不能為空")
private String id;
//AAA
private String aaa;
}
@Data
public class TestQueryLVo {
private static final long serialVersionUID = 1L;
//ID
private String id;
}
@Data
public class TestResultVo {
private static final long serialVersionUID = 1L;
//ID
private String id;
//AAA
private String aaa;
}
Controller層
//用途說明: 根據主鍵查詢列表資料資訊
@PostMapping("queryTestById")
public AjaxResult queryTestById(@RequestBody TestQueryVo params) {
try {
TestResultVo page = iTestApplication.queryTestById(params);
return getObject(page);
} catch (Exception e) {
e.printStackTrace();
return error(e.getMessage());
}
}
//用途說明: 新增資訊
@PostMapping("addTest")
public AjaxResult addTest(@RequestBody @Validated TestAddVo params) {
try {
boolean flag = false;
flag = iTestApplication.addTest(params);
if (flag) {
return success();
} else {
return error("儲存資訊失敗!");
}
} catch (Exception e){
e.printStackTrace();
return error(e.getMessage());
}
}
//用途說明: 編輯資訊
@PostMapping("updateTest")
public AjaxResult updateTest(@RequestBody @Validated TestEditVo params) {
try {
boolean flag = false;
flag = iTestApplication.updateTest(params);
if (flag) {
return success();
} else {
return error("儲存資訊失敗!");
}
} catch (Exception e){
e.printStackTrace();
return error(e.getMessage());
}
}
service介面省略,service實現類
//用途說明: 根據主鍵查詢列表資料資訊
@Override
public TestResultVo queryTestById(TestQueryVo params) {
//查詢資料
TestResultVo resultVo = TestMapper.queryInfo(params);
return resultVo;
}
//用途說明: 新增資訊
@Override
public boolean addTest(TestAddVo params) throws Exception {
//欄位aaa JSON轉String
String aaa = "";
if(params.getAaa() != null){
aaa = params.getAaa().toString();
}
TestSQLVo testSQLVo = new TestSQLVo();
BeanUtils.copyProperties(params,testSQLVo);
//自動生成主鍵
testSQLVo.setId(IDHelper.uuid());
return TestMapper.insert(testSQLVo);
}
//用途說明: 編輯資訊
@Override
public boolean updateTest(TestEditVo params) throws Exception {
//欄位aaa JSON轉String
String aaa = "";
if(params.getAaa() != null){
aaa = params.getAaa().toString();
}
TestSQLVo testSQLVo = new TestSQLVo();
BeanUtils.copyProperties(params,testSQLVo);
return TestMapper.update(testSQLVo);
}
mapper
TestResultVo queryInfo(@Param("TestQueryVo") TestQueryVo params);
boolean insert(@Param("TestSQLVo") TestAddSQLVo params);
boolean update(@Param("TestSQLVo") TestSQLVo params);
xml
<!--新增資訊-->
<insert id="insert" parameterType="TestSQLVo">
INSERT INTO Test
(ID,AAA)
VALUES
(#{TestSQLVo.id,jdbcType=VARCHAR},#{TestSQLVo.aaa,jdbcType=VARCHAR})
</insert>
<!--編輯資訊-->
<update id="update" parameterType="TestSQLVo">
UPDATE Test
SET
AAA = #{TestSQLVo.aaa,jdbcType=VARCHAR}
WHERE
ID = #{TestSQLVo.id,jdbcType=VARCHAR}
</update>
<!--根據主鍵查詢列表資料資訊-->
<select id="queryInfo" parameterType="TestQueryVo" resultType="TestResultVo">
SELECT
ID,
AAA
FROM
test
<!--指標明細評分ID-->
<if test="TestQueryVo.aaa != '' and TestQueryVo.aaa != null">
AND AAA = #{TestQueryVo.aaa,jdbcType=VARCHAR}
</if>
</select>
成功插入截圖
新增,編輯入參,根據id查詢返回結果展示
新增
{
“aaa”: {“one”: 1,“two”: 2,“three”: 3}
}
編輯
{
“aaa”: {“one”: 1,“two”: 2,“three”: 3},
“id”: “c4b03fbefecf425db1c6e357903423a8”
}
查詢返回格式
{
“msg”: “操作成功”,
“code”: “0”,
“data”: {
“id”: “c4b03fbefecf425db1c6e357903423a8”,
“aaa”: “{“one”: 1, “two”: 2, “three”: 3}”
}
}
小編提示: 前臺傳來的JSON需要轉成String型別當引數條件插入資料庫中,資料庫中查到的需要用String型別返回