日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用resultMap定义查询结果集,实现关联查询

發布時間:2025/7/25 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用resultMap定义查询结果集,实现关联查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

接下來介紹resultMap定義查詢結果集,實現關聯查詢

1 首先在接口中定義操作的方法

public interface EmployeeMapperPlus {

public Employee getEmpAndDept(Integer id);

}

2在xml里進行配置

<!--第一種進行配置

聯合查詢:級聯屬性封裝結果集
-->
<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="departmentName" property="dept.departmentName"/>
</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="departmentName" property="departmentName"/>
</association>
</resultMap>
<!-- public Employee getEmpAndDept(Integer id);-->
<select id="getEmpAndDept" resultMap="MyDifEmp2">
SELECT e.id id,e.last_name last_name,e.email email ,e.gender gender,e.did eedid,
d.id did,d.dname departmentName FROM tbl_employee e,department d
WHERE e.did=d.id AND e.id=#{id}
</select>

3在junit里進行測試

@Test
public void test05() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
Employee empAndDept = mapper.getEmpAndDept(1);
System.out.println(empAndDept);
System.out.println(empAndDept.getDept());

}finally{
openSession.close();
}


}

轉載于:https://www.cnblogs.com/zhangzhiqin/p/8546241.html

總結

以上是生活随笔為你收集整理的使用resultMap定义查询结果集,实现关联查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。