mybatis的強大之處就在於它的sql對映檔案,下面介紹sql對映檔案的幾個頂級元素配置
2)cache:配置給定的命名空間的快取
3)cache——ref:從其他冥冥空間的引用快取配置
4)resultmap:用來描述資料庫結果集和物件的對應關係
5) sql : 可以重用的sql塊,也可以被其他語句引用
6) insert:對映插入語句
7) update :對映更新語句
8) delete :對映刪除語句
9)select :對映查詢語句
1.使用select完成單條件查詢
1〉dao曾方法
public listfindall();
2〉xml檔案
findall" resulttype="
user
">select *
from user
select>
3〉測試方法
publicvoid
findall()
} catch
(exception e)
finally
}
2.使用select完成多條件查詢
1〉dao層方法
//多條件查詢
public listfindlistbymanneycontent(mapmap);
2〉xml檔案的sql語句
findlistbymanneycontentindex
" resulttype="
user
">select * from user where username like concat('
%',#,'
%') and id>=#
select>
3〉測試方法
//多條件查詢,查詢id〉2的員工姓名
@test
public
void
findlistbymanneycontentindex()
} catch
(exception e)
finally
}
3.使用insert完成增加功能
1〉dao層方法
//增加的方法public void add(role role);
2〉xml配置的sql語句
insert into role(rolecode,rolename) values (#,#)
3〉測試方法
//增加的方法
@test
public
void
addtest()
catch
(exception e)
finally
}
4.使用update完成修改功能
1〉dao層方法
//修改的方法
public
int update(user user);
2〉xml配置檔案的sql語句
update user set usercode=#,username=# where id=#
3〉測試方法
@testpublic
void
update()
catch
(exception e)
finally
}
5.使用delete完成刪除功能
1〉dao層方法
//刪除的方法
public
void delete(role role);
2〉xml配置檔案的sql語句
delete from role where id=#
3〉測試方法
//刪除的方法
@test
public
void
deletetest()
catch
(exception e)
finally
}
MyBatis 之 對映檔案 動態sql
mybatis 之 對映檔案 動態sql f標籤 作為判斷入參來使用的,如果符合條件,則把if標籤體內的sql拼接上。注意 用 if進行判斷是否為空時,不僅要判斷 null 也要判斷空字串 where標籤 會去掉條件中的第乙個and符號。public class user public class ...
Mybatis之sql對映檔案篇
mybatis之sql對映檔案 用來存放sql語句的配置檔案,需要與同名sql介面繫結,sql語句有著各自的對應標籤 實際值,與該方法的引數名無關,引數名不需要與bean屬性名對應 id delete from tb1 employee where id and gender update tb1 ...
MyBatis對映檔案
5 刪除 6 更新 7 根據員工id和姓名獲取員工物件 8 測試類 mybatis 的真正強大在於它的對映語句,也是它的魔力所在。由於它的異常強大,對映器的 xml 檔案就顯得相對簡單。如果拿它跟具有相同功能的 jdbc 進行對比,你會立即發現省掉了將近 95 的 mybatis 就是針對 sql ...