In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to solve the problems caused by Mybatis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Background
Some time ago in my technology group, we discussed why UserMapper.java is an interface, there is no specific implementation class, and we can directly call its methods?
On this question, I have interviewed some people before, and many of them answered like this:
1. My leader asked us to use Mybatis, and everyone used it this way and just used it (never thought of it, anyway).
two。 Although I don't know exactly how to achieve it, I think it must be. (some conjectures are omitted here), but it's not true, is it. Omit some seemingly unintelligible things again.
3. Implemented using dynamic proxies (and then nothing more).
For the above three answers, there is no need for us to talk about the first two.
But the third answer, it is necessary to ask: what are the ways to implement dynamic agents? which one is used by Mybatis?
If you can still answer this question, you will continue to ask: can the big method in UserMapper.java be overloaded?
If you can answer the above questions, there is no need to read on, it is no longer suitable for you.
Analysis of problems
Let's take a look at a picture, where the code is the demo we wrote earlier:
Why can an interface be bound to a xml file? That's what we're going to talk about today.
Maybe many friends are not familiar with ibatis. Before 2010, there was no Mybatis. After that, ibatis has become the present Mybatis. If you are interested, you can see the package directory in Mybatis.
Ibatis is still in this package directory, and the author of ibatis now works for Tencent to develop League of Legends LOL.
If there is a small friend of Tencent, you can inquire, ha, the boss is by your side. Let's get back to the point.
The Mapper layer is now done in the form of interfaces in Mybatis, but in the ibatis era there must be implementation classes. I remember that in 2012, ibatis,Dao (Mapper) was used with implementation classes.
Let's take a look at how it is done in Mybatis.
Use case
Continue to use the code from our previous section.
Controller
In the service implementation class
Hit a breakpoint and start the project using debug mode. And visit:
Http://localhost:9002/test
UserMapper=org.apache.ibatis.binding.MapperProxy@6da21078
It is found that Mybatis generates a proxy object for UserMapper.java, and the name indicates that it is a JDK dynamic proxy.
About dynamic agents, here I recommend an article I've written before:
Https://gitbook.cn/m/mazi/activity/5d44e35e4fbf44126135c292?sut=c93c00a03b4f11eba07ad99b4dfbdab0&utm_source=chatweixinshare
In fact, it is almost back to the era of ibatis, but the proxy class generated by dynamic proxy in Mybatis is not developed by us, but generated by JDK dynamic proxy.
Let's also use the JDK dynamic proxy to simulate it.
Public class MapperProxy implements InvocationHandler {@ SuppressWarnings ("unchecked") public T newInstance (Class clz) {return (T) Proxy.newProxyInstance (clz.getClassLoader (), new Class [] {clz}, this) } @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {if (Object.class.equals (method.getDeclaringClass () {try {/ / such as hashCode (), toString (), equals (), etc., to point target to the current object this return method.invoke (this, args) } catch (Throwable t) {}} / / return new User ((Integer) args [0], Tian Weichang, 22);}}
Write another test class
Import com.tian.mybatis.entity.User; import com.tian.mybatis.mapper.UserMapper; public class TestProxy {public static void main (String [] args) {MapperProxy proxy = new MapperProxy (); UserMapper mapper = proxy.newInstance (UserMapper.class); User user = mapper.selectById (999); System.out.println (user); System.out.println (mapper.toString ());}}
Output
User {id=999, userName=' Tian Weichang', age=22, gender=null} com.tian.mybatis.proxy.MapperProxy@39a054a5
This is the underlying implementation principle of Mybatis automatic mapper Mapper.
But in Mybatis, it's far from that simple, but that's what it is.
Let's take a look at this process in Mybatis.
Can the methods in the interface Mapper be overloaded?
Similar to the following:
Public User getUserById (Integer id)
Public User getUserById (Integer id, String name)
Answer: no
Because the package+Mapper+method full limit name is used as the key in Mybatis, go to the xml to find a unique sql to execute.
Similar to: key=com.tian.mybatis.UserMapper.getUserById, then, overloading the method will lead to contradiction.
For Mapper interfaces, Mybatis forbids method overloading (overLoad).
There is a resolveMappedStatement method in SqlCommand in the static inner class of the MapperMethod class.
There is an attribute in Configuration, that is, when the project starts, the information in Mapper.xml will be parsed into this attribute and put into Map with the specified namespace+method as key. Later, we call a method of the dynamic class of the Mapper API and then go to map to get it.
Protected final Map mappedStatements
Is to use the full path name of the class. Method is stored in Map as a key.
This is the end of "how to solve the problems caused by Mybatis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 0
*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.