[MyBatis] MyBatis problem sorting (absolute dry goods)

Interface binding implementation

  • Binding by annotation means adding @Select, @Update and other annotations to the interface method, which contains Sql statements to bind;

  • Binding is done by writing SQL in xml. In this case, you must specify the namespace in the xml mapping file

Must be the full path name of the interface. When the Sql statement is relatively simple, use annotation binding, when the SQL statement is more complex, use xml binding, generally xml binding is more.

What are the requirements when using the mapper interface of MyBatis?

1. The Mapper interface method name is the same as the id of each SQL defined in mapper.xml.

2, each input parameter type and sql mapper.xml Mapper interface methods defined in the parameterTypesame type.

3, the output parameter type Mapper interface method and mapper.xmlthe same type of each of the sql resultType defined.

4, Mapper.xml file namespacethat is classpath mapper interface.

Usually an Xml mapping file will write a Dao interface corresponding to it. What is the working principle of this Dao interface? When the parameters of the methods in the Dao interface are different, can the methods be overloaded?

How the Dao interface works

Dao interface is often said Mapper interface fully qualified name of the interface is the map file namespacevalue, the interface method name, is mapping file MappedStatementid value parameters within the interface method, the parameter that is passed to sql . The Mapper interface does not have an implementation class. When calling an interface method, the interface full name + method name spliced ​​string as the key value can be uniquely located MappedStatement.

For example :

com.mybatis3.mappers.StudentDao.findStudentById, the only MappedStatement whose namespace is com.mybatis3.mappers.StudentDao under id = findStudentById can be found.

In Mybatis, each of < select >, < insert >, < update >, < delete >tag, is resolved to a MappedStatement object.

Overloading of Dao interface methods

The methods in the Dao interface cannot be overloaded because it is the storage and search strategy of the fully qualified name + method name.

Dao is the interface works JDK dynamic proxy, use JDK dynamic proxy interface generation agent for Dao Mybatis runtime proxyobject proxy object proxywill intercept interface methods in favor of the implementation of MappedStatement represented sql, the sql execution then returns the results.

In the Xml mapping file of Mybatis, can the id be repeated for different Xml mapping files?

For different Xml mapping files, if the namespace is configured, the id can be repeated; if the namespace is not configured, then the id cannot be repeated; after all, the namespace is not required, it is just a best practice.

The reason is that namespace+id is used as the key of Map<String, MappedStatement> . If there is no namespace, there will be an id, so duplicate id will cause data to overwrite each other. With the namespace, the id can be repeated naturally, and the namespace+id is naturally different if the namespace is different.

Briefly describe the mapping relationship between Mybatis's Xml mapping file and Mybatis internal data structure?

Answer: Mybatis encapsulates all Xml configuration information into the All-In-One heavyweight object Configuration.

In the Xml mapping file, the <parameterMap> tag will be parsed as a ParameterMap object, and each of its child elements will be parsed as a ParameterMapping object. <resultMap>The label will be parsed as a ResultMap object, and each of its child elements will be parsed as a ResultMapping object. Every <select>, <insert>, <update>, <delete>labels will be parsed as MappedStatement objects, sql within the tag will be parsed as BoundSql object.

How does Mybatis encapsulate the sql execution result as a target object and return it? What are the mapping forms?

  • The first is to use tags to define the mapping relationship between column names and object attribute names one by one.

  • The second is to use the alias function of the sql column to write the column alias as the object attribute name, such as T_NAME AS NAME,

Object attribute names are generally name, lowercase, but column names are not case sensitive. Mybatis will ignore the case of column names and find the corresponding object attribute names intelligently. You can even write T_NAME AS NaMe. Mybatis can work normally.

With the mapping relationship between column names and attribute names, Mybatis creates objects through reflection, and at the same time uses the attributes reflected to the objects to assign and return one by one. Those attributes that cannot find the mapping relationship cannot be assigned.

In the Xml mapping file, besides the common select|insert|updae|delete tags, what other tags are there?

There are many other tags,,,,,, plus dynamic sql nine labels
trim, where set|foreach if choose when otherwise bindetc., which <sql>is a fragment sql tag by < include>tag sql introducing fragments, labeled primary key generation strategy is not supported by the self-energizing.

