1.查詢補充 當你查詢一條記錄并且是簡單查詢時,情況相對簡單,可以參考以下的例子: public Employee getEmpById(Integer id); 對應的xml文件中:
<select id="getEmpById" resultType="com.mybatis.learn.bean.Employee">select id, last_name lastName, gender, email from tbl_employee where id = #{id}</select>
當查詢多條記錄時,可以參考以下方式:、
public List<Employee> getListByGender(String gender);public Map<String, Object> getMapById(Integer id);//該注解時指定封存記錄的map的key@MapKey("lastName")public Map<String, Employee> getMap(String gender);
xml中:
<!-- 查詢多條記錄時,若是用list封裝結果,resultType填寫list中的每條記錄的泛型的全類名 --><select id="getListByGender" resultType="com.mybatis.learn.bean.Employee">select * from tbl_employee where gender=#{gender}</select><!-- map存儲查詢結果時,單條記錄可以直接在resultType中寫map --><select id="getMapById" resultType="map">select * from tbl_employee where id=#{id}</select><!-- map存錯多條查詢結果時,reusltType指定的也是里面每條記錄的泛型的全類名 --><select id="getMap" resultType="com.mybatis.learn.bean.Employee">select * from tbl_employee where gender=#{gender}</select>
2.resultType&resultMap 如果是簡單查詢,推薦使用resultType(resultMap也能使用,但是比較麻煩),使用方式在前面演示過了。
resultMap除了可以使用在簡單查詢情況下,也能使用在resultType不能勝任的地方,如:聯合查詢,關聯查詢等,舉個例子:
查詢員工信息時包含部門信息:
新建javaBean: package com.mybatis.learn.bean;import lombok.*;@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Department {private Integer deptId;private String deptName;
} 在原來的Employee類添加新的屬性department以及getter setter
private Department deparment; 建表sql:
CREATE TABLE `tbl_dept` (`dept_id` int(11) unsigned NOT NULL,`dept_name` varchar(255) DEFAULT NULL,PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
修改表tbl_employee ,新增字段dept_id; 此時,由于全部字段存在于兩個表中,需要關聯查詢,已經創建的JavaBean沒有一個能滿足要 求,這時可以結合resultMap創建一個: 還是在EmployeeMapper.xml文件中:
<!--
場景一:查詢Employee的同時查詢員工對應的部門Employee===Department一個員工有與之對應的部門信息;id last_name gender dept_id dept_name (private Department dept;)--><!--聯合查詢:級聯屬性封裝結果集resultMap:id標簽:指定主鍵;result標簽:指定普通列column:指定數據庫中的列名property:指定對應的JavaBean中的屬性名其實,指定id后再指定和數據庫不一樣的字段即可,不過推薦全部指定--><!-- 方式一:關聯查詢--><resultMap id="myEmp" type="com.mybatis.learn.bean.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><result column="dept_id" property="dept.deptId"/><result column="dept_name" property="dept.deptName"/></resultMap><!-- 方式二:使用association定義關聯的單個對象的封裝規則;--><resultMap id="myEmp2" type="com.mybatis.learn.bean.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><!--association可以指定聯合的javaBean對象property="dept":指定哪個屬性是聯合的對象javaType:指定這個屬性對象的類型[不能省略]--><association property="dept" javaType="com.mybatis.learn.bean.Department"><id column="dept_id" property="deptId"/><result column="dept_name" property="deptName"/></association></resultMap><select id="getFullEmpById" resultMap="myEmp">SELECT e.id, e.last_name, e.gender, e.email, d.dept_id, d.dept_nameFROM tbl_employee e LEFT JOIN tbl_dept d ON e.dept_id = d.dept_idWHERE id = #{id}</select><select id="getFullEmp2ById" resultMap="myEmp2">SELECT e.id, e.last_name, e.gender, e.email, d.dept_id, d.dept_nameFROM tbl_employee e LEFT JOIN tbl_dept d ON e.dept_id = d.dept_idWHERE id = #{id}</select> 更改查詢Mapper: public Employee getFullEmp2ById(Integer id);public Employee getFullEmpById(Integer id); 這時可以分別測試一下了。
當然,resultMap的意義還不止于此,比如你想分布查詢時:
首先開啟延遲加載:更改mybatis-config.xml文件: <!--設置延遲加載顯示的指定每個我們需要更改的配置的值,即使他是默認的。防止版本更新帶來的問題 --><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/> 原理就是在一個查詢中嵌套一個查詢,并且在需要關聯的字段時才去執行嵌套的查詢: //EmployeeMapper中新增的方法public Employee getEmpByStep(Integer id);//EmployeeMapper.xml中新增的內容:
<resultMap id="myEmpByStep" type="com.mybatis.learn.bean.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><association column="dept_id" property="dept"select="com.mybatis.learn.dao.DepartmentMapper.getDeptById"/></resultMap><select id="getEmpByStep" resultMap="myEmpByStep">select * from tbl_employee where id=#{id}</select>//你也發現了問題對不對,沒有對應的嵌套的sql,OK,現在補齊:
//新增DepartmentMapper:
import com.mybatis.learn.bean.Department;public interface DepartmentMapper {public Department getDeptById(Integer deptId);
}//以及對應的xml:<select id="getDeptById" resultType="com.mybatis.learn.bean.Department">select * from tbl_dept where dept_id=#{deptId}</select>
ok,如何證明分布查詢呢: 下面的例子將會證明:
@Testpublic void testGetEmpByStep() {String resources = "mybatis-config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resources);} catch (IOException e) {e.printStackTrace();}SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession session = sessionFactory.openSession(true);EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);Employee emp = mapper.getEmpByStep(1);System.out.println(emp.getEmail());System.out.println(emp.getDept());}
執行結果如下:第一次查詢,不需要查詢dept DEBUG 03-23 15:51:26,066 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:159) DEBUG 03-23 15:51:26,134 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159) DEBUG 03-23 15:51:26,231 <== Total: 1 (BaseJdbcLogger.java:159)第一次的查詢結果,打印了email eeee需要dept時,執行對應的sql DEBUG 03-23 15:51:26,232 ==> Preparing: select * from tbl_dept where dept_id=? (BaseJdbcLogger.java:159) DEBUG 03-23 15:51:26,233 ==> Parameters: 1(Long) (BaseJdbcLogger.java:159) DEBUG 03-23 15:51:26,236 <== Total: 1 (BaseJdbcLogger.java:159) Department(deptId=1, deptName=組織部)
轉載于:https://www.cnblogs.com/JackHou/p/10584433.html
總結
以上是生活随笔 為你收集整理的MyBatis3系列__05查询补充resultMap与resultType区别 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。