1.返回一個對象
public interface EmployeeMapper {
public Employee getEmpByMap(Map<String, Object> map);
}
對應的EmployeeMapper.xml
語句
<!-- public Employee getEmpByMap(Map<String, Object> map); -->
<select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from ${tableName} where id=${id} and last_name=#{lastName}
</select>
返回值為該對象的類型
2.返回一個集合
public List<Employee> getEmpsByLastNameLike(String lastName);
對應的xml
語句
<!-- public Employee getEmpByMap(Map<String, Object> map); -->
<select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from ${tableName} where id=${id} and last_name=#{lastName}
</select>
返回值為集合中元素的類型
3.返回一個map集合
//返回一條記錄的map;key就是列名,值就是對應的值
public Map<String, Object> getEmpByIdReturnMap(Integer id);
對應的xml
語句
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); -->
<select id="getEmpByIdReturnMap" resultType="map">
select * from tbl_employee where id=#{id}
</select>
這里查找到tbl_employee
中的一條記錄,可以直接返回一個Employee
對象,也可以直接返回一個map集合
此時map
中的鍵就是列名,如id
、last_name
,值則就是該行記錄中對應的值。
4.返回一個定制的map
//多條記錄封裝一個map:Map<Integer,Employee>:鍵是這條記錄的主鍵,值是記錄封裝后的javaBean
//@MapKey:告訴mybatis封裝這個map的時候使用哪個屬性作為map的key
@MapKey("lastName")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
對應的xml
語句
<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); -->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select>
該sql查找到多條記錄,其實也就對應着多個Employee,那如何封裝成一個map集合,使其值為Employee,鍵為我們定制的值呢?
可以在查詢語句中加上@MapKey("lastName")
,當然你也可以替換成主鍵id
以上這些其實都是resultType的用法
思考:表中的 一條記錄通過restultType=“Employee”將結果與javaBean
自動對應了起來,達到了自動映射的效果
當自動映射查詢結果時,MyBatis 會獲取結果中返回的列名並在 Java 類中查找相同名字的屬性(忽略大小寫)。 這意味着如果發現了 ID 列和 id 屬性,MyBatis 會將列 ID 的值賦給 id 屬性。
通常數據庫列使用大寫字母組成的單詞命名,單詞間用下划線分隔;而 Java 屬性一般遵循駝峰命名法約定。為了在這兩種命名方式之間啟用自動映射,需要將 mapUnderscoreToCamelCase
設置為 true。
那如果返回的數據與javaBean不一致呢,比如我們關聯查詢了多張表,返回了各表中的部分字段,難道我們要往javaBean
添加一些屬性么?
那這里或許resultMap
就能派上用場了,這是一種手動映射。
現在我們來看resultMap
自定義結果集映射規則
// mapper接口與mapper.xml進行綁定
public interface EmployeeMapperPlus {
public Employee getEmpById(Integer id);
public Employee getEmpAndDept(Integer id);
public Employee getEmpByIdStep(Integer id);
public List<Employee> getEmpsByDeptId(Integer deptId);
}
以下為其基本用法:
<!--自定義某個javaBean的封裝規則
type:自定義規則的Java類型
id:唯一id方便引用
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
<!--指定主鍵列的封裝規則
id定義主鍵會底層有優化;
column:指定哪一列
property:指定對應的javaBean屬性
-->
<id column="id" property="id"/>
<!-- 定義普通列封裝規則 -->
<result column="last_name" property="lastName"/>
<!-- 其他不指定的列會自動封裝:推薦我們只要寫resultMap就把全部的映射規則都寫上。 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<!-- resultMap:自定義結果集映射規則; -->
<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById" resultMap="MySimpleEmp">
select * from tbl_employee where id=#{id}
</select>
場景一:
查詢Employee的同時查詢員工對應的部門 Employee===Department 一個員工有與之對應的部門信息; 屬性: id last_name gender d_id did dept_name (private Department dept;)
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept; //一個部門屬性
}
public class Department {
private Integer id;
private String departmentName;
private List<Employee> emps;
}
以下是sql
<!-- public Employee getEmpAndDept(Integer id);-->
<select id="getEmpAndDept" resultMap="MyDifEmp">
SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
WHERE e.d_id=d.id AND e.id=#{id}
</select>
那如何封裝resultMap
?
<!--
聯合查詢:級聯屬性封裝結果集
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
可見resultMap
支持:級聯封裝
還可以這么封裝
<!--
使用association定義關聯的單個對象的封裝規則;
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<!-- association可以指定聯合的javaBean對象
property="dept":指定哪個屬性是聯合的對象
javaType:指定這個屬性對象的類型[不能省略]
-->
<association property="dept" javaType="com.atguigu.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>