There Mybatis this operation? Why look at the source code analysis

Many people have a doubt, like to ask why the interview principle, ask the source, but no access to the actual work, that is, we often say, the interview made rockets, tighten the screws go in. I have a lot of friends around also asked me, my answer to them is if you do not look at the source code, but not the principle, how do you solve a problem they gave me the answer basically two words:? "Search."

Indeed, the work of most of the problems can be solved by copying the error message search, and now more and more frameworks, fight building blocks of programming plus search engines, so that more and more people had developed a very easy the illusion of things. I have always wanted to give a little search found less than, depending on the source code in order to make sense of the reasons examples.

There Mybatis this operation? Why look at the source code analysis

This happened happened in August last year, I have a good friend asked me such a question, he said.

Why do I pass an empty string, but use if the label Mybatis judgment turned out to be an empty string == 0 founded
There Mybatis this operation? Why look at the source code analysis

From our knowledge, the empty string and a number equal to 0 is impossible. So my first reaction was that he was not a usage right? Or is his business to interfere with other parts of the code? So I decided to write the most simple demo for testing. below

There Mybatis this operation? Why look at the source code analysis

And then outputs the results are as follows:

There Mybatis this operation? Why look at the source code analysis

Surprisingly found that if this really empty string and tag numbers become equal to 0 is determined.

Here I am not trying to cheat you encounter this problem, frankly, the first reaction is certainly not the source code to see you, of course, is to open a browser search. The main direction of our search there are two, one is to determine the principles mybatis if the label, Why mybatis if a label is an empty string and 0 are equal. it was found, did not we want to find the answer (you can search on their own).

Of course, although there is no satisfactory answer to the search, but we found another example.

I believe the code we project similar to this judgment should there have been many.

<if test="uid != null and uid != '' "></if>

We usually develop, many of my colleagues are like copy paste!

There Mybatis this operation? Why look at the source code analysis

So without thinking copy paste it in the end there will be any problem, we look at the following example

There Mybatis this operation? Why look at the source code analysis

这个判断虽然是复制黏贴一把梭出来的,但是从我们的认知上来说,这个对象确实不是null,也不等于空字符串,所以这个判断应该是true的,但是运行结果如下:

There Mybatis this operation? Why look at the source code analysis

果然,这个又颠覆了我们的认知,但是如果你遇到的是案例2这种情况还比较好搜索,还是能搜到解决方案,如下图

There Mybatis this operation? Why look at the source code analysis

其实这两个案例都是一个问题,那就是这个if标签,把0和空字符串判断成了相等.

这个时候要敲黑板划重点了,俗话说一朝被蛇咬十年怕井绳,虽然第二个例子我们有了解决方案,但是这些解决方案都是治标不治本,如果我们没弄懂这其中的原理,那么你心里永远是有一块疙瘩的.你害怕下一次,又有奇奇怪怪的事情发生,只有弄懂原理,才能从根源解决问题,也就是解决一类问题,而不是某一个问题.

同时我也认识到,机会来了,终于找到一个为什么要看源码的比较合适例子了

分析源码

由于链路比较长.这里就不把debug过程展示了(对Mybatis执行流程不熟悉的,可以看看我之前的别怕看源码,一张图搞定Mybatis的Mapper原理,然后顺着执行流程debug

我们拿第一个例子来分析,因为两个案例其实遇到的问题都是一样的.

There Mybatis this operation? Why look at the source code analysis
There Mybatis this operation? Why look at the source code analysis
There Mybatis this operation? Why look at the source code analysis
There Mybatis this operation? Why look at the source code analysis
There Mybatis this operation? Why look at the source code analysis

如果上面看不懂,我这里可以简单描述一下:

首先他会获取两个判断对象的类型,当拿一个字符串和一个数字判断的时候,因为类型不一样嘛,当mybatis发现,这个字符串是可以转换成数字的,那么就会把这个字符串转成数字,然后再和这个数字判断.那么问题就来了,这个空字符串会转换成什么数字呢?

从源码的这个

return s.length() == 0 ? 0.0D : Double.parseDouble(s);

就可以看出,这个空字符串,是会被转成0的.所以现在一切豁然开朗.

但是源码是看了,问题还是没有解决啊.他里面其他类型判断的源码这么多,不可能全部看完,时间也不允许啊,万一还有其他坑怎么办.由此可见,只看源码还是不够的,还需要一些解决问题的分析思路,这就是为什么网上源码解析的文章这么多。

解决问题的思路

While we looked at the source code, we also know the rules and we want to determine, is the discrepancies. But the key is, how to solve the problem thing. A lot of people first reaction was that it would modify the source code chant. But frankly, you look at a small piece of source code so hastily modified, can determine control is maintained, certainly will not lead to other problems? so the direction of thinking to solve the problem, note that I said is the direction, it is very important.

If the three characteristics of object-oriented when it comes, then it surely is not unfamiliar. Encapsulation, inheritance, polymorphism, but the five principles of object-oriented. So we might be a little strange. That is

  • Single Responsibility
  • Open Closed Principle
  • Reliance resulting in principle
  • Interface Segregation Principle
  • Liskov Substitution Principle

Then I said about the principle of opening and closing, citing what Baidu know there is this relatively brief description of

Open Closed Principle, the core idea is: software entities should be scalable, and can not be modified. That is, open for extension, closed for modification.

If you understand the design patterns, then it is to understand what this means. If you do not understand this, you can look at the Westward design patterns this book, is how to introduce the policy design patterns. Simply, it is Well, if you are used to determine if, a demand increase so much, you have to add a multi-else if, that is to modify the code. but good design should be, more than the increase a demand, I only need to add a implementation class, which is a strategy. (If you do not know the students, it is recommended to see design mode), in fact, SPI, also contains the idea of ​​this principle of opening and closing.

Mybatis such a good framework. People naturally understand the five principles of object-oriented, it will certainly follow this principle. In other words, he will provide a way for you to add a multi-class, then the class inside, to customize this if the judge rules.

solution

We customize a class, for example, I named it FeiChaoOgnl

There Mybatis this operation? Why look at the source code analysis

Then we come to this wording

There Mybatis this operation? Why look at the source code analysis

Then we look at the operation

There Mybatis this operation? Why look at the source code analysis
There Mybatis this operation? Why look at the source code analysis

FeiChaoOgnl as long as the method for determining the full complement, according to this wording, even copy paste a shuttle, is also greatly reduced the risk of problems

Guess you like

Origin www.cnblogs.com/CQqf2019/p/10983557.html