In the Mybatis mapping file, if the A tag refers to the content of the B tag through include, can the B tag be defined after the A tag, or must it be defined before the A tag?

Although Mybatis parses the Xml mapping file in order, the quoted B tag can still be defined anywhere, and Mybatis can recognize it correctly.

The principle is that Mybatis parses the A label and finds that the A label refers to the B label, but the B label has not been parsed and does not exist yet. At this point, Mybatis will mark the A label as unresolved, and then continue to parse the remaining labels, including B Labels. After all the labels are parsed, Mybatis will re-parse the labels that are marked as unresolved. At this time, when the A label is parsed, the B label already exists, and the A label can be parsed normally.

Advanced Search

There are several ways to realize one-to-one and one-to-many in MyBatis, how to operate?

There are joint queries and nested queries. Several joint union query table query, the query only once, by resultMap inside association, collectionnode configuration one to one, one to many classes can be completed.

Nested query is to check a table, according to the results of the foreign key table id inside, and then further to a table inside the data query, but also by the configuration association, collectionbut additionally by a look-up table select node configuration.

Can Mybatis map Enum enumeration class?

Mybatis can map enumeration classes, not only can map enumeration classes, Mybatis can map any object to a column of the table. The mapping method is a custom one TypeHandler, which implements the setParameter() and getResult() interface methods of TypeHandler.

TypeHandler has two functions, one is to complete the conversion from javaType to jdbcType, and the other is to complete the conversion from jdbcType to javaType, which is embodied in two methods, setParameter() and getResult(), which respectively represent setting sql question mark placeholder parameters and obtaining columns search result.

Dynamic SQL

What does Mybatis dynamic sql do? What are the dynamic SQL? Can you briefly describe the implementation principle of dynamic sql?

Mybatis dynamic sql can let us in Xml mapping file, written in the form of dynamic sql tag to complete the logic and dynamic sql stitching function, Mybatis offers nine dynamic sql tag trim where set foreach if choose when otherwise bind.

The principle of execution is to use OGNL to calculate the value of the expression from the sql parameter object, and dynamically splice sql according to the value of the expression to complete the function of dynamic sql.

Plug-in module

How does Mybatis pagination? What is the principle of the paging plugin?

Mybatis uses the RowBounds object for paging. It is a memory paging for the ResultSet result set, not physical paging. You can write parameters with physical paging directly in SQL to complete the physical paging function, or you can use the paging plug-in to complete physical paging. .

The basic principle of the paging plug-in is to use the plug-in interface provided by Mybatis to implement a custom plug-in, intercept the SQL to be executed in the plug-in's interception method, then rewrite the SQL, and add the corresponding physical paging statement and physical paging parameters according to the dialect dialect.

For example :

select * from student, after intercepting sql, rewrite as:

select t.* from (select * from student) t limit 0, 10

Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in.

Mybatis can only write for ParameterHandler, ResultSetHandler, StatementHandler, Executorplug the four interfaces, Mybatis use JDK dynamic proxy for the proxy object interface generation need to intercept interface methods to achieve the interception function, method executes whenever these four interface object, it Will enter the interception method, specifically InvocationHandlerthe invoke() method. Of course, only those methods that you specify to be intercepted will be intercepted.

Implement the Interceptor interface of Mybatis and override the intercept() method. Then write an annotation for the plug-in, and specify which methods of which interface you want to intercept. Remember, don't forget to configure the plug-in you wrote in the configuration file.

Cache

Mybatis's primary and secondary cache

1) Level 1 cache: HashMap local cache based on PerpetualCache. Its storage scope is Session. After Session flush or close, all Caches in the Session will be emptied, and Level 1 cache is turned on by default.

2) The mechanism of the second-level cache is the same as that of the first-level cache. By default, PerpetualCache and HashMap storage are used. The difference is that the storage scope is Mapper (Namespace), and the storage source can be customized, such as Ehcache. The second-level cache is not turned on by default. To enable the second-level cache, the use of the second-level cache attribute class needs to implement the Serializable serialization interface (which can be used to save the state of the object), which can be configured in its mapping file <cache/>;

3) For the cache data update mechanism, when a C/U/D operation is performed in a certain scope (first-level cache Session/second-level cache Namespaces), all caches in select under this scope will be cleared by default.

Guess you like

Origin blog.csdn.net/Black_Customer/article/details/107420116