Solve the coding problem of spring data jpa mysql table building

  • 2021-11-01 03:18:18
  • OfStack

UTF8 encoding using InnoDB engine

To create a configuration class:


import org.hibernate.dialect.MySQL5InnoDBDialect;
import org.hibernate.dialect.MySQLInnoDBDialect;
import org.springframework.stereotype.Component;
 
/**
 * Created by ZXing
 * QQ:1490570560
 */
public class MysqlConfig extends MySQL5InnoDBDialect {
    @Override
    public String getTableTypeString() {
        return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
    }
}

Add configuration to configuration file: (. yml)


spring:
  jpa:
     properties:
       hibernate:
         dialect: com.zxing.user.config.MysqlConfig  # Configure class full path 

jpa operation mysql garbled code and Chinese query error

If the fields in the MySQL table have Chinese, when you query and report javax. persistence. NonUniqueResultException, you need one line of results to return multiple lines of errors, and the final location is still caused by the reason that the coding of MySQL configuration is not specified in the configuration file.

Same as jpa operation MySQL garbled code is one kind of modification method

You need to specify the following configuration:


spring.datasource.url = jdbc:mysql://localhost:3306/videobase?characterEncoding=utf-8

Related articles: