The web project saves the data to the database, the Chinese garbled, the solution process

first:

Reason for investigation:

Interrupt the point to see whether the garbled characters are already garbled before the database operation is performed, or the garbled characters are stored after the database operation.

The former solution:

Add in web.xml:

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param:>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在配置jdbc数据源的时候加上:
jdbc:mysql://127.0.0.1:3306/logistics?useUnicode=true&characterEncoding=UTF-8 
is garbled after the database operation, the solution:
log in to the database and execute the following sql:

show variables like 'character%';

result:

| character_set_client     | utf8                                                   |

| character_set_connection | utf8                                                   |

| character_set_database   | latin1                                                 |

| character_set_filesystem | binary                                                 |

| character_set_results    | utf8                                                   |

| character_set_server     | latin1                                                 |

| character_set_system     | utf8                                                   |

 

It means that the encoding format of character_set_database and character_set_server is set incorrectly when installing the database.

solve:

Find the my.cnf file in the mysql installation path and add the following two lines under [mysqld]:

character_set_server=utf8 
init_connect='SET NAMES utf8'

Check again, problem solved:

| character_set_client     | utf8                                                   |

| character_set_connection | utf8                                                   |

| character_set_database   | utf8                                                   |

| character_set_filesystem | binary                                                 |

| character_set_results    | utf8                                                   |

| character_set_server     | utf8                                                   |

| character_set_system     | utf8        

 

Reprinted from: https://www.cnblogs.com/tom-plus/p/5701741.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324597099&siteId=291194637