1. 程式人生 > >字符編碼問題mysql

字符編碼問題mysql

framework 註冊 行修改 ref strac rac 就是 del bat

  2019-02-27 07:32:17.108 ERROR 21745 --- [nio-8086-exec-2] c.h.h.rest.configurer.WebMvcConfigurer : 接口[/flyfish/h5/user/register]出
  
  現異常 :[{}]
  
  org.springframework.jdbc.UncategorizedSQLException:
  
  ### Error querying database. Cause: java.sql.SQLException: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation ‘=‘

  
  ### The error may exist in com/hn/haoniu/businesscore/dao/UserMapper.java (best guess)
  
  ### The error may involve com.hn.haoniu.businesscore.dao.UserMapper.selectOne-Inline
  
  ### The error occurred while setting parameters
  
  ### SQL: SELECT id,invite_code,recommend_user_id,nick_name,phone,password,real_name,sex,area,user_type,robot_type,head_img,salt,lei_type,token_id,create_time,update_time,del_flag FROM user WHERE nick_name = ?
  
  ### Cause: java.sql.SQLException: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation ‘=‘
  
  ; uncategorized SQLException for SQL []; SQL state [HY000]; error code [1267]; Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation ‘=‘; nested exception is java.sql.SQLException: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation ‘=‘
  
  at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  
  at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  
  at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  
  at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) ~[mybatis-spring-1.3.1.jar:1.3.1]
  
  at org.mybatis.spring.SqlSessionTemplate$SqlSessionInte
  
  解決方法
  
  java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ‘=‘,意思是說字符編碼不一樣,不能進行比較,也就是說數據庫內部的編碼都不一樣,有的數據是latin1_swedish_ci,有的數據是utf8_general_ci,,因此解決此問題的核心就是將數據庫所有的編碼進行統一。
  
  1、查看數據庫編碼,使用sql語句:show variables like ‘character_set_%‘;
  
  正確的如下圖:
  
  如果編碼不對,可使用以下sql語句進行修改:
  
  set character_set_client=utf8;
  
  set character_set_connection=utf8;
  
  set character_set_database=utf8;
  
  set character_set_results=utf8;
  
  set character_set_server=utf8;
  
  set character_set_system=utf8;
  
  2、查看排序規則,使用sql語句:show variables like ‘collation_%‘;
  
  正確的如下圖:
  
  如果不對,可使用以下sql語句進行修改:
  
  set collation_connection=utf8;
  
  set collation_database=utf8;
  
  set collation_server=utf8;
  
  SpringBoot 的自動配置如此強大,比如我們經常使用的@Enable* 註解來開啟對某方面的支持。那麽@Enable* 註解的原理是什麽呢?
  
  一、@Enable* 註解與 @Import 註解之間的關系
  
  @Enable* 舉例:
  
  @EnableScheduling 開啟計劃任務的支持
  
  @EnableAsync 開啟異步方法的支持
  
  @EnableAspectJAutoProxy 開啟對 AspectJ 代理的支持
  
  @EnableTransactionManagement 開啟對事務的支持
  
  @EnableCaching 開啟對註解式緩存的支持
  
  等等
  
  我們觀察這些@Enable* 的源碼可以看出,所有@Enable* 註解都是有@Import的組合註解,@Enable* 自動開啟的實現其實就是導入了一些自動配置的Bean
  
  看下 Spring Boot Reference Guide原文
  
  You need not put all your @Configuration into a single class. The @Import annotation
  
  can be used to import additional configuration classes.
  
  您不需要把所有的 @Configuration 放到一個類中。@Import 註解可以導入額外的配置類。
  
  @Import 註解的最主要功能就是導入額外的配置信息
  
  二、 @Import 註解的用法
  
  官方介紹:
  
  * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
  
  * Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
  
  * {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
  
  * classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
  
  有以下三種使用方式
  
  1、直接導入配置類(@Configuration 類)
  
  @Target(ElementType.TYPE)
  
  @Retention(RetentionPolicy.RUNTIME)
  
  @Import(SchedulingConfiguration.class)
  
  @Documented
  
  public @interface EnableScheduling {
  
  }
  
  可以看到EnableScheduling註解直接導入配置類SchedulingConfiguration,這個類註解了@Configuration,且註冊了一個scheduledAnnotationProcessor的Bean,SchedulingConfiguration的源碼如下:
  
  @Configuration
  
  @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  
  public class SchedulingConfiguration {
  
  @Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
  
  @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  
  public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
  
  return new ScheduledAnnotationBeanPostProcessor();
  
  }
  
  }
  
  2、依據條件選擇配置類(實現 ImportSelector 接口)
  
  如果並不確定引入哪個配置類,需要根據@Import註解所標識的類或者另一個註解(通常是註解)裏的定義信息選擇配置類的話,用這種方式。
  
  ImportSelector接口只有一個方法
  
  String[] selectImports(AnnotationMetadata importingClassMetadata);
  
  AnnotationMetadata:用來獲得當前配置類上的註解
  
  例:
  
  @Target(ElementType.TYPE)
  
  @Retention(RetentionPolicy.RUNTIME)
  
  @Documented
  
  @Import(AsyncConfigurationSelector.class)
  
  public @interface EnableAsync {
  
  Class<? extends Annotation> annotation() default Annotation.class;
  
  boolean proxyTargetClass(www.jiahuayulpt.com) default false;
  
  AdviceMode mode() default AdviceMode.PROXY;
  
  int order() default Ordered.LOWEST_PRECEDENCE;
  
  }
  
  AsyncConfigurationSelector繼承AdviceModeImportSelector,AdviceModeImportSelector類實現ImportSelector接口 根據AdviceMode的不同來選擇生明不同的Bean
  
  public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {
  
  private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
  
  "org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";
  
  @Override
  
  @Nullable
  
  public String[] selectImports(AdviceMode adviceMode) {
  
  switch (adviceMode) {
  
  case PROXY:
  
  return new String[] {ProxyAsyncConfiguration.class.getName()};
  
  case ASPECTJ:
  
  return new String[] {ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
  
  default:
  
  return null;
  
  }
  
  }
  
  }
  
  3、動態註冊Bean(實現 ImportBeanDefinitionRegistrar 接口)
  
  一般只要用戶確切知道哪些Bean需要放入容器的話,自己可以通過spring 提供的註解來標識就可以了,比如@Component,@Service,@Repository,@Bean等。 如果是不確定的類,或者不是spring專用的,所以並不想用spring的註解進行侵入式標識,那麽就可以通過@Import註解,實現ImportBeanDefinitionRegistrar接口來動態註冊Bean。 比如:
  
  @Target(ElementType.TYPE)
  
  @Retention(RetentionPolicy.RUNTIME)
  
  @Documented
  
  @Import(AspectJAutoProxyRegistrar.class)
  
  public @interface EnableAspectJAutoProxy {
  
  boolean proxyTargetClass(www.myzx157.com) default false;
  
  boolean exposeProxy() default false;
  
  }
  
  AspectJAutoProxyRegistrar實現了ImportBeanDefinitionRegistrar接口,ImportBeanDefinitionRegistrar的作用是在運行時自動添加Bean到已有的配置類,通過重寫方法:
  
  public void registerBeanDefinitions(
  
  AnnotationMetadata importingClassMetadata,www.yongshiyule178.com BeanDefinitionRegistry registry);
  
  AnnotationMetadata 參數用來獲得當前配置類上的註解
  
  BeanDefinitionRegistry 參數用來註冊Bean
  
  源碼:
  
  @Override
  
  public void registerBeanDefinitions(
  
  AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
  
  AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
  
  AnnotationAttributes www.yaxingyule.cn enableAspectJAutoProxy www.leyouzaixian2.com =
  
  AnnotationConfigUtils.www.yingxionghui1.cn attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
  
  if (enableAspectJAutoProxy != www.taoyang2vip.com null) {
  
  if (enableAspectJAutoProxy.www.yongshi123.cn getBoolean("proxyTargetClass")) {
  
  AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
  
  }
  
  if (enableAspectJAutoProxy.getBoolean( www.feishenbo.cn/ "exposeProxy")) {
  
  AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);

字符編碼問題mysql