In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant information to solve the problem of Chinese garbled code in springmvc+mybatis+mysql in java. Friends who need it can refer to it.
Recently, ajax was used to request springmvc backend to query mysql database, and the page showed garbled codes in Chinese.
The initial configuration in mybatis is as follows
Select text from News where id=# {o}
The text field of table News is of type blob
The text value thus found is always garbled in the console.
After that, google found nothing about resultType=blob, so it was changed to resultType= "java.util.Map", and
?
one
two
Byte [] b = (byte []) map.get ("text")
String s = new String (b, "utf-8")
Print out s, Chinese is displayed normally at this time, but the page is still garbled.
Since this is an ajax request, check the response header information and find out as follows
?
one
Content-Typetext/html;charset=ISO-8859-1
Because the database is coded 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.
1. The Controller of SpringMVC is garbled:
(1) add a character set filter to web.xml:
The copy code is as follows:
SpringEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true SpringEncodingFilter / *
(2) modify: charset=UTF-8 "and pageEncoding=" UTF-8 "on JSP and other pages
2. Controller reads the correct Chinese, but after saving it to the database, it becomes "?"
(1) modify the database connection jdbc_url=jdbc:mysql://localhost:3306/mybatistest?useUnicode=yes&characterEncoding=UTF8 ("&": represent "&" in the xml file)
(2) modify the character set of the database to utf-8: open my.ini under the root directory of mysql (mysql5.6 is my-default.ini, copy a copy of it to be named my.ini), and add (or modify) in the following specific location:
The copy code is as follows:
[mysqld] character-set-server=utf8 [client] default-character-set = UTF8 [MySQL] default-character-set = utf8
There will be no problem with this setting on my side.
Summary:
Usually, the problem of Chinese garbled code is caused by incorrect character coding settings. Here, no matter it is the database or java file or jsp file, it is unified into UTF-8. Finally, the problem was solved.
1. Use Alibaba Druid connection pool (efficient, powerful and scalable database connection pool, monitor database access performance, support Common-Logging, Log4j and JdkLog, monitor database access)
two。 Provide high concurrency JMS message processing mechanism
3. All functional modularization, all module service, all service atomization, provide an extensible service model, so that the program runs stably and never downtime.
4. Provides Wink Rest and Webservice services, so it can be deployed as an independent service platform
Framework integration:
Springmvc + Mybatis + Shiro (permission) + REST (Service) + WebService (Service) + JMS (message) + Lucene (search engine) + Quartz (scheduled scheduling) + Bootstrap Html5 (support for PC, IOS, Android)
Introduction to the framework:
Project Maven construction, real large-scale Internet architecture, to achieve high concurrency, big data processing, the whole project uses customized service ideas, to provide modular, service-oriented, atomized solution, the functional modules will be split, can be shared to all projects. The architecture adopts distributed deployment architecture, and all modules are split, so that the project is absolutely decoupled and stability trumps everything.
Continuous integration:
1. My to-do workflow service (providing Webservice services)
two。 My to-do workflow integrates JMS message services (supports high concurrency and can support thousands of system integration)
3. My tasks provide Rest services, complete daily work management, and dynamically generate my tasks, cycle tasks, scheduled mail reminders to complete tasks, etc., through the scheduled scheduling platform.
4. File upload, multithreaded download service, sending mail, short message service, department information service, product information service, information release service, my subscription service, my task service, public link, my collection service, etc.
System module:
1. User Management:
User information management (add, delete, modify, user authorization, user column management, query, etc.)
User group management (add, delete, modify, user group column authorization, column authorization, query, user group personnel add query, etc.)
User role management (add, delete, modify, user role authorization, user role column information query settings, etc.)
two。 Article Management:
Column management: query infinite pole column tree, create infinite pole column tree classification (navigation column, picture list column, article list column, article content column, etc.), delete, modify column information.
Article management: create, delete, modify articles, multi-dimensional article queries, including published, unpublished, all articles, etc. Article rich text editor, article multi-file upload, article status control and so on.
3. System Settings:
Data dictionary management: support Chinese and English information, support unlimited level classification configuration, dynamic control whether available, etc.
Department information management: support unlimited level department information in Chinese and English to add, delete, modify operation, department list, tree heart query and so on.
Log management: system log list query, online viewing, online download, etc.
Route management: integrate Baidu map API, provide line query management function
Druid Monitor (monitoring): integrate Alibaba connection pool, provide online connection pool monitoring program, including: data source, SQL monitoring, URL monitoring, Session monitoring, Spring monitoring, etc.
Website information management: operate the website content through the system configuration file, including mail server configuration, company basic information configuration and so on.
4. Integrated REST service, which can be used as an independent service platform (provides a large number of examples and testing platforms, including: file uploads and downloads, email messages, departments, products, public connections, my favorites, my tasks, information release, etc.)
5. Integrated Quartz scheduling, which can be used as a timing scheduling platform (dynamically configure scheduling classes, scheduling time, so that programs automatically perform certain services)
6. Lucene search engine, which can index documents and support document content search, keyword search, highlighted keywords, etc., so that the information can be extracted and queried in milliseconds.
7. User setting functions: including modifying user information, changing passwords, sending messages, modifying personal pictures, viewing roles, viewing user groups, administrators modifying roles, users, user groups, and so on.
8. Integrate Webservice platform, including jaxws service, CXF framework, configure double encryption authority authentication. Make service integration more secure.
9. Bootstrap html5 provides two sets of foreground environments, including CMS and e-commerce sites, to make your development more concise.
Technical points:
1. Springmvc + Mybatis integration, SpringSecurity permission control, Spring AOP transaction processing.
2. Wink Rest service, Webservice service: jaxws, CXF, etc.
3. IO stream uploads and downloads files and operates with multiple threads
4. Send mail, configure mail server, send mail based on html, plain text format
5. MD5 encryption (login password verification, encryption, etc.), unified Session, Cookie management, unified CAPTCHA verification, etc.
6. Unified configuration of database connection pool
7. Quartz scheduled task integration (directly through configuration)
8. Httpclient cracked the CAPTCHA and logged in to Unicom's recharge platform.
9. Chinese characters, English split, can be used as document keyword search and so on.
10. Base64 image processing, which supports PC,Android,IOS
11. Service Socket, Client Socket communication technology (GPRS data acquisition has been done and used in the project)
twelve。 Provides a large number of tool classes that can be used directly
13. Maven project building, you can directly do the architecture, you can improve your learning ability and make you a true architect.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 234
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.