Recently with friends to build a project, with the Spring+mybatis+mysql.
I met a mybatis today. When inserting data into MySQL, Chinese is displayed as '??? ' Problem, take out the next.
For the database operation in the Chinese garbled, there are generally two kinds of situations:
- The database itself is set
- When connecting to a database, the encoding settings for JDBC
For the first case, you can view your own MySQL settings:
Show variables like '%character% '
My problem is the second case, which needs to be set in the MyBatis configuration file, before my connection field is:
<!--1. Data Source: Drivermanagerdatasource-- <bean id= "DataSource" class= " Org.springframework.jdbc.datasource.DriverManagerDataSource "> <property name=" Driverclassname "value=" Com.mysql.jdbc.Driver "/> <property name=" url "value=" jdbc:mysql://127.0.0.1:3306/uct "/> < Property name= "username" value= "root"/> <property name= "password" value= ""/> </bean>
Modified to:
<!--1. Data Source: Drivermanagerdatasource-- <bean id= "DataSource" class= " Org.springframework.jdbc.datasource.DriverManagerDataSource "> <property name=" Driverclassname "value=" Com.mysql.jdbc.Driver "/> <property name=" url "value=" jdbc:mysql://127.0.0.1:3306/uct?useunicode=true &characterencoding=utf-8 "/> <property name=" username "value=" root "/> <property name=" Password "value=" "/> </bean>
You can see that the Useunicode and characterencoding values are added after the URL of the connection, set to Utf-8.
One thing to note here is that you need to escape the ' & ' symbol in the XML configuration file, so write ' & ' here.
If it is in the configuration file, it cannot be escaped, should be: xxxx.jdbc.url=jdbc:mysql://127.0.0.1:8080:3306/uct?useunicode=true&characterencoding= UTF-8
Mybatis + Mysql inserting data when the Chinese garbled problem