mybatis framework --mybatis interview summary

 

Make advertising, selling things to help a friend, something super cheap yo [] clothes, shoes, etc. , factory direct shipping, is lower than the market price! ! Generally cheaper than the market price of 3-7 fold [are] brand goods, if you are interested, you can scan two-dimensional code bottom of the screen, thanks attention! ! !

Micro letter

  • What is the difference between # {} and {} is $

        Properties file $ {} is the placeholder variable, which can be a tag property value and the internal sql, a static text replacement, such as $ {Driver} to be static for com.mysql.jdbc.Driver. # {} Is sql parameter placeholder, Mybatis will in sql # {} with the? Number, parameter setting method will be used before the PreparedStatement sql executed sequentially to the sql? Parameter set number placeholder such ps.setInt (0, parameterValue), # {item.name} way using a reflection values ​​acquired item name attribute value of the object from the object parameter corresponds param.getItem (). getName ().

  • Xml mapping file, in addition to the common select | insert | updae | than delete the label, what label?

        There are many other tags, <resultMap>, <parameterMap>, <sql>, <include>, <selectKey>, plus nine dynamic sql tag, trim | where | set | foreach | if | choose | when | otherwise | bind the like, where <sql> tag of sql fragments, fragments introduced sql <include> tags, <selectKey> does not support the primary key generation strategy increment of labels.

  • In practice, usually a Xml mapping file, will write a corresponding interface to Dao, may I ask, what is the working principle of the interface is Dao? Dao interfaces in the methods, parameters are different, the method can reload it?

        Dao interface is often said Mapper interface fully qualified name of the interface is the namespace value mapping file, the name of the interface method, the value is the id mapping file MappedStatement, the parameters within the interface method that was passed to sql parameter. Mapper not interface implementation class, when a method call interface, the interface name + fully qualified method name as a string concatenation key value, may be a uniquely positioned MappedStatement, for example: com.mybatis3.mappers.StudentDao.findStudentById, can be found a unique namespace id = findStudentById com.mybatis3.mappers.StudentDao is below the MappedStatement. In Mybatis, each <select>, <insert>, <update>, <delete> tag is resolved to a MappedStatement object.

        Dao in the interface method is not overloaded, because the policy is to preserve and to find the fully qualified name + method name.

        Dao is the interface works JDK dynamic proxy will be used Mybatis running JDK dynamic proxy interface generation Dao proxy proxy object, the proxy object proxy intercepts interface methods in favor of the implementation of sql MappedStatement represented, then the sql execution results returned.

  • How Mybatis is paged? What is the principle pagination plug-in?

        Mybatis RowBounds objects using paging, which is set for paging ResultSet execution result, rather than the physical page can be directly written with the parameters of the physical pages in the physical page is accomplished sql function may be used to complete the physical page tab widget .

        The basic principle is to use the plug-in tab Mybatis plugin interface provided, to achieve a custom plug-ins, sql intercept interception to be performed within the insert method, and then rewrite the sql, according dialect dialect, add statements and physical page corresponding to the physical paging parameters.

