2022年Java備戰(zhàn)秋招,程序員求職必看的Mybatis面試題

前言

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。

小編分享的這份2022年Java秋招備戰(zhàn)面試題總計有1000多道面試題,包含了MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、Redis、MySQL、Java 并發(fā)編程、Java基礎(chǔ)、Spring、微服務(wù)、Linux、Spring Boot 、Spring Cloud、RabbitMQ、kafka等16個專題技術(shù)點,都是小編在今年金三銀四總結(jié)出來的面試真題,已經(jīng)有很多粉絲靠這份PDF拿下眾多大廠的offer,今天在這里總結(jié)分享給到大家!【持續(xù)更新中!】

序號 專題技術(shù) 內(nèi)容 地址
1 MyBatis 2022年Java備戰(zhàn)秋招,程序員求職必看的Mybatis面試題
2 ZooKeeper 2022年Java備戰(zhàn)秋招,程序員求職必看的ZooKeeper面試題

1、什么是Mybatis?

2、Mybaits的優(yōu)點:

3、MyBatis框架的缺點:

4、MyBatis框架適用場合:

5、MyBatis與Hibernate有哪些不同?

6、#{}和${}的區(qū)別是什么?

7、當(dāng)實體類中的屬性名和表中的字段名不一樣 ,怎么辦 ?

第 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>來映射字段名和實體類屬性名的一一對應(yīng)的關(guān)系

<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
為數(shù)據(jù)表中的屬性–>
<result property = “orderno” column =”order_no”/>
<result property=”price” column=”order_price” />
</reslutMap>

8、 模糊查詢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>

9、通常一個Xml映射文件,都會寫一個Dao接口與之對應(yīng),請問,這個Dao接口的工作原理是什么?

10、Mybatis是如何進(jìn)行分頁的?分頁插件的原理是什么?

11、Mybatis是如何將sql執(zhí)行結(jié)果封裝為目標(biāo)對象并返回的?都有哪些映射形式?

第一種是使用<resultMap>標(biāo)簽,逐一定義數(shù)據(jù)庫列名和對象屬性名之間的映 射關(guān)系。

第二種是使用 sql 列的別名功能,將列的別名書寫為對象屬性名。

有了列名與屬性名的映射關(guān)系后,Mybatis 通過反射創(chuàng)建對象,同時使用反射給 對象的屬性逐一賦值并返回,那些找不到映射關(guān)系的屬性,是無法完成賦值的。

12、如何執(zhí)行批量插入?

首先,創(chuàng)建一個簡單的 insert 語句:

<insert id=”insertname”>
insert into names (name) values (#{value})
</insert>

然后在 java 代碼中像下面這樣執(zhí)行批處理插入:

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();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
}
finally {
sqlsession.close();
}

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

14、在mapper中如何傳遞多個參數(shù)?

3、第三種:多個參數(shù)封裝成 map

try {
//映射文件的命名空間.SQL 片段的 ID,就可以調(diào)用對應(yīng)的映射文件中的
SQL
//由于我們的參數(shù)超過了兩個,而方法中只有一個 Object 參數(shù)收集,因此
我們使用 Map 集合來裝載我們的參數(shù)
Map < String, Object > map = new HashMap();
map.put("start", start);
map.put("end", end);
return sqlSession.selectList("StudentID.pagination", map);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}

15、Mybatis動態(tài)sql有什么用?執(zhí)行原理?有哪些動態(tài)sql?

16、Xml映射文件中,除了常見的select|insert|updae|delete標(biāo)簽之外,還有什么?

18、為什么說Mybatis是半自動ORM映射工具?它與全自動的區(qū)別在哪里?

19、 一對一、一對多的關(guān)聯(lián)查詢 ?

<mapper namespace="com.lcb.mapping.userMapper">
<!--association 一對一關(guān)聯(lián)查詢 -->
<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">
<!-- 實體類的字段名和數(shù)據(jù)表的字段名映射 -->
<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 一對多關(guān)聯(lián)查詢 -->
<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>

20、MyBatis實現(xiàn)一對一有幾種方式?具體怎么操作的?

21、MyBatis實現(xiàn)一對多有幾種方式,怎么操作的?

22、Mybatis是否支持延遲加載?如果支持,它的實現(xiàn)原理是什么?

23、Mybatis的一級、二級緩存:

24、什么是MyBatis的接口綁定?有哪些實現(xiàn)方式?

25、使用MyBatis的mapper接口調(diào)用時有哪些要求?

26、Mapper編寫有哪幾種方式?

第一種:接口實現(xiàn)類繼承 SqlSessionDaoSupport:使用此種方法需要編寫 mapper 接口,mapper 接口實現(xiàn)類、mapper.xml 文件。

第二種:使用 org.mybatis.spring.mapper.MapperFactoryBean:

1、在 sqlMapConfig.xml 中配置 mapper.xml 的位置,如果 mapper.xml 和 mappre 接口的名稱相同且在同一個目錄,這里可以不用配置

<mappers>
<mapper resource="mapper.xml 文件的地址" />
<mapper resource="mapper.xml 文件的地址" />
</mappers>

2、定義 mapper 接口:

1、mapper.xml 中的 namespace 為 mapper 接口的地址

2、mapper 接口中的方法名和 mapper.xml 中的定義的 statement 的 id 保持一 致

3、Spring 中定義

<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper 接口地址" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

第三種:使用 mapper 掃描器:

4、使用掃描器后從 spring 容器中獲取 mapper 的實現(xiàn)對象

27、簡述Mybatis的插件運行原理,以及如何編寫一個插件。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容