UTF-8 only in Grails database tables

2019-03-16 10:02发布

When using Grails 1.0.4 together with a MySQL the charsets of the auto-generated database tables seem to default to ISO-8859-1. I'd rather have everything stored as pure UTF-8. Is that possible?

From the auto-generated database definitions:

ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

Note the "latin1" part.

4条回答
干净又极端
2楼-- · 2019-03-16 10:37

Most MySQL installations default to latin1, so if not instructed otherwise, the driver will go by the default values. It should suffice to prepend the charset directives in the connection URL, for example:

jdbc:mysql://localhost/databaseName?useUnicode=yes&characterEncoding=UTF-8

For this to work, the MySQL Connector JAR must be of version 5 (3.x will not work).

Source: MySQL Reference manual: Using Character Sets and Unicode

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-16 10:44

also the default character encoding must be set for the database during creating

CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8;
查看更多
可以哭但决不认输i
4楼-- · 2019-03-16 10:48

You must create database use collation utf8_genenal_ci. And then you add this string to connection string:

useUnicode=true&characterEncoding=utf-8"
查看更多
对你真心纯属浪费
5楼-- · 2019-03-16 10:49

Apparently you have to hack your way around by defining your own custom SQL dialect for Hibernate (which Grails uses for persistence).

查看更多
登录 后发表回答