For example: select * from overwriting the student, to intercept sql: select t * from (select * from student) t limit 0,10.

  • Mybatis perform bulk insert, you can return the database primary key list it?

        Energy, JDBC can, Mybatis certainly can.

  • Mybatis dynamic sql is doing what? What are dynamic sql? Can briefly principle of dynamic sql execution is not?

        Mybatis dynamic sql can let us in Xml mapping file, write dynamic sql in the form of labels, complete logic and dynamic splicing sql functions, Mybatis offers nine dynamic sql tag trim | where | set | foreach | if | choose | when | otherwise | bind.

        Principle of its execution, the value calculated from an expression using OGNL sql parameter object, according to the value of the dynamic splicing sql expression, in order to complete the dynamic sql function.

  • Mybatis is how to execute sql package is the result of the target object and returns? What are mapping form?

        The first is to use the <resultMap> tag, of defining the mapping between the name of the column names and object properties. The second function is to use an alias sql column, the column alias name is written as object properties, such as T_NAME AS NAME, General object properties are name, lowercase, but the column names are case insensitive, Mybatis ignores the column name case, smart finds the corresponding object property name, you can even write T_NAME aS naMe, as Mybatis can work properly.

        Once you have a column mapping between names and attribute names, Mybatis creating objects through reflection, use reflection individually assigned to the properties of the object and returns, those properties can not find the mapping relationship, it is unable to complete the assignment.

  • Whether Mybatis support lazy loading? If so, what is its principle is to achieve?

        Mybatis association only supports lazy loading the associated object collection and a collection of associated objects, association refers to one, collection refers to the many queries. In Mybatis configuration file, you can configure whether to enable lazy loading lazyLoadingEnabled = true | false.

        Its principle is to use CGLIB create a proxy object of the target object when the target method call, enter the interceptor methods, such as call a.getB (). GetName (), interceptor invoke () method found a.getB () is null value, then it will separate the saved queries sent in advance sql associated object B, the query up B, then call a.setB (b), then there is a target attribute value b, and then completed a.getB ( ) call .getName () method. This is the basic principle lazy loading.

        Of course, not only Mybatis, including almost all of Hibernate, lazy loading support for the principle is the same.

  • Mybatis how to perform batch processing?

        Use BatchExecutor complete batch.

  • What are Mybatis Executor executor? What is the difference between them?

        Mybatis There are three basic Executor executor, SimpleExecutor, ReuseExecutor, BatchExecutor.

  1. SimpleExecutor: Each time update or select, just open a Statement object, run out immediately closed Statement object.
  2. ReuseExecutor: execute update or select, as a key to find sql Statement object exists on the use, it does not exist to create, after use, do not close the Statement object, but placed Map <String, Statement> within, for the next use. In short, re-use Statement objects.
  3. BatchExecutor: execute update (not select, JDBC does not support batch select), all sql are added to the batch (addBatch ()), waiting for the uniform implementation (executeBatch ()), it caches multiple Statement objects, each Statement objects are addBatch () after completion, waiting for execution one by one executeBatch () batch. And JDBC same batch.

Scope: Executor of these features are strictly limited in scope SqlSession life cycle.

  • Mybatis how to specify which one to use Executor executor?

        In Mybatis profile you can specify a default ExecutorType actuator type, the method may also be created manually to transfer the SqlSession DefaultSqlSessionFactory ExecutorType type parameter.

  • Mybatis mapping file, if the A label by reference to the content of B include labels, may I ask, Can I define B label at the back of the label A, or that A must be defined in the previous label?

        Although Mybatis parsing Xml map file is parsed in the order, however, B label is still referenced can be defined anywhere, Mybatis can be identified correctly.

        The principle is, Mybatis A label parsing, references B found A label tag, but the tag has not been resolved to B, does not exist yet, at this time, labeled A-label will Mybatis unresolved, and parse the remainder of the label, containing B labels, all labels to be resolved is completed, Mybatis re-render those marked as unresolved tag, then re-parsing time label a, B tag already exists, it can normally resolve a label done.

  • Brief Mybatis mapping relationship between the Xml mapping file and Mybatis internal data structures?

        Xml Mybatis all configuration information is encapsulated into All-In-One Configuration heavyweight objects inside. In Xml mapping file, <parameterMap> tag will be resolved to ParameterMap objects, each sub-element will be parsed as ParameterMapping object. <ResultMap> tag will be resolved to ResultMap objects, each sub-element will be parsed as ResultMapping object. Each <select>, <insert>, <update>, <delete> tag will be resolved to MappedStatement objects, sql within the tag BoundSql are parsed into objects.

  • Why Mybatis semi-automatic ORM mapping tools? Where it differs is in automatic?

        Hibernate ORM mapping tool is fully automatic, with the Hibernate query associated object or collection object can be directly obtained according to an object-relational model, it is fully automatic. And Mybatis when querying the associated object or collection of objects associated with the need to manually write sql to complete, so called semi-automatic ORM mapping tools.

Published 107 original articles · won praise 184 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_22172133/article/details/87360850