Mysql save emoji expression

Mysql save emoji expression

  • Emoji in our lives really are becoming more common, and almost every time a message is not the time to take a Emoji, always felt something was lacking, it seems vapid text has been unable to carry our rich feelings. For us developers, how Emoji MySql database into or taken out, it becomes a must master the skill.

  • Emoji is a graphical symbol that can reflect some very intuitive meaning of the text. It reminds me of the times of ancient hieroglyphs

Here Insert Picture Description

  • Bad
    if we Emoji expression directly into the database, usually following error.
    Here Insert Picture Description

mysql database can not be modified to utf8mb4 coding, but want to keep emoji expression of how to do?
Because the character encoding of the database is generally utf8 (range of supported coding \ u0000- \ uFFFF), and coding range Emoji where is \ u1F601- \ u1F64F, beyond the boundaries of the MySql.

How to solve this problem?

A, utf8mb4

MySql character set can be adjusted by utf8 as utf8mb4. utf8mb4 MySql is added after a version 5.5.3 encoding, four bytes for compatibility with Unicode (including Emoji).

In theory, utf8mb4 utf8 is a superset, which is the most bytes mean mb4 4, will modify the character set is "utf8mb4", will not have an existing utf8 code reading any problems.

But usually this way is not the optimal solution, because the application layer will also need to make the following adjustments MySql connections:

jdbcUrl = jdbc:mysql://localhost/jfinal_demo?characterEncoding=utf8mb4&useSSL=false&zeroDateTimeBehavior=convertToNul
由原来的 characterEncoding=utf8 调整为 characterEncoding=utf8mb4。

Two, EmojiConverter

More friendly solutions should be stored Emoji as a string, then converted into Emoji when taken out, so that is compatible with all versions of the database.

I find such a library --EmojiConverter on GitHub, it can easily be converted to a string Emoji alias, also supports convert this alias Emoji.

1) was added in pom.xml file EmojiConverter

<dependency>
	<groupId>com.github.binarywang</groupId>
	<artifactId>java-emoji-converter</artifactId>
	<version>0.1.1</version>
</dependency>

2) call toHtml () method before it is stored Emoji conversion

EmojiConverter emojiConverter = EmojiConverter.getInstance();
String html = emojiConverter.toHtml(keywords.getContent().trim());
// JFinal 的保存方式
Record record = new Record().set("content", html)
Db.save("keywords", record);

For example, among the stored content to be included Emoji a point of praise.
Here Insert Picture Description
mysql database can not be modified to utf8mb4 coding, but want to keep emoji expression of how to do?
Then the content after the turn by emojiConverter.toHtml () is like? A code point:, debug time screenshot below.
Here Insert Picture Description
mysql database can not be modified to utf8mb4 coding, but want to keep emoji expression of how to do?
In this case, the contents stored in MySql is a normal string, the encoding still be utf8.

3) Emoji display when calling toUnicode () method to format it

String unicode = emojiConverter.toUnicode(content);
outMsg.setContent(unicode);

Original: https: //juejin.im/post/5db696b76fb9a0203b234c22 utm_source = gold_browser_extension?

Published 20 original articles · won praise 0 · Views 262

Guess you like

Origin blog.csdn.net/white_zzZ/article/details/103349210