Mybatis返回值封裝在Map集合中

2021-08-20 13:26:31 字數 1501 閱讀 9130

資料庫表:

create

table

`products`

(`prod_id`

char(10

)collate utf8_unicode_ci not

null

,`vend_id`

char(10

)collate utf8_unicode_ci not

null

,`prod_name`

char

(255

)collate utf8_unicode_ci not

null

,`prod_price`

decimal(8

,2)not

null

,`prod_desc`

text

collate utf8_unicode_ci,

primary

key(

`prod_id`))

engine

=innodb

default

charset

=utf8 collate

=utf8_unicode_ci;

對映檔案:

<?xml version="1.0" encoding="utf-8" ?>

namespace

=>

"getresultusemap"

resulttype

="map"

>

select * from products where prod_id = #

select

>

>

/**

引入了lombok外掛程式

*/@setter

@getter

@tostring

@allargsconstructor

@noargsconstructor

@equalsandhashcode

public

class

products

對映介面:

//結果集以map形式返回

public map>

getresultusemap(@param("prod_id") string productid);}

測試**:

sqlsession sqlsession = null;

trycatch

(ioexception e)

finally

結果:

總結:

mybatis對映返回值封裝成map需要在對映檔案中的resulttype屬性值設定為map(map為mybatis預設設定的map集合類別名).

封裝的結果結構為:

WebUtils封裝返回值

在專案會有一些請求來規定返回值給前台,一般封裝個jsonobject物件即可,主要包括 code code,msg message,data data三個字段 主要有成功和出錯的兩種情況,這裡我們可以封裝乙個responseenum列舉類專門用於錯誤 和錯誤資訊提示 如下類public enum r...

函式返回值和封裝

需要用到函式內部的變數或者是函式的時候需要使用return 進行資料返回 函式返回值 return 可以將函式內部的變數或函式返回到函式外面去使用 語法 return 需要返回的內容 特點 return 一次只能返回乙個,寫多個最後只會返回最後乙個 函式中只要遇到return函式就結束了 retur...

snprintf返回值陷阱封裝

snprintf 函式用於將格式化的資料寫入字串,其原型為 int snprintf char str,int size,char format argument,返回值 對於snprintf有乙個不安全的地方就是它的返回值 當要寫入的argument的長度大於str的長度時,返回值不是str的長度...