日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2025/7/25 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用resultMap定义查询结果集,实现关联查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

接下來(lái)介紹resultMap定義查詢結(jié)果集,實(shí)現(xiàn)關(guān)聯(lián)查詢

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

public interface EmployeeMapperPlus {

public Employee getEmpAndDept(Integer id);

}

2在xml里進(jìn)行配置

<!--第一種進(jìn)行配置

聯(lián)合查詢:級(jí)聯(lián)屬性封裝結(jié)果集
-->
<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定義關(guān)聯(lián)的單個(gè)對(duì)象的封裝規(guī)則;
-->
<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可以指定聯(lián)合的javaBean對(duì)象
property="dept":指定哪個(gè)屬性是聯(lián)合的對(duì)象
javaType:指定這個(gè)屬性對(duì)象的類型[不能省略]
-->
<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里進(jìn)行測(cè)試

@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();
}


}

轉(zhuǎn)載于:https://www.cnblogs.com/zhangzhiqin/p/8546241.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。