MyBatis資料庫操作總結

2021-08-18 14:43:19 字數 2278 閱讀 7197

1. namespace

1.2. 介面寫全稱

2. 配置檔案實現介面的步驟

2.1. 通過配置select,insert,update,delete節點並拼寫sql語句實現介面

2.2. id 方法名

2.3. parametertype 介面方法的引數型別(如果方法有多個引數,則可以省略)

3. 複雜引數的介面方法的對映配置

3.1. 乙個引數

3.1.1. 基本型別

emp findempbyid(

//為介面方法引數命別名,方便在對映檔案中使用#代表傳入的引數值

@param("abcd") int id);

select * from emp where empid=#

3.1.2. 物件型別

int updateemp(emp emp);

update emp set ename=# where empid=#

3.2. 多個引數

listfindempbycondition(

@param("empid")

int id,

@param("emp")

emp emp);

select * from emp where empid=# and ename=#

emp login(int id,string name);

select * from emp where empid=# and ename=#

emp login(

@param("empid")

int id,

@param("ename")

string name);

select * from emp where empid=# and ename=#

4. resultmap和resulttype

4.1. resultmap 程式設計師根據業務需要自定義的屬性名與列名的對映關係,如果屬性名與列名不一致

4.2. resulttype 介面方法返回值集合的元素型別,一般就是實體類的型別

4.3. resultmap可以根據需要,配置多個對映方案

5. mybatis實現分頁查詢

5.1. 設計介面方法

listfindempbycondition2(hashmap map);

select * from (

select e.*,rownum r from emp e

where e.ename like #

and e.hiredate >= #

and e.hiredate <= #

and e.deptid=#

and rownum <=#

)where r>#

5.3. 測試類

//定義分頁查詢需要的所有條件

int pageindex=1;

int pagesize=5;

//多條件採用幫助類形式儲存

emputil eu=new emputil("%劉%", emputil.stringtodate("2015-01-01"), emputil.stringtodate("2018-12-31"), 3);

hashmap map=new hashmap();

//將所有條件以鍵值對的方式儲存在hashmap

//配置檔案將以鍵獲取對應的資料

map.put("eu", eu);

map.put("start", pagesize*pageindex);

map.put("end",pagesize*(pageindex-1));

listemps= ed.findempbycondition2(map);

for (emp emp : emps) {

system.out.println(emp);

6. mybatis多對一配置

6.1. 實體類中存在物件型別屬性,則視為一對一或者多對一的關聯關係

6.2. 實體類設計

public class emp{

private dept dept;

6.3. 介面設計

listfindallemps();

6.4. 配置mybatis-config.xml

full 可以自動對映物件的子物件屬性與列名的對應關係

6.6. 拼寫sql語句

resultmap對映關係基於查詢結果的

select * from emp e

left join dept d

on e.deptid=d.deptid

mybatis動態操作mysql資料庫

建立表 create table not null auto increment default null comment primary key id 0 unique key index 1 engine innodb default charset utf8 comment 刪除表 drop ...

資料庫操作總結

string constrfororacle data source jxorcl user scott password oracle oracleconnection oracleconn oracleconn new oracleconnection constrfororacle publi...

對資料庫與mybatis的總結

是預編譯處理,是字串替換。使用 可以有效的防止sql注入,提高系統安全性。資料的完整性 約束 實體完整性 主鍵約束 唯一約束 這兩者的區別在於主鍵必須滿足唯一和非空,而唯一約束可以為空 域完整性 保證欄位的完整性 資料型別 預設約束 default 非空約束 引用完整性 表與表之間的完整性 外來鍵約...