Summary of common basic interview questions

Keywords: Mybatis Spring Interview

1. What is the difference between synchronous and asynchronous?

Synchronization means that when a function call is issued, the call will not return or continue to perform subsequent operations until the result is obtained.

Asynchrony is relative to synchronization. When an asynchronous procedure call is issued, the caller can proceed with subsequent operations until the result is obtained. When the call is complete, the caller is usually notified through status, notification, and callback.

  2. What is mybatis? What are the characteristics?

It is a semi-automatic ORM persistence layer framework based on JDBC. It has high SQL flexibility, supports advanced mapping (one-to-one, one to many), dynamic SQL, delayed loading and caching, but its database independence is low

What is ORM?

Object Relation Mapping. Objects refer to Java objects, relationships refer to the relational model in the database, and object relational mapping refers to establishing a corresponding relationship between Java objects and the relational model of the database. For example, a java student class is used to correspond to a student table in the database, and the attributes in the class correspond to the columns in the table one by one. The student class corresponds to the student table, and a student object corresponds to a row of data in the student table

Why is mybatis a semi-automatic ORM framework?

To develop with mybatis, you need to write SQL statements manually. The fully automatic ORM framework, such as hibernate, does not need to write SQL statements. Developed with hibernate, you only need to define the ORM mapping relationship, and you can directly carry out CRUD operation. Because mybatis requires handwritten SQL statements, it has high flexibility and can customize SQL freely according to needs. Also, because it needs handwritten SQL, SQL statements may have to be rewritten when switching databases. Because different databases have different dialects, mybatis has low database independence. Although mybatis requires handwritten SQL, compared with JDBC, it provides input mapping and output mapping, and can easily set SQL parameters and encapsulate result sets. It also provides functions such as association query and dynamic SQL, which greatly improves the efficiency of development. And its learning cost is much lower than hibernate

3. How does mybatis realize many to one and one to many?

Query the information of all students, including the corresponding teacher name (many to one) association

<select id="getStudent2" resultMap="studentTeacher2">
    select s.id sid,s.name sname,t.name tname
    from student s,teacher t
    where s.tid=t.id
</select>
<resultMap id="studentTeacher2" type="pojo.Student">
    <result column="sid" property="id"/>
    <result column="sname" property="name"/>
    <association property="teacher" javaType="pojo.Teacher">
        <result column="tname" property="name"/>
    </association>
</resultMap>

Query the information of all teachers, including the corresponding collection of all students (one to many)

<!--Result nested mapping-->
	<select id="getTeacher" resultMap="teacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>

    <resultMap id="teacherStudent" type="pojo.Teacher">
        <result column="tid" property="id"/>
        <result column="tname" property="name"/>
        <!--
            javaType:Specifies the type of the property
            Generic information in the collection,use oftype obtain
		-->
        <collection property="students" ofType="pojo.Student">
            <result column="sid" property="id"/>
            <result column="sname" property="name"/>
            <result column="tid" property="tid"/>
        </collection>
    </resultMap>

4. Dynamic SQL of mybatis

if and where Tags

  <!--dynamic Sql : where / if-->
    <select id="dynamicSql"  resultType="com.lks.domain.User">
        select <include refid="tableAllkey"/> from users
        <where>
            <if test="id != null and id != 0">
                AND id = #{id}
            </if>
            <if test="name != null and name != ''">
                AND name = #{name}
            </if>
            <if test="county != null and county != ''">
                AND county = #{county}
            </if>
        </where>
    </select>

Generally, when developing query criteria for list business, if there are multiple query criteria, labels are usually used for control. The tag can automatically remove the logical operator (or, and) in front of the first condition. As written in the code, there is an "and" keyword in front of the id query condition, but it is not in the printed SQL. This is its role.

choose, when, otherwise Tags

These three tags need to be used together, similar to switch, case and default in Java. Only one condition is effective, that is, only the satisfied condition when is executed, and otherwise is executed for the unsatisfied condition, indicating the default condition.

 <!--dynamic Sql: choose,when,otherwise label-->
    <select id="dynamicSql2" resultType="com.lks.domain.User">
        select * from users
        <where>
            <choose>
                <when test="name != null and name != ''">
                    AND name = #{name}
                </when>
                <when test="county != null and county != ''">
                    AND county = #{county}
                </when>
                <otherwise>
                    AND id = #{id}
                </otherwise>
            </choose>
        </where>
    </select>

set tag

Use the SET tag to dynamically configure the SET keyword and eliminate any irrelevant commas appended to the end of the condition. After modifying the if+set tag, which field has a value to update in the form update operation. If an item is null, it will not be updated, but the original value of the database will be maintained.

<!--dynamic Sql: set label-->
    <update id="updateSet" parameterType="com.lks.domain.User">
        update users
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="county != null and county != ''">
                county = #{county},
            </if>
        </set>
        where id = #{id}
    </update>

trim tag
trim is a formatted label, which can complete the function of < set > or < where > marking. There are four main parameters:
① Prefix: prefix

② prefixOverrides: remove the first and or

③ Suffix: suffix

④ suffixOverrides: remove the last comma or other marks

<!--dynamic Sql: trim label-->
    <select id="dynamicSqlTrim" resultType="com.lks.domain.User">
        select * from users
        <trim prefix="where" suffix="order by age" prefixOverrides="and | or" suffixOverrides=",">
            <if test="name != null and name != ''">
                AND name = #{name}
            </if>
            <if test="county != null and county != ''">
                AND county = #{county}
            </if>
        </trim>
    </select>

5.mybatis features such as delayed loading and caching?

The delayed loading of mybatis means that during a join table query, if delayed loading is enabled, mybatis will split our join table query statements into single table query statements for separate query. For example, in the many to one relationship between students and teachers, query all students and their corresponding teachers. After delayed loading, it will be disassembled into select * from student first; Query the student information from the student table, get the tid, and then select * from teacher where id =? Query the teacher's information. The advantage of this is to delay the select query of associated objects, which can effectively reduce the pressure on the database.

How does mybatis perform delayed loading? Does it cause data security problems if an operation is divided into two operations. The answer is no, which involves the caching problem of mybatis. There are two-level caches in mybatis. The first level cache is enabled by default, and the second level cache needs to be configured in settings.

Level-1 cache: the role of Mybatis level-1 cache is the same SqlSession. When the same SqlSession is used to execute the same sql statement multiple times, the data will be stored in the cache (memory) from the database query for the first time, and the subsequent query will directly read the data in memory, so as to improve the query efficiency.

L2 cache: Mybatis L2 cache is shared by multiple sqlsessions. Its scope is the same namespace of mapper. Multiple sqlsessions execute the same sql. After the first execution, the data will be stored in the cache (memory), and the rest will be read directly. L2 cache is not enabled by default. L2 cache needs to be enabled in setting global parameter configuration.

Posted by cruzbullit on Sat, 18 Sep 2021 05:04:51 -0700