2022年3月9日 星期三

資料表存JSON格式文字

把JSON存進資料庫的欄位, 要做以下的設置



@Entity(name = "patient_xxx")
@TypeDef(name = "json", typeClass = JsonType.class)
public class PatientXXX {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    // 傳送Json內容
    @Type(type = "json")
    @Column(name = "content", columnDefinition = "json")
    private String content;
...
}

另外, 接值進來的時候, 要用ObjectMapper轉成字串




    @PostMapping("/lab/PutStatus")
	 public JwtResponseResult putStatus(@RequestBody PutStatusRequestVo putStatusRequestVo) {
    	
    	try {
    		ObjectMapper mapper = new ObjectMapper();
       	  String jsonString = mapper.writeValueAsString(putStatusRequestVo);
       
        	labService.updatePatientExamineOrder(putStatusRequestVo.getExamList());
        	labService.recordCallApiLog(
        		putStatusRequestVo.getLab_id(), 
        		jsonString, 
        		PatientExamineLogEnum.Receive.getCode(),
        		"OK");
        
		} catch (Exception ex) {
			ex.printStackTrace();
			return new JwtResponseResult("fail", messageHelper, "common.error", ex.getMessage());
		}
		return new JwtResponseResult("ok", null);
	 }

***如果直接VO.toString()的話, 是沒辦法列印成做成JSON結構的

沒有留言:

張貼留言

20251217 短期三個月目標

目前因為有傳福音需求 如果看起來不好, 見證也會不好的 1 體型優化: 再瘦10公斤(97->87) 2 早睡: 目前12:00-7:00->11:00-6:00(每早起來跑步) 3 吃健康: 每周4天健康餐(午晚)+水果 4 保養皮膚 5 固定週六進修辦讀書會 6 ...