Mybatis學習總結

2021-08-21 21:20:43 字數 1868 閱讀 9764

mybatis

1.目前最主流的持久層框架為hibernate與mybatis,而且國內目前情況使用mybatis的公司比hibernate要多。 

2.hibernate學習門檻不低,要精通門檻更高。門檻高在怎麼設計o/r對映,在效能和物件模型之間如何權衡取得平衡,以及怎樣用好hibernate快取與資料載入策略方面需要你的經驗和能力都很強才行。國內目前前的情況精通hibernate技術大牛非常少。 

3.sql優化方面,hibernate的查詢會將表中的所有字段查詢出來,這一點會有效能消耗。當然了,hibernate也可以自己寫sql來指定需要查詢的字段,但這樣就破壞了hibernate開發的簡潔性。說得更深入一些,如果有個查詢要關聯多張表,比如5張表,10張表時,而且,我們要取的字段只是其中幾張表的部分字段。這時用hibernate時就會顯得非常力不從心。就算用hibernate的sqlquery,後續的維護工作也會讓人發狂。

讀取配置檔案 

properties pro = new properties(); 

inputstream in = servletactioncontext.getservletcontext().getresourceasstream("/web-inf/classes/upfilepath.properties"); 

pro.load(in); 

string path = pro.getproperty("path");

//根據id查詢

// 模糊查詢

// select * from user where username like #

// select * from user where username like '%$%';

<--

這兩個屬性配套使用

keyproperty :用某個屬性來接收

usegeneratedkeys="true" 使用自增

-->

// 如果配置這個,上面的在insert上面配置的主鍵返回就會失效

select uuid()

insert into `user`

(`username`,

`birthday`,

`***`,

`address`)

values (#, // 直接取出pojo的屬性值

#, #,

#);// 更新使用者

update `user` set `username` = #

where `id`= #;

delete from

`user` where `id`=#;

在配置檔案中加入對映檔案

@test

public void testgetuserbyid() throws exception

1.原始方式:

public inte***ce userdao 

public class userdaoimple implements userdao

public listgetuserbyusername(string username)

public void insertuser(user user)

2.動態**方式包裝dao

動態**只有介面不用開發人員自己寫實現類。 

開發規則

在對映檔案中namespace必須是介面的全路經名。

介面的方法名必須與sql id 一致。

介面的入參parametertype型別必須一致。

介面的返回值必須與resulttype型別一致。

Mybatis學習總結

使用過程 新增依賴 新增配置檔案 通過mybatis config得到sqlsessionfactory 使用sqlsession操作要執行的sql語句 非執行緒安全,使用後關閉 全域性配置檔案mybatis config.xml 資料庫資訊 全域性設定 cacheenabled一般設為false不...

MyBatis學習總結 七 Mybatis快取

正如大多數持久層框架一樣,mybatis 同樣提供了一級快取和二級快取的支援 一級快取 基於perpetualcache 的 hashmap本地快取,其儲存作用域為session,當session flush 或 close之後,該session中的所有 cache 就將清空。2.二級快取,如 eh...

MyBatis學習總結 七 Mybatis快取

正如大多數持久層框架一樣,mybatis 同樣提供了一級快取和二級快取的支援 一級快取 基於perpetualcache 的 hashmap本地快取,其儲存作用域為session,當session flush 或 close之後,該session中的所有 cache 就將清空。2.二級快取,如 eh...