之前的文章里面有對resultType和resultMap的簡單介紹這一期出點詳細的
resultType:
1,返回值為簡單類型。
? ? ? ? 直接使用resultType=“類型”,如string,Integer等。
String getEmpNameById(Integer id);
<!-- 指定 resultType 返回值類型時 String 類型的,string 在這里是一個別名,代表的是 java.lang.String 對于引用數據類型,都是將大寫字母轉小寫,比如 HashMap 對應的別名是 'hashmap'基本數據類型考慮到重復的問題,會在其前面加上 '_',比如 byte 對應的別名是 '_byte'--><select id="getEmpNameById" resultType="string">select username from t_employee where id = #{id}</select>
?2.返回值為List類型。
? ? ? ?使用resultType=“list元素的類型”,一般是實體類如User,也可以是Map,對應返回值類型是List<User> , List<Map<String,Object>>,不管是哪種,最終結果會根據接口返回值類型自動將多個 resultType指定的類型的元素(User或以一條記錄為一個Map)組裝成List。
List<User> getUser(String age);
<select id="getUser" resultType="User">select * from userwhereage = #{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要注意查詢結果的條數,多條會報錯)
1.?如果查詢的結果是一條,我們可以把查詢的數據以{表字段名, 對應的值}
方式存入到Map
中。
Map<String, Object> getEmpAsMapById(Integer id);
<!-- 注意這里的 resultType 返回值類型是 'map'--><select id="getEmpAsMapById" resultType="map">select * from t_employee where id = #{id}</select>
?
2.?如果查詢的結果是多條數據,我們也可以把查詢的數據以{表中某一字段名, JavaBean}
方式來封裝成Map
。
// 查詢所有員工的信息,把數據庫中的 'id' 字段作為 key,對應的 value 封裝成 Employee 對象// @MapKey 中的值表示用數據庫中的哪個字段名作 key@MapKey("id")Map<Integer, Employee> getAllEmpsAsMap();
<!--注意 resultType 返回值類型,不再是 'map',而是 Map 的 value 對應的 JavaBean 類型--><select id="getAllEmpsAsMap" resultType="employee">select * from t_employee</select>
?擴展.?上面返回結果的形式都是基于查詢 (select
) 的,其實對于增刪改的操作也可以返回一定類型的數據,比如Boolean
,Integer
等。
?resultMap:
?屬于自定義的映射,用于由于各種原因數據庫的字段名跟實體類的字段名不一致
1.Emp實體類的屬性:
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.執行結果發現empId=null,empName=null
原因:數據庫表字段名中的emp_id,emp_name與實體類的屬性名empId,empName不一致,由數據庫不能映射到實體類屬性對應的屬性名
?1. 方式一 字段名設置別名
select emp_id empid from emp where emp_id = #{emp_id}
?2. 方式二 下劃線映射為駝峰(在配置文件中配置)
<!--引入properties文件,此時就可以${屬性名}的方式訪問屬性值--><properties resource="jdbc.properties"/><!--配置mybatis自動轉換為駝峰式命名--><settings><setting name="mapUnderscoreToCamelCase" value="true"/><!--開啟延遲加載--><setting name="lazyLoadingEnabled" value="true" /></settings>
3. 方式三 自定義映射resultMap
resultMap: 設置自定義的映射關系
id: 唯一標識–>resultMap=" "
type: 處理映射關系的實體類的類型
標簽:
id: 處理主鍵和實體類中屬性的映射關系
result: 處理普通字段和實體類中屬性的映射關系
column: 設置映射關系中的字段名,必須是sql中的某字段
property: 設置映射關系中的屬性的屬性名,必須為實體類中的屬性名
<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>
?
?