當我們封裝的時候我們一般要求實體類中和資料庫的列名保持一致。
如果不一致將會導致查詢結果為空。
1.在sql語句中給資料庫中的字段起別名
如:
"findall"
resulttype
="com.domain.user"
>
select product_name as name, product_type as type,product_price as price,regist_date as date from product ;
select
>
2.給實體類和資料庫建立對應關係
利用resultmap配置查詢結果中的列名和實體類的屬性名的對應關係,如下:
namespace
="com.dao.userdao"
>
"usermap"
type
="com.domain.user"
>
property
="name"
column
="product_name"
/>
property
="type"
column
="product_type"
/>
property
="price"
column
="product_price"
/>
property
="date"
column
="regist_date"
/>
resultmap
>
"findall"
resultmap
="usermap"
>
select * from product
select
>
然後在sql語句中將resulttype設定為"usermap"即可;
"findall"
resultmap
="usermap"
>
select * from product
select
>
where標籤類似於mysql中的where語句。
"findbyproduct"
parametertype
="product"
resulttype
="product"
>
select * from product
>
test
="product_type!=null"
>
and product_type like #
if>
test
="product_price!=null"
>
and product_price>#
if>
where
>
select
>
當上面的product_type和product_price都不為null的時候,那麼列印出來的sql語句為:
select * from product where product_type like 「***」 and product_price>***;
where標籤會自動將其後第乙個條件的and或者是or給忽略掉。
if標籤就類似於我們平時使用的if語句,起著選擇的作用,如下:
"findbyproduct"
parametertype
="product"
resulttype
="product"
>
select * from product
>
test
="product_type!=null"
>
and product_type like #
if>
test
="product_price!=null"
>
and product_price>#
if>
where
>
select
>
我們可以根據product_type或product_price的符合條件來查詢,只要滿足了if標籤裡面給出的條件,即可進行查詢。
只依照乙個條件查詢
@test
public
void
testfindbyproduct()
}
當上面兩個條件都啟用的時候,即:
@test
public
void
testfindbyproduct()
}
表示的sql語句為:select * from product where product_type like 「%用品%」 and product_price>100;
foreach標籤如其名,起到遍歷集合型別的作用,如下:
"findbytype2"
parametertype
="queryproduct"
resulttype
="product"
>
select * from product
>
test
="product_types!=null and product_types.size()>0"
>
collection
="product_types"
open
="and product_type in("
close
=")"
item
="product_type"
separator
=","
>
#foreach
>
if>
where
>
select
>
上面中的collection表示這是乙個集合,由open開始,在close結束,並且將遍歷出來的每一項存到item裡面,由separator分割。
@test
public
void
testfindbytype2()
}
利用sql標籤來配置重複的sql語句,可以避免在專案開發的過程中重複編寫大量相同的sql語句,如下:
"defaultuser"
>
select * from product
sql>
使用方式(使用include標籤)
"findbyproduct"
parametertype
="product"
resulttype
="product"
>
refid
="defaultuser"
/>
>
test
="product_type!=null"
>
and product_type like #
if>
test
="product_price!=null"
>
and product_price>#
if>
where
>
select
>
java SE複習筆記4
知之為知之,不知為不知,是知也!知道就是知道,不知道就是不知道,這才是真正的智慧型!如果我們還記得要把自己不知道的都弄會,那麼就是大智了!哈哈 關鍵字 賦予了某種特殊含義 關鍵字全都為小寫字母 保留字 以後版本可能變成關鍵字 識別符號 我們自己定義的變數名 方法名 類名 介面名等 1 26個字母大小...
log4j複習筆記
自己複習log4j的功能,寫的乙個小demo public class testlog4j package hb.log4j import org.apache.log4j.logger public class log4jtest 關於log4j.properties檔案的配置說明 direct ...
《MyBatis技術內幕》筆記4 高階主題
四 高階主題 1 interceptor 相關模式 責任鏈模式 interceptor 可以改變mybatis的預設行為,如實現sql重寫之類的功能。使用者自定義 除了繼承interceptor介面,還需要使用 intercepts和 signature來指定攔截的方法列表,最後需要在mybatis...