1. View MySQL character encoding
>show variables like ' character% '; #执行编码显示
Where character_set_client,character_set_results,character_set_connection three running variables is the key to garbled
2. Modify MySQL encoding format to UTF8
Edit My.ini, (note: Not Utf-8, also pay attention to case)
Locate the client configuration [clients] below to add
# # # Default character set is UTF8
Default-character-set=utf8
When you find [mysqld] Add
# # # Default character set is UTF8
Default-character-set=utf8
# # # (set the MySQL database to use UTF8 encoding to let MySQL database run for UTF8)
init_connect= ' SET NAMES UTF8 '
After modification, restart MySQL, check show variables like ' character% ';
3. When you create hibernate database at the same time, you need to display the setting database encoding is UTF8. Example:
Create DATABASE Daycode default Charset=utf8;
4, to finish these two steps or not, you need to modify Hibernate configuration file Hibernate.cfg.xml, configure the Hibernate.connection.url property in the configuration file. Example:
<property name= "Hibernate.connection.url" >
<! [cdata[jdbc:mysql://localhost:3306/daycode?useunicode=true&characterencoding=utf8]]>
</property>
Note: This string cannot be written as Jdbc:mysql://localhost:3306/daycode?useunicode=true&characterencoding=utf8, or a compile error will occur, and the error message will be & connector changed to;.
After setting these, the garbled problem is solved.
Hibernate connection MySQL database garbled related problems