You will not only encounter this situation in Hibernate, but also in mainstream php. If your database encoding is different from the page encoding, chinese garbled characters may occur.
Today, we encountered garbled characters when using Hibernate to insert data into MySQL. During the specific performance, I used UTF-8 to print Chinese characters before insertion, then set MySQL Workbench to uft-8 to display the data queried from MySQL is correct, however, when I insert the data in the code into the database and then perform the query, the query results are garbled and I don't know how to set it because the two UTF-8 files are okay, the problem must be in the middle of the connection. The search results are found everywhere on the Internet.
We only need to set it in the hibernate. property Or hibernate. cfg. xml (depending on how you connect to the database) file.
My connection statement was originally written as follows:
The Code is as follows: |
Copy code |
<Property name = "connection. url"> Jdbc: mysql: // localhost: 3306/tablename </Property> |
Then I changed it:
The Code is as follows: |
Copy code |
<Property name = "connection. url"> Jdbc: mysql: // localhost: 3306/tablename? UseUnicode = true & characterEncoding = utf8 </Property> |
I found that it still does not work. I used to change it to this:
The Code is as follows: |
Copy code |
<Property name = "connection. url"> <! [CDATA [ Jdbc: mysql: // localhost: 3306/tablename? UseUnicode = true & characterEncoding = utf8 ]> </Property> |