Mybatis返回值(resultType&resultMap)的具體使用
之前的文章里面有對resultType和resultMap的簡單介紹這一期出點(diǎn)詳細(xì)的
resultType:
1,返回值為簡單類型。
直接使用resultType=“類型”,如string,Integer等。
String getEmpNameById(Integer id);
<!-- 指定 resultType 返回值類型時(shí) String 類型的, string 在這里是一個(gè)別名,代表的是 java.lang.String 對于引用數(shù)據(jù)類型,都是將大寫字母轉(zhuǎn)小寫,比如 HashMap 對應(yīng)的別名是 'hashmap' 基本數(shù)據(jù)類型考慮到重復(fù)的問題,會在其前面加上 '_',比如 byte 對應(yīng)的別名是 '_byte' --> <select id="getEmpNameById" resultType="string"> select username from t_employee where id = #{id} </select>
2.返回值為List類型。
使用resultType=“list元素的類型”,一般是實(shí)體類如User,也可以是Map,對應(yīng)返回值類型是List<User> , List<Map<String,Object>>,不管是哪種,最終結(jié)果會根據(jù)接口返回值類型自動將多個(gè) resultType指定的類型的元素(User或以一條記錄為一個(gè)Map)組裝成List。
List<User> getUser(String age);
<select id="getUser" resultType="User"> select * from user where age = #{age} </select>
List<Map<String,Object>> findUserList();
<select id="findUserList" parameterType="int" resultType="java.util.Map"> SELECT * FROM user </select>
用java.util.List也是可以的,java.util.Map也是可以的至于為什么我沒有沒有研究過
3.返回值為Map類型。(使用map要注意查詢結(jié)果的條數(shù),多條會報(bào)錯)
1. 如果查詢的結(jié)果是一條,我們可以把查詢的數(shù)據(jù)以{表字段名, 對應(yīng)的值}
方式存入到Map
中。
Map<String, Object> getEmpAsMapById(Integer id);
<!-- 注意這里的 resultType 返回值類型是 'map' --> <select id="getEmpAsMapById" resultType="map"> select * from t_employee where id = #{id} </select>
2. 如果查詢的結(jié)果是多條數(shù)據(jù),我們也可以把查詢的數(shù)據(jù)以{表中某一字段名, JavaBean}
方式來封裝成Map
。
// 查詢所有員工的信息,把數(shù)據(jù)庫中的 'id' 字段作為 key,對應(yīng)的 value 封裝成 Employee 對象 // @MapKey 中的值表示用數(shù)據(jù)庫中的哪個(gè)字段名作 key @MapKey("id") Map<Integer, Employee> getAllEmpsAsMap();
<!-- 注意 resultType 返回值類型,不再是 'map',而是 Map 的 value 對應(yīng)的 JavaBean 類型 --> <select id="getAllEmpsAsMap" resultType="employee"> select * from t_employee </select>
擴(kuò)展. 上面返回結(jié)果的形式都是基于查詢 (select
) 的,其實(shí)對于增刪改的操作也可以返回一定類型的數(shù)據(jù),比如Boolean
,Integer
等。
resultMap:
屬于自定義的映射,用于由于各種原因數(shù)據(jù)庫的字段名跟實(shí)體類的字段名不一致
1.Emp實(shí)體類的屬性:
private Integer empId; private String empName; private Integer age; private String gender;
2.表字段名:
emp_id | emp_name | age | gender | dept_id |
3.映射文件配置:
<!-- Emp getEmpById(@Param("emp_id") Integer emp_id);--> <select id="getEmpById" resultType="Emp"> select * from t_emp where emp_id = #{emp_id} </select>
4.執(zhí)行結(jié)果發(fā)現(xiàn)empId=null,empName=null
原因:數(shù)據(jù)庫表字段名中的emp_id,emp_name與實(shí)體類的屬性名empId,empName不一致,由數(shù)據(jù)庫不能映射到實(shí)體類屬性對應(yīng)的屬性名
1. 方式一 字段名設(shè)置別名
select emp_id empid from emp where emp_id = #{emp_id}
2. 方式二 下劃線映射為駝峰(在配置文件中配置)
<!--引入properties文件,此時(shí)就可以${屬性名}的方式訪問屬性值--> <properties resource="jdbc.properties"/> <!--配置mybatis自動轉(zhuǎn)換為駝峰式命名--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--開啟延遲加載--> <setting name="lazyLoadingEnabled" value="true" /> </settings>
3. 方式三 自定義映射resultMap
resultMap: 設(shè)置自定義的映射關(guān)系
id: 唯一標(biāo)識–>resultMap=" "
type: 處理映射關(guān)系的實(shí)體類的類型
標(biāo)簽:
id: 處理主鍵和實(shí)體類中屬性的映射關(guān)系
result: 處理普通字段和實(shí)體類中屬性的映射關(guān)系
column: 設(shè)置映射關(guān)系中的字段名,必須是sql中的某字段
property: 設(shè)置映射關(guān)系中的屬性的屬性名,必須為實(shí)體類中的屬性名
<resultMap id="empDeptMapResultMapOne" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> <result column="did" property="dept.did"></result> <result column="dname" property="dept.dname"></result> </resultMap> <select id="getEmpAndDept" resultMap="empDeptMapResultMapTwo"> select emp.*,dept.* from emp left join dept on emp.did = dept.did where emp.eid = #{eid} </select>
到此這篇關(guān)于Mybatis返回值(resultType&resultMap)的具體使用的文章就介紹到這了,更多相關(guān)Mybatis返回值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于SpringCloud分布式系統(tǒng)中實(shí)現(xiàn)冪等性的幾種方式
這篇文章主要介紹了關(guān)于SpringCloud分布式系統(tǒng)中實(shí)現(xiàn)冪等性的幾種方式,冪等函數(shù),或冪等方法,是指可以使用相同參數(shù)重復(fù)執(zhí)行,并能獲得相同結(jié)果的函數(shù),這些函數(shù)不會影響系統(tǒng)狀態(tài),也不用擔(dān)心重復(fù)執(zhí)行會對系統(tǒng)造成改變,需要的朋友可以參考下2023-10-10Java 枚舉類和自定義枚舉類和enum聲明及實(shí)現(xiàn)接口的操作
這篇文章主要介紹了Java 枚舉類和自定義枚舉類和enum聲明及實(shí)現(xiàn)接口的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式
這篇文章主要介紹了如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07spring mvc中的@ModelAttribute注解示例介紹
在Spring mvc中,注解@ModelAttribute是一個(gè)非常常用的注解,下面這篇文章主要給大家介紹了關(guān)于spring mvc中@ModelAttribute注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09