Location>code7788 >text

Common Interview Questions for MyBatis

Popularity:652 ℃/2024-10-10 09:10:52

MyBatis

1, what is MyBatis

MyBatis is an excellent semi-automated persistence layer framework . Support for custom SQL, stored procedures and advanced mapping .

2, MyBatis features?

Simple, flexible, decoupled, rich labeling

3, MyBatis core components

Global configuration file: MyBatis some of the global information, including database link information and MyBatis need to run a variety of features, as well as setting and affecting the behavior of MyBatis some properties

Core mapping file: The Mapper mapping file is an xml format file, the mapping file starts with aas the root node, and then the root node supports nine elements, respectively, insert, update, select, delete, etc.

SqlSession: SqlSession is the key object of MyBatis , is to perform persistence operations unique , similar to JDBC in the Connection. it is between the application and the persistence layer to perform interactive operations between a single-threaded object .

4, MyBatis mapping file additions, deletions and changes whether the return value type is required

Not necessary, because the return values of additions, deletions, and modifications are all of type int, and do not need to be specified.

5, MyBatis mapping file Mapper tag in the role of namespace and considerations

Role: realize and dao layer or mapper layer interface binding namespace called namespace

namespace and the fully qualified and unique name of the corresponding interface.

6. Briefly explain the role of the id attribute in the mapping file

The id attribute identifies a unique label, consistent with the method name in the corresponding mapper interface.

7, MyBatis in the multi-parameter binding of the way there are

Use #{param1} for the first parameter #{param2} for the second parameter and so on

Use #{arg0} for the first parameter #{arg1} for the second parameter and so on

Adding @Param(name) to the formal parameter of a persistence layer method is done by using #{name} in the mapping file to get the

8, in MyBatis # {param} and $ {param} difference

Using pre-compilation in MyBatis #{param} can effectively prevent sql injection

${param} uses sql splicing, which does not prevent sql injection, and is often used for fuzzy queries and paging operations.

9, MyBatis in how to get to add automatically generated primary key

Two ways: global configuration, local configuration

Global Configuration:

(1) Add the following to the configuration file:

<settings>
  <settings name="useGeneratedKeys" value="true">
</settings>

(2) Add the keyProperty property to the add tag and specify the accepted property name

Local Configuration:

Set useGeneratedKeys="true" and keyProperty="Received property name" on the add tag

10, MyBatis in the time to add, delete and check the two ways

Mapping file based approach

Annotation-based approach

11, MyBatis core configuration file in the commonly used labels

properties Load database parameters

settings Sets the return home key

typeAliases set aliases

environments sets up the data source

mappers sets the mapped resources

12, in the MyBatis configuration file Settings tab can be set which content

Turn on secondary cache, configure logging, set up use to generate primary keys, set up fields with null get values

13. Briefly describe what dynamic tags are in MyBatis

where When any condition in where holds, add the where keyword to the sql. When all conditions do not hold, the where keyword is not spliced and the and keyword is removed from the first condition

The if judgment operation uses test in it, as in

set The set keyword is spliced when the condition in the set tag holds, and not spliced if the condition does not hold, removing the last comma that matches the condition

trim can specify prefixes (perfix="") suffixes (suffix="") and remove redundant symbols (suffixoverrides="")

foreach iterates through a collection or list, specify the start and end identifiers, you can specify the splice symbol for each loop, often used in batch delete and add operations

collection :result set
    1, if the input parameter is a list, array, map type can directly write list, collection, array, map;
    2, or according to the index position of the parameters, arg0, arg1
item: each object in the collection
index: subscript
open: the loop begins with a character
close: loop ends with a character
separator: separator between the contents of the loop, will automatically remove redundant separators

sql extracts the public sql fragment, encapsulates it within the sql tag , include introduces the public sql fragment

sql fragment: <sql id=""> </sql>
Use sql fragment: <include ref/>

choose-when

<choose>
  	<when test=""></when>
	<when test=""></when>
	<otherwise></otherwise>
</choose>

14、MyBatis in the time one-to-one, many-to-many mapping of the core label is?

Use association for one-to-one and collection for one-to-many.

15、Please briefly describe the resultType, resultMap attribute and the difference between the resultMap tags

resultType attribute Specifies the Java type of the result.

resultMap attribute Specifies the mapping corresponding to the result.

The resultMap tag implements the mapping of entity class attributes to table fields.

16、log4j logging levels have which

log4j defines eight levels of log (excluding OFF and ALL, can be said to be divided into six levels), the priority from low to high: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL.
Understand:
ALL: lowest level, used to open all log records
DEBUG: mainly used to print some running information during development.
INFO: message highlights the process of running the application at a coarse-grained level.
WARN: Indicates a situation where a potential error occurs
ERROR: Indicates that an error event has occurred but does not affect the continued operation of the system.
FATAL: Indicates that each severe error event will cause the application to exit.
OFF: Used to turn off all logging

17, MyBatis persistence layer interface methods can be overloaded?

Can't, because the id of the mapping file is unique

18, MyBatis how to solve the problem of multiple parameters

Use of #{param1} #{arg0} Use of @Param in persistence layer interface method formal parameter

19、What are the commonly used labels of MyBatis

<select>
<update>
<delete>
<insert>

20, MyBatis how to call the stored procedure

Persistence tier methods do not have a return value because stored procedures do not have a return value

The mapping file corresponds to the SQL executor to be modified: callable

SQL is called using the Call keyword, and mode=IN/OUT is used in #{} to mark input/output parameters.

The output of the procedure goes into the object of the reference.

statementType: type of SQL executor
statement: execute normal SQL, can't prevent SQL injection.
prepared: executes normal SQL, can prevent SQL injection.
callable: execute procedure or function.
useCache: use cache; false means don't use cache, every time we execute, we will query the latest data from database.
mode: specify whether the parameter is an input or an output parameter. in and out

21, MyBatis how to prevent the entry parameter is empty (invalid column type: 1111)

Add jdbcType #{data, jdbcType.type} after the value

22、How to solve the problem of Map mapping is empty

In the MyBatis configuration file, add the

<settings>
	<setting name="callSettersOnNulls" value="true"/>
</settings>

23, MyBatis core objects

SqlSessionFactory

24, MyBatis paging plugin is what? How to use

PageHelper、PageInfo

Importing PageHelper dependencies

(pageNum, pageSize);

PageInfo pagination information: data, the previous page, the next page, the current page, page number array, whether there is a top and bottom page, whether it is the first and last page, etc.