案例:
資料庫表
Person,有三個 列:id、name、city
類Person有三個成員變數:pid、pname、pcity
當資料庫的欄位名和我們的類的成員變數名不一致,主要通過兩種方法實作映射:
- 通過sql陳述句,使用
as起別名,就是給資料庫的列名起一個別名,別名與類的成員變數名一致,
select
id as pid,
name as pname,
money as pmoney
from person
注意最后一個查詢陳述句不需要逗號
- 通過
resultMap標簽進行資料庫列與類的成員變數的映射
<resultMap id="personMap" type="person">
<id column="id" property="pid"/>
<result column="name" property="pname"/>
<result column="city" property="pcity"/>
</resultMap>
<select id="findAll" resultMap="personMap">
select
id,name,city
from ss_company
</select>
resultMap標簽進行資料庫與物體類的映射
標簽id,對應資料庫表的主鍵,,標簽result,對應資料庫非主鍵的其他列
屬性column對應資料庫表的列名,屬性property對應物體類
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/189783.html
標籤:其他
下一篇:【非關系型資料庫NOSQL Redis】 NoSql與Redis 關系型資料庫和非關系型資料庫的區別 Redis的介紹 Redis的應用場景
