Lock screen interview questions – mybatis (I)


Lock screen interview questions - mybatis (I)
Lock screen interview questions will be brushed every day, and the interview questions will be updated every working day. The lock screen interview question app and applet are now online. The official website address is: https://www.demosoftware.cc/#… 。 It has included all the contents of the interview questions updated every day, as well as the features of unlocking the screen to review the interview questions, daily programming questions, e-mail push and so on. Let you take the lead in the interview and hang the interviewer! Next is today’s interview question:
====What’s the difference between # and $in mybatis?
“#” is equivalent to adding double quotation marks to the data, and $is equivalent to displaying the data directly

  1. “#” treats the incoming data as a string, and adds a double quotation mark to the automatically incoming data. For example: order by #user_ ID #, if the value passed in is 111, the value parsed into SQL is order by “111”. If the value passed in is ID, the value parsed into SQL is order by “Id”
  2. $displays the incoming data directly and generates it in SQL. For example, order by $user_ ID $. If the value passed in is 111, the value parsed into SQL is order by 111. If the value passed in is ID, the value parsed into SQL is order by ID
  3. The “#” method can prevent SQL injection to a great extent$ Method cannot prevent SQL injection.
    4. The $method is generally used to pass in database objects, such as table names
    5. Do not use # the $

====What are the programming steps of mybatis?
1. Create sqlsessionfactory

//Obtain sqlsessionfactory according to the configuration information of mybatis-config.xml
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new             SqlSessionFactoryBuilder().build(inputStream);

2. Create sqlsession through sqlsessionfactory
3. Execute database operation through sqlsession
4. Call session. Commit() to commit the transaction
5. Call session. Close() to close the session

====What are the shortcomings of JDBC programming and how does mybatis solve these problems?

  1. The frequent creation and release of database links cause a waste of system resources, which affects the system performance. If you use database link pool, you can solve this problem.
    Solution: configure the data link pool in sqlmapconfig.xml and use the connection pool to manage database links.
  2. SQL statements are written in the code, which makes the code difficult to maintain. The actual application of SQL may change greatly. SQL changes need to change the Java code.
    Solution: configure the SQL statement in the xxxxmapper.xml file and separate it from the Java code.
  3. It is troublesome to pass parameters to SQL statements, because the where conditions of SQL statements are not necessarily many or few. Placeholders need to correspond to parameters one by one.
    Solution: mybatis automatically maps Java objects to SQL statements.
  4. It is troublesome to parse the result set. The change of SQL leads to the change of parsing code, and it needs to be traversed before parsing. It is more convenient to parse if the database records can be encapsulated into POJO objects.
    Solution: mybatis automatically maps SQL execution results to Java objects.

====What are the requirements when using the mapper interface of mybatis?

  1. The mapper interface method name is the same as the ID of each SQL defined in mapper.xml
  2. The input parameter type of mapper interface method is the same as the parametertype of each SQL defined in mapper.xml
  3. The output parameter type of mapper interface method is the same as the resulttype of each SQL defined in mapper.xml
  4. The namespace in mapper.xml file is the classpath of mapper interface.

====What about the first level cache and second level cache in mybatis?

  1. L1 cache: HashMap local cache based on perpetualcache. Its storage scope is session. When session flush or
    After closing, all caches in the session will be emptied.
  2. The second level cache has the same mechanism as the first level cache. By default, it is stored in perpetual cache and HashMap. The difference is that its storage scope is mapper (namespace) and the storage source can be customized, such as ehcache. The scope is namespan, which means that all the select operation results in the configuration file corresponding to the namespan are cached, so that different threads can share the secondary cache. Start L2 cache: configure the < cache / > node in the mapper configuration file.
<mapper namespace="com.yihaomen.mybatis.dao.StudentMapper">
    <!-- Open the L2 cache under the namespace of this mapper -- >
Eviction: represents the cache recycling policy. At present, mybatis provides the following policies.
        (1) LRU, the least used object recently and the longest unused object
        (2) FIFO, first in first out, removes objects in the order they enter the cache
        (3) Soft, soft reference, removing objects based on garbage collector status and soft reference rules
        (4) Weak, weak reference, more actively removes objects based on garbage collector status and weak reference rules. LRU is used here to remove the pair images that are not used for the longest time
Flushinterval: refresh interval, in milliseconds. 100 second refresh is configured here. If you do not configure it, the cache will be refreshed when SQL is executed.
      Size: the number of references, a positive integer, represents the maximum number of objects that can be stored in the cache. It should not be set too large. Setting too high will cause memory overflow. 1024 objects are configured here
      Readonly: read only, which means that the cached data can only be read and cannot be modified. The advantage of this setting is that we can quickly read the cache. The disadvantage is that we can't modify the cache. Its default value is false and we are not allowed to modify -- >
    <cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/>

3. For the cache data update mechanism, when a C / U / D operation is performed on the of a scope (L1 cache session / L2 cache namespaces), by default, the caches in all selections under the scope will be cleared.
====How does mybatis set to return the primary key ID during the insert operation?
When the database is MySQL:

<insert id="insert" parameterType="com.test.User" keyProperty="userId" 
useGeneratedKeys="true" > 

“Keyproperty” indicates that the returned ID is to be saved in the property of the object, “usegeneratedkeys” indicates that the primary key ID is in self growth mode.
The above configuration in MySQL is OK
When the database is Oracle:

<insert id="insert" parameterType="com.test.User">
     <selectKey resultType="INTEGER" order="BEFORE" keyProperty="userId"> 
insert into user (user_id, user_name, modified, state)
values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, 
#{modified,jdbcType=TIMESTAMP}, #{state,jdbcType=INTEGER})

Since Oracle does not have the term “self growth”, only the form of sequence imitating self growth, the “usegeneratedkeys” attribute can no longer be used. Instead, use < selectkey > to obtain and assign the ID to the attribute of the object. The ID is inserted normally during the insert operation.

More interview questions can be concerned about “demo lock screen interview questions” official account through small programs or App to obtain interview questions and learning resources.
Lock screen interview questions - mybatis (I)

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]