詳解MyBatis resultType與resultMap中的幾種返回類型
一、返回集合
1.返回JavaBean集合
public List<MyUser> selectMyUserByNameLike(String name);
<!-- resultType 集合內(nèi)的元素類型 --> <select id="selectMyUserByNameLike" resultType="myUser" parameterType="string"> select * from myuser where name like #{name} </select>
測試方法
public static void main(String[] args) { SqlSession session = null; try { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); session = sqlSessionFactory.openSession(); MyUserMapper mapper = session.getMapper(MyUserMapper.class); List<MyUser> myUsers = mapper.selectMyUserByNameLike("%a%"); System.out.println(myUsers); } catch (IOException e) { e.printStackTrace(); } finally { if (session != null) { session.close(); } } }
2.返回 Map 集合
<!--public List<Map<String,Object>> getMyUser()--> <select id="getMyUser" resultType="map"> select * from myuser </select>
二、返回 Map
1.一條記錄
public Map<String,Object> selectMyUserById(Integer id);
<select id="selectMyUserById" resultType="map" parameterType="integer"> select * from myuser where id = #{id} </select>
2.多條記錄,需要指定 Map 的 Key 和 Value 的類型
// 指定 Map 的 Key 從記錄中的 id 列獲取 @MapKey("id") public Map<String,MyUser> selectMyUserByGtId(Integer id);
<!-- resultType Map 中 value 的類型 --> <select id="selectMyUserByGtId" resultType="myUser" parameterType="integer"> select * from myuser where id > #{id} </select>
三、返回 resultMap 自定義結(jié)果集封裝
關(guān)于自動映射封裝的配置
<settings> <!-- 自動映射有三種模式,NONE、PARTIAL、FULL。NONE 不啟用自動映射,PARTIAL 只對非嵌套的 resultMap 進(jìn)行自動映射,F(xiàn)ULL 表示對所有的 resultMap 都進(jìn)行自動映射。默認(rèn)為 PARTIAL --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 數(shù)據(jù)庫字段下劃線轉(zhuǎn)Bean字段的駝峰命名 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 控制臺打印SQL --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings>
默認(rèn)數(shù)據(jù)庫字段與 JavaBean 對應(yīng)不上時可開啟駝峰命名或查詢時使用別名http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html
1.自定義 JavaBean 的封裝
確認(rèn)是否成功可以關(guān)掉 MyBatis 的自動映射
<setting name="autoMappingBehavior" value="NONE"/>
public MyUser selectMyUserById(Integer id);
<!-- 自定義某個 javaBean 的封裝規(guī)則 type:自定義規(guī)則中JavaBean類型的全路徑,可用別名 id:唯一id方便引用 --> <resultMap type="myUser" id="myUserResultMap"> <!-- 指定主鍵列的封裝規(guī)則,用 id 標(biāo)簽定義主鍵會底層有優(yōu)化 column:指定哪一列 property:指定對應(yīng)的javaBean屬性 --> <id column="id" property="id"/> <!-- 定義普通列封裝規(guī)則 --> <result column="name" property="name"/> <!-- 其他不指定的列會自動封裝:建議只要寫 resultMap 就把全部的映射規(guī)則都寫上 --> <result column="age" property="age"/> </resultMap> <!-- 使用 resultMap,不使用 resultType --> <select id="selectMyUserById" resultMap="myUserResultMap" parameterType="integer"> select * from myuser where id = #{id} </select>
2.關(guān)聯(lián)查詢的封裝,一對一,JavaBean 屬性包含 JavaBean
public MyUser selectMyUserById(Integer id);
直接調(diào)用屬性賦值
<resultMap type="myUser" id="myUserResultMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <!--直接屬性封裝--> <result column="did" property="dept.id"/> <result column="dname" property="dept.name"/> </resultMap> <select id="selectMyUserById" resultMap="myUserResultMap" parameterType="integer"> SELECT m.id, m.name, m.age, m.did, d.name AS dname FROM myuser m,dept d WHERE m.did = d.id AND m.id = #{id} </select>
使用association
<resultMap type="com.bean.MyUser" id="myUserResultMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <!-- association 指定聯(lián)合的 javaBean 對象 property="dept":指定哪個屬性是聯(lián)合的對象 javaType:指定這個屬性對象的類型[不能省略] --> <association property="dept" javaType="com.bean.Dept"> <id column="did" property="id"/> <result column="dname" property="name"/> </association> </resultMap> <select id="selectMyUserById" resultMap="myUserResultMap" parameterType="integer"> SELECT m.id, m.name, m.age, m.did, d.name AS dname FROM myuser m,dept d WHERE m.did = d.id AND m.id = #{id} </select>
使用association 二次查詢,即有兩條 SQL
<resultMap id="myUserResultMap" type="com.bean.MyUser"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <!-- association 定義關(guān)聯(lián)對象的封裝規(guī)則 select:當(dāng)前屬性是調(diào)用 select 指定的方法查出的結(jié)果 column:將哪一列的值傳給這個方法 --> <association property="dept" select="com.dao.DeptMapper.selectDeptById" column="did"/> </resultMap> <select id="selectMyUserById" resultMap="myUserResultMap" parameterType="integer"> SELECT * FROM myuser WHERE id = #{id} </select>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace 對應(yīng)接口文件的全路徑 --> <mapper namespace="com.dao.DeptMapper"> <select id="selectDeptById" resultType="dept" parameterType="string"> select * from dept where id = #{id} </select> </mapper>
開啟懶加載:在沒有使用 Dept 的屬性時,則只會加載 MyUser 的屬性。即只會發(fā)送一條 SQL 語句,要使用Dept 屬性時才會發(fā)送第二條 SQL。不會一次性發(fā)送兩條 SQL
<!-- 延遲加載的全局開關(guān)。當(dāng)開啟時,所有關(guān)聯(lián)對象都會延遲加載。 特定關(guān)聯(lián)關(guān)系中可通過設(shè)置fetchType屬性來覆蓋該項(xiàng)的開關(guān)狀態(tài)。默認(rèn)false --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 當(dāng)開啟時,任何方法的調(diào)用都會加載該對象的所有屬性。否則,每個屬性會按需加載。默認(rèn)false (true in ≤3.4.1) --> <setting name="aggressiveLazyLoading" value="false"/>
3.關(guān)聯(lián)查詢的封裝,一對多,JavaBean 屬性包含 JavaBean 的集合
使用association
public Dept getDeptById(Integer id);
<resultMap type="com.bean.Dept" id="MyDept"> <id column="did" property="id"/> <result column="dname" property="name"/> <!-- collection 定義關(guān)聯(lián)集合類型的屬性封裝規(guī)則 ofType 指定集合里面元素的類型 --> <collection property="myUsers" ofType="com.bean.MyUser"> <!-- 定義集合中元素的封裝規(guī)則 --> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> </collection> </resultMap> <select id="getDeptById" resultMap="MyDept"> SELECT m.id,m.name,m.age,m.did,d.name AS dname FROM myuser m,dept d WHERE m.did = d.id AND d.id = #{id} </select>
關(guān)閉懶加載,使用二次查詢
public Dept getDeptByIdStep(Integer did);
<!-- Collection 分段查詢 --> <resultMap type="com.bean.Dept" id="MyDeptStep"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="myUsers" select="com.dao.MyUserMapper.selectMyUserByDid" column="{did=id}" fetchType="eager"/> <!-- column 要處理復(fù)合主鍵或傳遞多個值過去:可以將多列的值封裝 Map 傳遞,指定多個列名通過 column="{prop1=col1,prop2=col2}" 語法來傳遞給嵌套查詢語句。這會引起 prop1 和 prop2 以參數(shù)對象形式來設(shè)置給目標(biāo)嵌套查詢語句 fetchType="lazy":是否延遲加載,優(yōu)先級高于全局配置,lazy:延遲,eager:立即 --> </resultMap> <select id="getDeptByIdStep" resultMap="MyDeptStep"> select * from dept where id = #{id} </select>
public List<MyUser> selectMyUserByDid(Integer dId);
<select id="selectMyUserByDid" resultType="myUser"> select * from myuser where dId = #{did} </select>
4.鑒別器discriminator
<!--public MyUser selectMyUserById(Integer id);--> <select id="selectMyUserById" resultMap="MyEmpDis" parameterType="integer"> SELECT * FROM myuser WHERE id = #{id} </select> <resultMap id="MyEmpDis" type="com.bean.MyUser"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <!-- column:指定判定的列名 javaType:列值對應(yīng)的java類型 --> <discriminator javaType="integer" column="age"> <!-- 21歲 封裝課程至 JavaBean --> <case value="21" resultType="com.bean.MyUser"> <association property="dept" select="com.dao.DeptMapper.selectDeptById" column="did"/> </case> <!-- 33歲 不封裝課程至 JavaBean 且把 age 賦值給 id --> <case value="33" resultType="com.bean.MyUser"> <result column="age" property="id"/> </case> </discriminator> </resultMap>
<!--public Dept selectDeptById(Integer id);--> <select id="selectDeptById" resultType="dept" parameterType="string"> select * from dept where id = #{id} </select>
上面測試中使用的實(shí)體類與數(shù)據(jù)
public class Dept { private Integer id; private String name; private List<MyUser> myUsers; public class MyUser { private Integer id; private String name; private Integer age; private Dept dept;
https://mybatis.org/mybatis-3/zh/sqlmap-xml.html
到此這篇關(guān)于詳解MyBatis resultType與resultMap中的幾種返回類型的文章就介紹到這了,更多相關(guān)MyBatis resultType與resultMap返回類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
有關(guān)ServletConfig與ServletContext的訪問
下面小編就為大家?guī)硪黄嘘P(guān)ServletConfig與ServletContext的訪問。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Spring MVC創(chuàng)建項(xiàng)目踩過的bug
這篇文章主要介紹了Spring MVC創(chuàng)建項(xiàng)目踩過的bug,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11基于Properties類操作.properties配置文件方法總結(jié)
這篇文章主要介紹了Properties類操作.properties配置文件方法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java如何使用httpclient檢測url狀態(tài)及鏈接是否能打開
這篇文章主要介紹了Java如何使用httpclient檢測url狀態(tài)及鏈接是否能打開,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Lombok為啥這么牛逼?SpringBoot和IDEA官方都要支持它
Lombok是一款Java代碼功能增強(qiáng)庫,在Github上已有9.8k+Star。這篇文章主要介紹了Lombok為啥這么牛逼?SpringBoot和IDEA官方都要支持它,需要的朋友可以參考下2020-12-12Mapstruct?@Mapper?@Mapping?使用小結(jié)
這篇文章主要介紹了Mapstruct?@Mapper?@Mapping使用小結(jié),他們用于各個對象實(shí)體間的相互轉(zhuǎn)換,例如數(shù)據(jù)庫底層實(shí)體轉(zhuǎn)為頁面對象,Model?轉(zhuǎn)為?DTO,?DTO?轉(zhuǎn)為其他中間對象,?VO?等等,相關(guān)轉(zhuǎn)換代碼為編譯時自動產(chǎn)生的新文件和代碼,需要的朋友可以參考下2023-09-09