仅在Grails数据库表中使用UTF-8

时间:2022-04-22 16:51:49

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?

将Grails 1.0.4与MySQL一起使用时,自动生成的数据库表的字符集似乎默认为ISO-8859-1。我宁愿将所有内容存储为纯UTF-8。那可能吗?

From the auto-generated database definitions:

从自动生成的数据库定义:

ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

Note the "latin1" part.

注意“latin1”部分。

4 个解决方案

#1


12  

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:

大多数MySQL安装默认为latin1,因此如果没有另外指示,驱动程序将使用默认值。应该在连接URL中添加charset指令就足够了,例如:

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).

为此,MySQL Connector JAR必须是版本5(3.x将无法工作)。

Source: MySQL Reference manual: Using Character Sets and Unicode

来源:MySQL参考手册:使用字符集和Unicode

#2


6  

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

还必须在创建期间为数据库设置默认字符编码

CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8;

#3


3  

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

显然你必须通过为Hibernate定义自己的自定义SQL方言(Grails用于持久性)来破解你的方法。

#4


1  

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

您必须创建数据库使用排序规则utf8_genenal_ci。然后将此字符串添加到连接字符串:

useUnicode=true&characterEncoding=utf-8"

#1


12  

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:

大多数MySQL安装默认为latin1,因此如果没有另外指示,驱动程序将使用默认值。应该在连接URL中添加charset指令就足够了,例如:

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).

为此,MySQL Connector JAR必须是版本5(3.x将无法工作)。

Source: MySQL Reference manual: Using Character Sets and Unicode

来源:MySQL参考手册:使用字符集和Unicode

#2


6  

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

还必须在创建期间为数据库设置默认字符编码

CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8;

#3


3  

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

显然你必须通过为Hibernate定义自己的自定义SQL方言(Grails用于持久性)来破解你的方法。

#4


1  

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

您必须创建数据库使用排序规则utf8_genenal_ci。然后将此字符串添加到连接字符串:

useUnicode=true&characterEncoding=utf-8"