MyBatis中resultType屬性的使用
MyBatis中resultType屬性
resultType:MyBatis中resultType是用來指定查詢結果類型的屬性
一.返回一般數(shù)據(jù)類型
對于引用類型一般采用大寫字母轉小寫的方式。
基本類型一般在前面加"_"
下面給了兩個例子:
string
- mapper接口:
String getPasswordByName(@Param("name") String name);- xml:
<select id="getPasswordByName" resultType="string">
select password from user where name = #{name}
</select>int
- mapper接口:
int getIdByName(@Param("name") String name);- xml:
<select id="getIdByName" resultType="_int">
select id from user where name = #{name}
</select>二.返回JavaBean 類型
比如要返回一個user:
- mapper接口:
User getUserByName(@Param("name") String name);- xml:
<select id="getUserByName" resultType="user">
select * from user where name = #{name}
</select>三.返回List
一般在mapper接口中返回List,在xml中resultType寫上T就可以。
- mapper接口:
List<User> list();
- xml:
<select id="list" resultType="user"> select * from user </select>
四.返回Map類型
1.如果是將某個字段作為key,對象作為value
@MapKey("id")
Map<Integer,User> getUser();- xml:
<select id="getUserById" resultType="User"> select * from user </select>
結果:
{1=User(id=1, babayId=1, name=kail)}
2.如果只有一條記錄可以將字段名作為key,值作為value
Map<Integer,Object> getUserById(@Param("name") int id);- xml:
<select id="getUserById" resultType="map">
select * from user where id={id}
</select>結果:
{id=1, babayId=1, name=kail}
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringCloud實現(xiàn)SSO 單點登錄的示例代碼
作為分布式項目,單點登錄是必不可少的,這篇文章主要介紹了SpringCloud實現(xiàn)SSO 單點登錄的示例代碼,非常具有實用價值,需要的朋友可以參考下2019-01-01
Spring中Bean的加載與SpringBoot的初始化流程詳解
這篇文章主要介紹了Spring中Bean的加載與SpringBoot的初始化流程詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot項目里面發(fā)起http請求的幾種方法
Spring Boot發(fā)起HTTP請求有多種方法,包括:RestTemplate,WebClient, HttpClient, Feign Client,第三方庫如OkHttp和Apache HttpClient,下面就來詳細的介紹一下如何實現(xiàn),感興趣的可以了解一下2025-12-12
Springboot 整合 Java DL4J 實現(xiàn)農(nóng)產(chǎn)品質量檢測系統(tǒng)(推薦)
本文詳細介紹了系統(tǒng)的搭建過程,包括技術選型、數(shù)據(jù)處理、模型訓練和評估等關鍵步驟,系統(tǒng)采用卷積神經(jīng)網(wǎng)絡,對水果成熟度和缺陷進行識別,有效解決了傳統(tǒng)方法成本高、效率低的問題,有助于提升農(nóng)產(chǎn)品檢測的科技含量和自動化水平2024-10-10

