19.
19. One to one and one to many association queries?
<mapper namespace="com.lcb.mapping.userMapper"> <!--association One to one association query --> <select id="getClass" parameterType="int" resultMap="ClassesResultMap"> select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id} </select> <resultMap type="com.lcb.user.Classes" id="ClassesResultMap"> <!-- The field name of the entity class is mapped to the field name of the data table --> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="com.lcb.user.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> </resultMap>
<!--collection One to many association query --> <select id="getClass2" parameterType="int" resultMap="ClassesResultMap2"> select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id} </select> <resultMap type="com.lcb.user.Classes" id="ClassesResultMap2"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="com.lcb.user.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <collection property="student" ofType="com.lcb.user.Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap> </mapper>
## 20. How many ways can mybatis implement one-to-one? How did you do it?
There are joint query and nested query. Joint query is the joint query of several tables. It can be completed only once by configuring the association node in resultMap and configuring one-to-one classes;
Nested query is to query a table first and then query data in another table according to the foreign key id of the result in this table. It is also configured through association, but the query of another table is configured through the select attribute.
22. Does Mybatis support delayed loading? If yes, what is its implementation principle?
A: Mybatis only supports the delayed loading of association objects and Collection Association collection objects. Association refers to one-to-one and Collection refers to one to many queries. In the Mybatis configuration file, you can configure whether to enable deferred loading. Lazyloading enabled = true | false.
Its principle is to use CGLIB to create the proxy object of the target object. When calling the target method, enter the interceptor method, such as calling a.getB() GetName (), the interceptor invoke() method finds that a.getB () is null value, then it sends the sql of the saved B query object separately, B queries up, and then a.setB(b) is invoked, so the object B property of a has value, and then completes the B (). Call of getname() method. This is the basic principle of delayed loading.
Not only Mybatis, but almost all, including Hibernate, support delayed loading on the same principle.
23. Mybatis L1 and L2 cache:
Level 1 Cache: the HashMap local Cache based on the PerpetualCache. Its storage scope is Session. When the Session is flushed or close d, all caches in the Session will be cleared. The level 1 Cache is opened by default.
The second level cache has the same mechanism as the first level cache. By default, it is stored in perpetual cache and HashMap. The difference is that its storage scope is Mapper(Namespace) and the storage source can be customized, such as Ehcache. The second level cache is not opened by default. To open the second level cache, you need to implement the Serializable serialization interface (which can be used to save the state of the object) to use the second level cache attribute class. You can configure < cache / > in its mapping file;
24. What is the interface binding of MyBatis? What are the implementation methods?
Interface binding is to define any interface in MyBatis, and then bind the methods in the interface with SQL statements. We can call the interface methods directly, so that we can have more flexible choices and settings than the methods provided by SqlSession.
There are two ways to implement interface binding. One is through annotation binding, that is, add @ Select, @ Update and other annotations on the interface methods, which contain SQL statements to bind; The other is to bind by writing SQL in xml. In this case, the namespace in the xml Mapping file must be the full pathname of the interface. When the SQL statement is relatively simple, it is bound with annotations. When the SQL statement is relatively complex, it is bound with xml. Generally, it is more bound with xml.
25. What are the requirements when using the mapper interface of MyBatis?
① Mapper interface method name and mapper The id of each sql defined in XML is the same;
② The input parameter type and m apper The parameterType of each sql defined in XML is the same;
③Mapper. The namespace in the XML file is the classpath of the mapper interface.
27. Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in.
A: mybatis can only write plug-ins for ParameterHandler, ResultSetHandler, StatementHandler and Executor. Mybatis uses the dynamic agent of JDK to generate proxy objects for the interfaces to be intercepted to realize the function of interface method interception. Whenever the methods of these four interface objects are executed, it will enter the interception method, Specifically, it is the invoke() method of InvocationHandler. Of course, it will only intercept those methods you specify to intercept.
Write a plug-in: implement the Interceptor interface of Mybatis and duplicate the intercept() method, and then write notes to the plug-in to specify which interface and which methods to intercept. Remember, don't forget to configure the plug-in you write in the configuration file.