SpringBoot+MyBatisPlus+MySQL cannot store (save) emoji expressions problem solved

1. I didn’t know the difference between utf8 and utf8mb4 during the learning process, and I didn’t know much about it. It wasn’t until recently that the database encoding I set was all utf8 and I discovered the problem. I couldn’t save emoticons! ! ! The whole person was immediately dumbfounded. Later I learned that utf8 is 3 bytes and cannot store expressions, but utf8mb4 can. Without further ado, it was immediately rectified. The next scene was a headache.

2. My mysql version is 5.7.40. First, follow the tutorials in some articles to modify the database encoding to utf8mb4 (note: the SQLyog tool is used)

Then continue to modify the character set format of the table to utf8mb4 (Note: If you do not have any suggestions on using tools, check the character set commands for modifying database and table characters)

Then the MySQL dependency package of my pom.xml is as follows

<dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <scope>runtime</scope>
</dependency>

Then the yml file database connection is as follows

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mcy_wechat_data?useSSL=false&severTimezone=GMT%2b8
    username: name
    password: password

Okay, I thought this would be fine, but I still got an error, as follows

        Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...' for column 'CONTENT' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)

My mentality collapsed again, and I kept looking for many articles to fix it. Later I discovered that although the character set of the database and table had been changed, what about the fields (column attributes)? Sure enough, after checking, I found that the character set of the field is still utf8. The command to view the character set of the field is as follows:

SHOW FULL COLUMNS FROM table_name;

I am really speechless this time. I changed the character set of the table, but the fields did not change accordingly. I used the command to change it here, as follows

ALTER TABLE 表名 MODIFY 字段名 类型(如varchar(255)) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

After making the changes, check the following. The others here are utf8 and the content is utf8mb4.

As expected, no error was reported, and I happily saved the emoticon package. As shown below, my content sent the emoticon package to the database in the form of a string.

Guess you like

Origin blog.csdn.net/m0_59799878/article/details/132962717