如题,底层框架就是springmvc+mybatis+mysql,想要实现前端excel文件导入后台数据库功能,测试用例目前在服务端已经成功存储文件并通过poi解析拿到了文件中的内容,但是在插入数据库后,数据库查询出中文显示为??,通过控制台打印已经可以知道在执行mybatis映射的dao类方法之前,Dao.addUser(username,pwd),username和pwd在控制台输出均为中文,但是数据库中查询出来中文就变成了??,求各位大神指教,困扰我一天了对了,我是MacOs系统
如下是我环境的配置,供大神参考:mysql > show variables like 'character%';
character_set_client     | utf8                                                      |
character_set_connection | utf8                                                      |
character_set_database   | utf8                                                      |
character_set_filesystem | binary                                                    |
character_set_results    | utf8                                                      |
character_set_server     | utf8                                                      |
character_set_system     | utf8                                                      |
character_sets_dir       | /usr/local/mysql-5.6.38-macos10.12-x86_64/share/charsets/数据库和表的编码均为utf-8。这是我mybatis的配置
url=jdbc:mysql://localhost:3306/gts?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=true还有什么地方会影响到中文传输,请大神指教。

解决方案 »

  1.   

    可以这样测试一下
    1、先不解析xls文件,在代码中写死一部分中文,插入数据库中,看看是?还是正常的中文
    2.1、如果第一步结果是中文,说明解析后得到的内容编码有问题,可以转一下编码再入库
    2.2、如果第一步是问号则说明是配置或者环境问题,再继续排查
      

  2.   

    1:spring-mvc.xml里面先配置一下
    <mvc:annotation-driven>
    <!-- 这里处理传入和传入数据的乱码问题 -->
    <mvc:message-converters register-defaults="true"> 
             <bean class="com.diao.characterencode.CharacterEncode"/> 
         </mvc:message-converters> 
    </mvc:annotation-driven>
    2:/**
     * 处理传入和传出的编码问题
     * @author luck
     *
     */
    public class CharacterEncode extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
        private final List<Charset> availableCharsets; 
     
        public CharacterEncode() { 
            this(DEFAULT_CHARSET); 
        } 
     
        public CharacterEncode(Charset defaultCharset) { 
            super(new MediaType("text", "json", defaultCharset), MediaType.ALL); 
            this.availableCharsets = new ArrayList<Charset>(Charset 
                    .availableCharsets().values()); 
        } 
     
        @Override 
        protected boolean supports(Class<?> clazz) { 
            return String.class.equals(clazz); 
        } 
     
        @Override 
        protected String readInternal(Class<? extends String> clazz, 
                HttpInputMessage inputMessage) throws IOException, 
                HttpMessageNotReadableException { 
            MediaType contentType = inputMessage.getHeaders().getContentType(); 
            Charset charset = contentType.getCharSet() != null ? contentType 
                    .getCharSet() : DEFAULT_CHARSET; 
            return FileCopyUtils.copyToString(new InputStreamReader(inputMessage 
                    .getBody(), charset)); 
        } 
     
        @Override 
        protected void writeInternal(String t, HttpOutputMessage outputMessage) 
                throws IOException, HttpMessageNotWritableException { 
            MediaType contentType = outputMessage.getHeaders().getContentType(); 
            Charset charset = contentType.getCharSet() != null ? contentType 
                    .getCharSet() : DEFAULT_CHARSET; 
            FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(), 
                    charset)); 
        } 
     
        protected List<Charset> getAcceptedCharsets() { 
            return this.availableCharsets; 
        } 
         
        @Override 
        protected Long getContentLength(String s, MediaType contentType) { 
            if (contentType != null && contentType.getCharSet() != null) { 
                Charset charset = contentType.getCharSet(); 
                try { 
                    return (long) s.getBytes(charset.name()).length; 
                } catch (UnsupportedEncodingException ex) {                 
                    throw new InternalError(ex.getMessage()); 
                } 
            } else { 
                return null; 
            } 
        } }
      

  3.   

    在 Web.xml 中加入下面的代码。<filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
      

  4.   

    你现确定你查询出来是乱码?这个查询是指的什么?是直接在数据库中看到的,还是你通过代码取出来的?
    如果数据库中就是乱码?那就要看看数据库的编码
    如果是代码取出来的,那么你是以什么样的方式现实的,是不是显示的页面编码不对,可以看看浏览器的url的请求,返回值是不是乱码
      

  5.   

    你应该在创建mysql数据库的时候选错了编码,改下数据库编码就行了
      

  6.   

    谢谢大家的分析,都有尝试,但是无果。最后发现是navicat的锅,当初在创建连接的时候为了和mysql统一编码特地选择了utf-8,但是中文会乱码,删除连接重新创建编码格式选择自动后,却能够正确读写中文,还不知道是为什么,总之是解决了,谢谢各位大佬