Spring Boot JPA MySQL 入库MySQL数据库时中文乱码的解决办法

问题表述:

       上篇博客中抒写了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

猜你喜欢

转载自blog.csdn.net/weixin_37838171/article/details/84582659