grails 解决emoji标签存入mysql

  1. domain将存储emoji属性类型设置位byte[]
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执行insertcaozuo

   def insertUser(){
        UserTest user = new UserTest()
        user.name = "雪雪 👑 Smile "
        if (!user.save()) {
            throw new ValidationException("添加用户失败", user.errors)
        }
    }

       3.返回json数据

[{
"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设置没有效果

      2.emoji标签👑是4字节,mysql的utf8的最大支持3字节,utf8mb4支持4字节,但设置数据库连接字符编码格式utf8mb4没有效果

posted @ 2015-05-05 22:02  muzi1994  阅读(322)  评论(0编辑  收藏  举报