Spring Boot JPA MySQL 入庫MySQL資料庫時中文亂碼的解決辦法
阿新 • • 發佈:2018-11-28
問題表述:
上篇部落格中抒寫了Spring Data Jpa實體類自動建立資料庫表失敗解決即原因(踩過的坑),接著在操作JPA插入資料進資料庫時,發現數據庫的值為中文亂碼(??),需要注意一下的配置點,所以記錄一下,希望能幫到大家(#^.^#)。
解決問題:
1.設定資料庫的編碼為UTF-8
可以使用GUI介面操作將編碼改為UTF-8,這裡加上SQL視窗語句: ALTER SCHEMA `YourDBName` DEFAULT CHARACTER SET utf8 ;
2. 配置SpringBoot pom.xml檔案,配置如下:
在pom.xml檔案中,新增properties配置project.build.sourceEncoding
3.更改Springboot的application.properties配置檔案, 更改MySQL連結
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/YourDBName?characterEncoding=UTF-8注意後面需要新增?characterEncoding=utf-8. 這一步是關鍵. 具體配置如下:
######################################################## ### MySQL DataSource ######################################################## spring.datasource.url = jdbc:mysql://127.0.0.1:3306/YourDBName?characterEncoding=UTF-8 spring.datasource.username = root spring.datasource.password = **** spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 ######################################################## ### Java Persistence Api \u914D\u7F6E\u4FE1\u606F ######################################################## # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy #[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy] spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect