Mybatis面試題

dalaoyang發表於2018-05-31

本文是在網上看了各種面試指南收集的題目及答案。無意冒犯各位原創作者,如果在您的部落格或者寫作平臺有相似問題答案可以跟我說,我給您連結加上,我只是為了方便以後自己需要的時候刷一刷,不用在到處找題。

1、#{}和${}的區別是什麼?

#{}是預編譯處理,${}是字串替換。Mybatis在處理#{}時,會將sql中的#{}替換為?號
,呼叫PreparedStatement的set方法來賦值;Mybatis在處理${}時,就是把${}替換成
變數的值。使用#{}可以有效的防止SQL隱碼攻擊,提高系統安全性。
複製程式碼

2、當實體類中的屬性名和表中的欄位名不一樣 ,怎麼辦 ?

第1種: 通過在查詢的sql語句中定義欄位名的別名,讓欄位名的別名和實體類的屬性名一致 
    <select id=”selectorder” parametertype=”int” resultetype=”me.gacl.domain.order”> 
       select order_id id, order_no orderno ,order_price price form orders where order_id=#{id}; 
    </select> 
    
第2種: 通過<resultMap>來對映欄位名和實體類屬性名的一一對應的關係 
    <select id="getOrder" parameterType="int" resultMap="orderresultmap">
        select * from orders where order_id=#{id}
    </select>
   <resultMap type=”me.gacl.domain.order” id=”orderresultmap”> 
        <!–用id屬性來對映主鍵欄位–> 
        <id property=”id” column=”order_id”> 
        <!–用result屬性來對映非主鍵欄位,property為實體類屬性名,column為資料表中的屬性–> 
        <result property = “orderno” column =”order_no”/> 
        <result property=”price” column=”order_price” /> 
    </reslutMap>
複製程式碼

3.模糊查詢like語句該怎麼寫?

第1種:在Java程式碼中新增sql萬用字元。
    string wildcardname = “%smi%”; 
    list<name> names = mapper.selectlike(wildcardname);

    <select id=”selectlike”> 
     select * from foo where bar like #{value} 
    </select>
    
第2種:在sql語句中拼接萬用字元,會引起sql注入
    string wildcardname = “smi”; 
    list<name> names = mapper.selectlike(wildcardname);

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

4、通常一個Xml對映檔案,都會寫一個Dao介面與之對應,請問,這個Dao介面的工作原理是什麼?Dao介面裡的方法,引數不同時,方法能過載嗎?

Dao介面,就是人們常說的Mapper介面,介面的全限名,就是對映檔案中的namespace的值,
介面的方法名,就是對映檔案中MappedStatement的id值,介面方法內的引數,就是傳遞
給sql的引數。Mapper介面是沒有實現類的,當呼叫介面方法時,介面全限名+方法名拼接
字串作為key值,可唯一定位一個MappedStatement,舉例:com.mybatis3.mappers.StudentDao.findStudentById,
可以唯一找到namespace為com.mybatis3.mappers.StudentDao下面id = findStudentById
的MappedStatement。在Mybatis中,每一個<select>、<insert>、<update>、<delete>標籤,都會被解析為一個MappedStatement物件。

Dao介面裡的方法,是不能過載的,因為是全限名+方法名的儲存和尋找策略。

Dao介面的工作原理是JDK動態代理,Mybatis執行時會使用JDK動態代理為Dao介面生成代理proxy物件,代理物件proxy會攔截介面方法,轉而執行MappedStatement所代表的sql,然後將sql執行結果返回。
複製程式碼

5、Mybatis是如何進行分頁的?分頁外掛的原理是什麼?

Mybatis使用RowBounds物件進行分頁,它是針對ResultSet結果集執行的記憶體分頁,而非物
理分頁,可以在sql內直接書寫帶有物理分頁的引數來完成物理分頁功能,也可以使用分頁外掛
來完成物理分頁。

分頁外掛的基本原理是使用Mybatis提供的外掛介面,實現自定義外掛,在外掛的攔截方法內
攔截待執行的sql,然後重寫sql,根據dialect方言,新增對應的物理分頁語句和物理分頁
引數。
複製程式碼

6、Mybatis是如何將sql執行結果封裝為目標物件並返回的?都有哪些對映形式?

答:第一種是使用<resultMap>標籤,逐一定義列名和物件屬性名之間的對映關係。第二種是使用sql列的別名功能,將列別名書寫為物件屬性名,比如T_NAME AS NAME,物件屬性名一般是name,小寫,但是列名不區分大小寫,Mybatis會忽略列名大小寫,智慧找到與之對應物件屬性名,你甚至可以寫成T_NAME AS NaMe,Mybatis一樣可以正常工作。
複製程式碼

7、如何執行批量插入?

首先,建立一個簡單的insert語句: 
<insert id=”insertname”> 
     insert into names (name) values (#{value}) 
</insert>
    
然後在java程式碼中像下面這樣執行批處理插入: 
   list<string> names = new arraylist(); 
    names.add(“fred”); 
    names.add(“barney”); 
    names.add(“betty”); 
    names.add(“wilma”); 

    // 注意這裡 executortype.batch 
    sqlsession sqlsession = sqlsessionfactory.opensession(executortype.batch); 
    try { 
     namemapper mapper = sqlsession.getmapper(namemapper.class); 
     for (string name : names) { 
     mapper.insertname(name); 
     } 
     sqlsession.commit(); 
    } finally { 
     sqlsession.close(); 
    }
複製程式碼

8、如何獲取自動生成的(主)鍵值?

insert 方法總是返回一個int值 - 這個值代表的是插入的行數。 
而自動生成的鍵值在 insert 方法執行完後可以被設定到傳入的引數物件中。 
示例: 
   <insert id=”insertname” usegeneratedkeys=”true” keyproperty=”id”> 
     insert into names (name) values (#{name}) 
    </insert>

    name name = new name(); 
    name.setname(“fred”); 

    int rows = mapper.insertname(name); 
    // 完成後,id已經被設定到物件中 
    system.out.println(“rows inserted = ” + rows); 
    system.out.println(“generated key value = ” + name.getid());
複製程式碼

9、在mapper中如何傳遞多個引數?

第1種
//DAO層的函式

Public UserselectUser(String name,String area); 
//對應的xml,#{0}代表接收的是dao層中的第一個引數,#{1}代表dao層中第二引數,更多引數一致往後加即可。

<select id="selectUser"resultMap="BaseResultMap">  
    select *  fromuser_user_t   whereuser_name = #{0} anduser_area=#{1}  
</select>  

第2種:    使用 @param 註解: 
    import org.apache.ibatis.annotations.param; 
        public interface usermapper { 
         user selectuser(@param(“username”) string username, 
         @param(“hashedpassword”) string hashedpassword); 
        }
然後,就可以在xml像下面這樣使用(推薦封裝為一個map,作為單個引數傳遞給mapper): 

   <select id=”selectuser” resulttype=”user”> 
         select id, username, hashedpassword 
         from some_table 
         where username = #{username} 
         and hashedpassword = #{hashedpassword} 
    </select>
    
複製程式碼

10、Mybatis動態sql是做什麼的?都有哪些動態sql?能簡述一下動態sql的執行原理不?

Mybatis動態sql可以讓我們在Xml對映檔案內,以標籤的形式編寫動態sql,完成邏輯判斷和動態拼接sql的功能。
Mybatis提供了9種動態sql標籤:trim|where|set|foreach|if|choose|when|otherwise|bind。
其執行原理為,使用OGNL從sql引數物件中計算表示式的值,根據表示式的值動態拼接sql,以此來完成動態sql的功能。
複製程式碼

11、Mybatis的Xml對映檔案中,不同的Xml對映檔案,id是否可以重複?

不同的Xml對映檔案,如果配置了namespace,那麼id可以重複;如果沒有配置namespace,那麼id不能重複;畢竟namespace不是必須的,只是最佳實踐而已。

原因就是namespace+id是作為Map<String, MappedStatement>的key使用的,如果沒有namespace,就剩下id,那麼,id重複會導致資料互相覆蓋。有了namespace,自然id就可以重複,namespace不同,namespace+id自然也就不同。
複製程式碼

12、為什麼說Mybatis是半自動ORM對映工具?它與全自動的區別在哪裡?

Hibernate屬於全自動ORM對映工具,使用Hibernate查詢關聯物件或者關聯集合物件時,可以根據物件關係模型直接獲取,所以它是全自動的。而Mybatis在查詢關聯物件或關聯集合物件時,需要手動編寫sql來完成,所以,稱之為半自動ORM對映工具。
複製程式碼

13、 一對一、一對多的關聯查詢 ?

<mapper namespace="com.lcb.mapping.userMapper">  
    <!--association  一對一關聯查詢 -->  
    <select id="getClass" parameterType="int" resultMap="ClassesResultMap">  
        select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}  
    </select>  
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap">  
        <!-- 實體類的欄位名和資料表的欄位名對映 -->  
        <id property="id" column="c_id"/>  
        <result property="name" column="c_name"/>  
        <association property="teacher" javaType="com.lcb.user.Teacher">  
            <id property="id" column="t_id"/>  
            <result property="name" column="t_name"/>  
        </association>  
    </resultMap>  

    <!--collection  一對多關聯查詢 -->  
    <select id="getClass2" parameterType="int" resultMap="ClassesResultMap2">  
        select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}  
    </select>  
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">  
        <id property="id" column="c_id"/>  
        <result property="name" column="c_name"/>  
        <association property="teacher" javaType="com.lcb.user.Teacher">  
            <id property="id" column="t_id"/>  
             <result property="name" column="t_name"/>  
        </association>  
        <collection property="student" ofType="com.lcb.user.Student">  
            <id property="id" column="s_id"/>  
            <result property="name" column="s_name"/>  
        </collection>  
    </resultMap>  

</mapper>  
複製程式碼

本文問題答案來源下面連結,沒有找到原文連結:

blog.csdn.net/eaphyy/arti…

相關文章