Java Interview summary of four

Source: Internet
Author: User

Objective

The previous article briefly introduced the knowledge of MySQL, speaking of MySQL will have to talk about the MyBatis framework, this article mainly talk about the application of MyBatis and interview questions. Overall, mybatis more can be understood as a tool, the interview asked a relatively small number of questions, you can also learn about hibernate, in the interview process can give the interviewer a knowledge-rich feeling.

Architecture of the MyBatis

There are two ways of mybatis and database interaction, one is traditional according to Statementid call API, the other is based on Mapper interface implementation, here we mainly talk about the way mapper interface. The benefits of accessing the database through the Mapper interface, the personal summary has two points, one conforms to the object-oriented programming of the dependency interface instead of relying on the implementation class, the second is to meet the Java dynamic Agent through the implementation of the interface. So here also stealth a problem, mybatis use of the design pattern is what-proxy mode.

By knowing that each mapper interface corresponds to a mapper.xml configuration file, where the ID value of each node in the XML (SELECT, INSERT, UPDATE, DELETE) corresponds to the method name in the Mapper interface, MyBatis each node in the Mapper.xml into a statement object, placed in the configuration, each time the method is called through the interface, according to the method name, that is, the statementid of the statement object to find the corresponding statement object, In order to generate SQL dynamically and invoke JDBC to execute a series of operations such as SQL, we have connected the mapper interface with the Mapper.xml. MyBatis also supports annotations in ways that integrate methods and SQL, and can write SQL directly on the interface's methods, as follows:

1 @Select ("select * from Debate")2 list<debate> selectdebate (row row);

Say here again about the configuration of the entry, that is, ParameterType and Parametermap, the difference between the two is very simple, a parameter and a number of parameters only, to understand, The parametermap is usually java.lang.HashMap and is passed through the Key-value form. There are two ways to get parameters in SQL #{} and ${}, and the result of parsing the same SQL is different, as follows:

1 Select *  from where = #{id}

When we pass id=1, the final SQL is

1 Select *  from where = ' 1 '

And when we use ${id}, the final SQL is

1 Select *  from where = 1

The difference between the two is that the single quotation marks this place, so that using #{} can prevent SQL injection, and #{} cannot, about SQL injection this place can be a little understanding, for example, still use the above SQL, assuming that the type of ID is a string, when we pass the value of the ID to "1 or 1 = 1" , the final SQL is

1 Select *  from where = 1 or 1 = 1

Because 1 = 1 is set up, so will all the data are queried out, for the user personal information such as data, this is very insecure, and more frightening is the following situation, when we pass the value of the ID as "1;delete from TableName", the final SQL is

1 Select *  from where = 1; Delete  from TableName

Once the data is queried, the database is emptied, and you cry!

It is said that since ${} does not prevent SQL injection, then we use it for what it is worth to exist, if we need to dynamically pass in the table name, we must use ${}, because #{} will add a single quotation mark to the argument

1 Select *  from ' TableName ' where = 1

Obviously, this is a bad SQL.

Say again Resultmap and resultclass two attributes. Typically resultclass specifies the model class defined in Java and maps the returned results resultset to the entity class. But because of the naming rules, we usually use "_" for database fields, such as user name: user_name, and for model entity classes, we usually use hump to name: UserName. When the two names are different, the use of ResultClass is not automatically mapped, this time you need to use Resultmap to implement.

1     <Resultmaptype= "User"ID= "Usermapper">  2         <ID Property= "id"column= "id"/>3         <!--only the inconsistent fields need to be modified -4         <result Property= "Sex"column= "User_sex"/>5     </Resultmap>6       7     <!--query students, based on ID -  8     <SelectID= "GetById"ParameterType= "int"Resulttype= "User">  9         <! [Cdata[ Ten SELECT * from Sys_user One WHERE id = #{id} A         ]]>    -     </Select>

Here I only write a different column, the same name will be automatically mapped, but the individual proposal to write the whole, looking more intuitive, conducive to later maintenance.

Speaking of which, by the way, about a pair of one or one-to-many and many-to-many mappings, although not asked during the interview, as a knowledge point extension. Here is a blog, interested students can see the "MyBatis one-to-many, a lot of a, many-to-many understanding." The results of the query to this SQL resultset and our model are also linked together, and the following is how the Mapper interface is used.

When we use mybatis, we usually inject our mapper interface into the specific DAO class, so how does spring manage these mapper interfaces? Add the following configuration to the spring configuration:

1 <Beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer">  2     < Propertyname= "Basepackage"value= "Test.mapper"></ Property>  3     < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory"></ Property>  4 </Bean>

The Org.mybatis.spring.mapper.MapperScannerConfigurer class automatically scans all mapper interfaces under the Test.mapper package and uses the proxy mode to generate a proxy class for each mapper interface, so that it is actually injected into the DAO class. is a proxy class, this must be remembered! At this point MyBatis can be used, and then say the composition of mybatis and the overall flow of SQL execution.

It is only necessary to understand the role of each object, the approximate process is sqlsession is responsible for invoking the API, the request method name as Statementid from the configuration object to find the corresponding Mappedstatement object, The configuration object is a key-value pair with Statementid as value key,mappedstatement. The Mappedstatement object is a node in the XML configuration (SELECT, INSERT, UPDATE, delete), and then sqlsession delegates the request to executors to execute. Executor is responsible for the generation of dynamic SQL statements and the maintenance of query caches, resulting in a Statementhandler object, The Statementhandler object is responsible for java.sql.Statement parameters, calling JDBC to query the database and turning the result java.sql.ResultSet into a list. May at first glance a bit messy, for these nouns a bit unfamiliar, here is an article recommended to everyone "mybatis principle in-depth analysis", after reading it back to see this paragraph will feel clear a lot.

To tell the truth, MyBatis in fact there is not much to say, as a framework for the JDBC package, the internal structure is very simple, easy to use, to say that the disadvantage is that SQL must program ape own handwriting, but as a qualified developer, SQL should be the necessary skills. Another point is about database switching, this situation I encountered in the development of a few, because of the reason of handwritten SQL, different databases for SQL support is not the same, you may need to modify the SQL statement, Hibernate does not have this problem (such as Oracle and MySQL paging is not the same). MyBatis's summary is probably so many, later thought the supplementary!

This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Original link: http://www.cnblogs.com/1ning/p/6727416.html

Java Interview summary of four

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.