Mybatis interview questions

Prodigal son Tang Shao 2022-02-13 07:29:28 阅读数:560

mybatis interview questions

mybatis Interview questions

1.mybatis First level cache and second level cache

1. First level cache

First level cache refers to sqlsession cache . The first level cache is enabled by default , It's only relative to the same SqlSession It works , So it's also called SqlSession cache . When the parameters and SQL In exactly the same way , Use the same SqlSession Object calls the same Mapper Method , When the first 1 Execution times SQL After the statement ,MyBatis Will automatically put it in the cache , When querying again later , If there is no claim to refresh , And the cache does not timeout , It will directly fetch the previously cached data , Instead of sending it again SQL To the database . If after checking , Add, delete and change operation , Will empty the cache .

2. Second level cache

The difference from L1 cache is that its storage scope is Mapper(Namespace) , Multiple SqlSession To operate the same Mapper Of sql sentence , Multiple SqlSession Second level cache can be shared .MyBatis L2 cache read priority is higher than MyBatis First level cache . close sqlsession after , Will put the sqlsession The data in the first level cache is added to namespace In the second level cache of .

L2 cache is not enabled by default , If you want to turn on , Need to be in mybatis To configure xml Configuration in file setting And configuration SQL Of XML Configuration in file cache node , Because each XML Through the root node namespace Property corresponds to a Mapper Interface , therefore , Secondary storage is also called namespace cache ! When using secondary storage , The result type returned by the query must be the implementation of Serializable Interface !

Be careful :

1. After caching data , If the current XML The increment configured in 、 Delete 、 Change operation , The previous cache data is refreshed !

2. In the first level cache are objects , So every time you get the same object , In the L2 cache is the data , So every time we get it, we will assign a new object to , But these different objects are obtained from the cache , Not from the database !

2.mybatis in #{} and ${} The differences and application scenarios

1 #{ } It's precompiling ,MyBatis Processing #{ } when , It will be sql Medium #{ } Replace with ? Place holder , Then pass in the parameter assignment , After passing in the string , Automatically put quotation marks around the value , Can prevent sql Inject
2 Pass on Enter into Of ginseng Count straight Pick up raw become stay s q l in , Rong easy lead Hair s q l notes Enter into site view : 1. The parameters passed in are directly generated in sql in , Easy to trigger sql Inject scene : 1. Pass on Enter into Of ginseng Count straight Pick up raw become stay sql in , Rong easy lead Hair sql notes Enter into site view 1.{} The application scenario is to use when you need to dynamically pass in the table name or column name , If you use #{}, After parsing, it is a string with double quotation marks , The database cannot find such a column name .

3.mybatis Of sql What is injection

Malicious sql Insert into the executed field , such as $ Import parameters directly into the database , If the parameter is written by the user delete Parameter patient drop Parameters , The database data or table structure is missing .

4.mybatis Pagination

1. Tradition sql Pagination (mysql)

Use limit Pagination , Parameters are transferred to the current page , Page size . then sql perform

select * from surface limit ( The current page -1)* Page size , Page size

such as :

Query the first 1 To the first article 10 The number of items is sql yes :select * from table limit 0,10; -> Our requirement is to query the data on the first page :select * from table limit (1-1)*10,10;

Query the first 11 To the first article 20 The number of items is sql yes :select * from table limit 10,10; -> Our requirement is to query the data on the second page :select * from table limit (2-1)*10,10;

Query the first 21 To the first article 30 The number of items is sql yes :select * from table limit 20,10; -> Our requirement is to query the data on the third page :select * from table limit (3-1)*10,10;

2. Tradition sql Pagination (oracle)

Use ROWNUM Pagination , Parameters are transferred to the current page , Page size ,sql perform

select * (select A.*,ROWNUM RN from (select * from Table name ) A where ROWNUM<= The current page * Page size )
where RN>= ( The current page -1)* Page size

3.PageHelper Pagination (springboot The way )

1. stay pom.xml Add... To the configuration file PageHelper rely on

2. stay springboot Of application.yml File configuration PageHelper Related information

3. Use... In business PageHelper.startPage( The current page , Page size ); Method , Use PageInfo< Entity class > userInfoPage = new PageInfo< Entity class >(mapper Method );

Such as :

 // Paging information
PageHelper.startPage(pageIndex, pageSize, orderBy);
// Perform paging queries
PageInfo<UserInfo> userInfoPage =
new PageInfo<UserInfo>(userMapper.searchUserList(userSearchParam));

5.mybatis Lazy loading

Deferred loading is to initiate a query when the data is actually used , Don't check when not in use , Check on demand .resultMap One on one and one on many , First query from a single table 、 When necessary, the query is associated from the associated table , Greatly improved Database performance , Because it is faster to query a single table than to query multiple tables by association .Mybatis The delayed loading function of is off by default , Need to be in mybatis Passed in configuration file setting Tag configuration to enable the delayed loading function

6.mybatis technological process

mybatis The whole call execution process :

  1. Load configuration and initialize : namely mybatis Master profile ,mapper Configuration files and annotations . Among them, the SQL The configured information is loaded one by one MapperStatement object ( Pass in parameter mapping configuration 、 Executive SQL sentence 、 Result mapping configuration ).
  2. Receive call request : Received the passed in parameters and the to be executed SQL Of ID, Pass the request to the lower data processing layer for processing .
  3. Process operation request : actuator Executor Process the data passed by the interface layer SQL Of ID And pass in parameters , according to ID Find the corresponding MapperStatement, analysis MapperStatement object , Get what needs to be done SQL Statement and inject the passed in parameters . Get the connection to the database , Will eventually SQL Statement to the database for execution , And get the result . according to MapperStatement The result mapping configuration in the object converts the obtained results , Get the final result . Finally, release the resources and return the results to the upper layer .

7.mybatis How to use (springboot The way )

1. Import mybatis Starting dependency of

2. establish application.yml Add database connection information

3. establish Mapper Interface

4. establish Mappper The mapping file

5. stay application.yml To configure mybatis And spring Integrate

#spring And mybatis Consolidation configuration
mybatis:
mapper-locations: classpath:mapper/*Dao.xml # Mapping configuration scan
type-aliases-package: com.itheima.pojo # Scan physical packages , Change to alias
copyright:author[Prodigal son Tang Shao],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130729259515.html