Solve Springmvc+mybatis+mysql Chinese garbled problem _java

Source: Internet
Author: User

Recently used AJAX request SPRINGMVC background query MySQL database, the page shows the Chinese garbled

Initially configured as follows in MyBatis

<select id= "Querycontentbyid" Resulttype = "java.lang.String" parametertype= "String" >
    select text from News Where Id=#{o} 
</select>

Where table News's text field is BLOB type

This detected text value is always garbled in the console.

Then Google to find relevant Resulttype=blob related content, and then change it to Resulttype = "Java.util.Map", and

Byte[] B = (byte[]) map.get ("text");
string s = new string (b, "Utf-8");

Print out S, at this time the Chinese normal display, but the page is still garbled.

Therefore, the request for Ajax, then check the response header information, identified as follows

Content-typetext/html;charset=iso-8859-1

Since the database is unified for encoding as UTF-8, the response header information is modified

@RequestMapping (value = "/querycontentbyid", method = Requestmethod.get,produces = "Text/plain;charset=utf-8")
Public @ResponseBody string Querycontentbyid (@RequestParam ("id") string id) throws SQLException, unsupportedencodingexception {
  Map map = (map) Ndrservice.querycontentbyid (ID);
  Byte[] B = (byte[]) map.get ("text");
  string s = new string (b, "Utf-8");
  return s;
}

Let's take a look at another example of the problem

1, SPRINGMVC controller get is garbled:
(1) in Web.xml plus character set filter:

Copy Code code as follows:
<!--spring Character set filter--> <filter> <filter-name>SpringEncodingFilter</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> <filter-mapping> <filter-name>springencodingfilter</ Filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

(2) in JSP and other pages modified: Charset=utf-8 "and pageencoding=" UTF-8 "

2, controller read the correct Chinese, but after saving to the database into "?? ”

(1) Modify the database connection Jdbc_url=jdbc:mysql://localhost:3306/mybatistest?useunicode=yes&characterencoding=utf8 ("&": Representing "&" in an XML file

(2) Modify the database's character set to Utf-8: Open the MySQL root directory My.ini (mysql5.6 for My-default.ini, to copy a copy of it named My.ini), add (or modify) in the following specific location:

Copy Code code as follows:
[Mysqld]character-set-server=utf8 [client]default-character-set = Utf8[mysql]default-character-set = UTF8

That's not going to be a problem on my side.

Review:

Usually Chinese garbled problem is due to the wrong character encoding settings, I am here, whether the database or Java files, JSP files, are unified into UTF-8. The final problem was solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.