class UserTest { byte[] nameBytes //存儲emoji表情字段 Date dateCreated //grails 時間魔性字段 insert自動獲取當前系統時間 Date lastUpdated //grails 時間魔性字段 update 自動獲取當前系統修改時間 static constraints = { } //自定義get、set方法 轉換byte[]和String def getName() { return new String(nameBytes, "UTF-8") } def setName(String name) { this.nameBytes = name.bytes } }
2.Service執行insertcaozuojava
def insertUser(){ UserTest user = new UserTest() user.name = "雪雪 👑 Smile " if (!user.save()) { throw new ValidationException("添加用戶失敗", user.errors) } }
3.返回json數據mysql
[{ "class": "wxproduct.UserTest", "id": 1, "dateCreated": "2015-05-05T13:49:18Z", "lastUpdated": "2015-05-05T13:49:18Z", "nameBytes": //mysql數據存儲的byte[] [-23, -101, -86, -23, -101, -86, 32,//空格字節 -16, -97, -111, -111, 32, 83, 109, 105, 108, 101, 32, -18, -127, -120]}, "雪雪 👑 Smile " //byte[]轉換String ]
注:1.mysql5.3以上版本支持utf8mb4編碼格式,但通過測試 有時mysql設置utf8mb4設置沒有效果sql
2.emoji標籤👑是4字節,mysql的utf8的最大支持3字節,utf8mb4支持4字節,但設置數據庫鏈接字符編碼格式utf8mb4沒有效果數據庫