SPRINGMVC + Mybatis bug Debug SQL correct, check database but return null

Source: Internet
Author: User

I got a bug today.

Background is a SPRINGMVC + MyBatis project, mapper file wrote a SQL roughly equivalent to select a from TableA where B = "123" Level

And then no matter what's in the database, it will return null.

The first response is that the SQL statement is wrong, such as the 1 and lowercase l mixed up, the parameters passed to SQL have strange spaces and so on

Then open debug log to get the PreparedStatement and corresponding parameters passed to SQL

Copy it to the console, check it out, execute it, return the result is correct, it's not a SQL problem.

Since it's not a SQL problem, you have to debug the code.

Since PreparedStatement SQL can be printed out, it does not add breakpoints from the business logic, directly to the PreparedStatement class to find the Execute method, the last breakpoint began a single step

Single-step time to find strange phenomenon, theIDE hints that the source code and class file corresponding row inconsistency, one-step debugging time code is jumping between the peers, and I am monitoring the variable reported that the variable does not exist in a class error

So the suspicion is a reference conflict.

Next, make sure that you quoted the strange class and use the following method to locate the PreparedStatement position.

     Public StaticString WHERE (FinalClass CLS) {        if(CLS = =NULL)Throw NewIllegalArgumentException ("null Input:cls"); URL result=NULL; FinalString Clsasresource = Cls.getname (). replace ('. ', '/'). Concat (". Class")); FinalProtectiondomain PD =Cls.getprotectiondomain (); if(PD! =NULL) {            FinalCodesource cs =Pd.getcodesource (); if(cs! =NULL) result =cs.getlocation (); if(Result! =NULL) {                if("File". Equals (Result.getprotocol ())) {                    Try {                        if(Result.toexternalform (). EndsWith (". Jar") | |result.toexternalform (). EndsWith (". zip") ) Result=NewURL ("Jar:". Concat (Result.toexternalform ()). Concat ("!/"). Concat (Clsasresource)); Else if(NewFile (Result.getfile ()). Isdirectory ()) Result=NewURL (result, clsasresource); }                    Catch(Malformedurlexception ignore) {}}} }        if(Result = =NULL) {            FinalClassLoader Clsloader =Cls.getclassloader (); Result= Clsloader! =NULL?Clsloader.getresource (Clsasresource): Classloader.getsystemresource (Clsasresou        RCE); }        returnresult.tostring (); }

When you step into idea, press Alt+f8 to check the class with the Where method, and find that the JVM does not load what I think of the msql-java-connector-5.1.63, but instead loads the java-connector in an internal class library, Positioning to look at a bit is really a problem in writing, the reason has been found.

Finally to Maven dependency, open the dependency tree, find the POM entry to load this custom connector, configure the <exclusions>, and then restart the project to resolve.

SPRINGMVC + Mybatis bug Debug SQL correct, check database but return null

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.