zoukankan      html  css  js  c++  java
  • 使用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();
    }


    }

  • 相关阅读:
    PAAS平台的web应用性能测试与分析
    thrift之TTransport层的分帧传输类TFramedTransport
    thrift之TTransport层的缓存传输类TBufferedTransport和缓冲基类TBufferBase
    thrift之TTransport层的堵塞的套接字I/O传输类TSocket
    ssh证书登录(实例详解)
    [转]个性化推荐技术的十大挑战
    Github上最全的APICloud开源前端框架效果盘点(转)
    api.connectionType 判断当前网络技术经验
    安卓苹果获取时间戳转成本地时间
    js获取网络图片的宽和高
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/8546241.html
Copyright © 2011-2022 走看看