Java Road to God: Java Interview Preparation (14)

Four, SpringMvc framework

1. SpringMVC process

1. The user sends a request to the DispatcherServlet of the front controller

2. DispatcherServlet receives the request to call the HandlerMapping processor mapper

3. The processor mapper finds the specific mapper, generates the processor object and the processor interceptor and returns it to DispatcherServlet

4. DispatcherServlet calls the HandlerAdapter processor adapter

5. HandlerAdapter calls a specific processor (Controller) after adaptation

6. After the Controller is executed, return to ModelAndView

7. HandlerAdapter returns the Controller execution result ModelAndView to DispatcherServlet

8. DispatcherServlet passes ModelAndView to ViewReslover

9, ViewReslove returns to the specific View after parsing

10. DispatcherServlet performs view rendering according to View

11. DispatcherServlet responds to users

2. What is SpringMVC

SpringMVC is an open source framework of MVC and a submodule of Spring

3. How to set up forwarding and redirection in SpringMVC

(1) Forwarding: add a "forward:" in front of the return value, such as "forward:user?name=zhangshan"

(2) Redirection: add a "redirect:" in front of the return value, such as "redirect:http://www.baidu.com"

4. What are the common annotations in SpringMVC

@RequestMapping: An annotation used to process request url mapping, which can be used on classes or methods. Used on a class, it means that all methods in the class that respond to requests use this address as the parent path

@RequestBody: Annotation realizes receiving json data of http request, and converting json into java object

@ResponsBody: Annotation converts the object returned by the controller method into a json object response to the customer

Five, MyBatis framework

1. What is MyBatis

MyBatis is an excellent persistence layer framework, a semi-ORM framework, which supports customized SQL, stored procedures and advanced mapping. Mybatis avoids almost all JDBC code and manual setting of parameters and obtaining result sets. Mybatis can map native types, interfaces and Java pojos through simple XML annotations and configuration

2. The advantages and disadvantages of Mybatis

advantage

  • Based on traditional SQL statement programming, it is quite flexible and will not have any impact on the existing design of applications or databases. SQL is written in XML, which releases the coupling between SQL and program code and facilitates unified management; XML tags are provided, Support for writing dynamic SQL statements
  • Compared with traditional JDBC, it reduces the amount of code by more than 50% and eliminates a lot of redundant code in JDBC
  • Good compatibility with various databases
  • Provide mapping tags

Disadvantage

  • The workload of SQL statement writing is relatively large, especially when there are many fields and associated tables, there are certain requirements for the ability of developers to write SQL statements
  • SQL statements depend on the database, resulting in poor portability of the database, and the database cannot be changed at will

3. MyBatis programming steps

1. Create SqlSessionFactory

2. Create SqlSession through SqlSessionFactory

3. Perform database operations through sqlSession

4. Call sqlSession.commit() to commit the transaction

5. Close sqlSession.close()

4. What design patterns are used at the bottom of MyBatis

1. Builder mode

SqlSessionFactoryBuilder
XMLConfigBuilder
XMLMapperBuilder

2. Factory model

SqlSessionFactory
ObjectFactory
MapperProxyFactory

3. Singleton mode

ErrorContext
LogFactory

4. Agency model

MapperProxy 
ConnectionLogger

5. What is the difference between #{} and ${}

1. #{} is pre-compilation processing, ${} is string replacement

2. When MyBatis processes #{}, it replaces #{} in sql with? (Placeholder), and calls the set method of PreparedStatement to assign the value

3. When MyBatis handles the {}, it isWhen , it is the {} replace the value of the variable. (Concatenation string)

4. Using #{} can effectively prevent sql injection

6. How to deal with the difference between the attribute name in the entity class and the field name in the table

1. By defining the alias of the field in the query sql statement, the field alias is consistent with the attribute name of the entity class

<select id="getOrderById" parameterType="int" resultType="Order">
   slelect order_id id,order_no orderNo,order_price price 
    from orders
    where order_id=#{id}
</select>

2. Using resultMap to map the field name and attribute name are inconsistent

<select id="getOrder" parameterType="int" resultMap="orderMap">
   select * from orders where order_id=#{id}
</select>
<resultMap id="orderMap" type="Order">
   <!-- id属性来映射主键字段-->
    <id property="id" cloumn="order_id"></id>
    
    <result property="orderNo" cloumn="order_no"></result>
    <result property="price" cloumn="order_price"></result>
</resultMap>

7. Fuzzy query Like statement writing

1. Add sql wildcard in java code

string wildcardname =%smi%;
list<name> names = mapper.selectlike(wildcardname);

<select id=”selectlike”>
select * from foo where bar like #{
    
    value}
</select>

2. Splicing strings in sql statements will cause sql injection

tring wildcardname = “smi”;
list<name> names = mapper.selectlike(wildcardname);

<select id=”selectlike”>
select * from foo where bar like "%"#{
    
    value}"%"
</select>

8. Usually an xml mapping file has 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?

The Dao interface is the Mapper interface. The fully qualified name of the interface is the value of the namespace in the mapping file; the method name of the interface is the id value of the Statement of the Mapper in the mapping file; the parameters in the interface method are the parameters passed to sql. The Mapper interface has no implementation class. When the interface method is called, the interface full name + method name splicing string as the key value can uniquely locate a MapperStatement. In Mybatis, each <select>, <insert>, <update>, <delete> tag corresponds to a MapperStatement object.

The methods in the Mapper interface cannot be overloaded, because the storage and search strategy of fully qualified name + method name is used. The working principle of the Mapper interface is the JDK dynamic proxy. Mybatis will use the JDK dynamic proxy to generate a proxy object proxy for the Mapper interface when Mybatis runs. The proxy object intercepts the interface method and executes the sql represented by the MapperStatement instead, and then returns the sql execution result.

9. How does MyBatis perform paging? The principle of the paging plugin

10. Briefly describe the operating principle of the MyBatis plug-in and how to write a plug-in

11. MyBatis executes batch insert, can it return the database primary key list?

12. What does MyBatis dynamic SQL do? What are the dynamic SQLs? Briefly describe the execution principle of dynamic SQL

13. How does MyBatis encapsulate the SQL execution result as a target object and return it

14. MyBatis dynamic proxy mechanism is applied to that part of the framework

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/113977180
Recommended