mybatis文件映射之利用association进行关联查询之分步查询(三)

mybatis文件映射之利用association进行关联查询之分步查询(三),第1张

概述上一节我们 有Employee.java和Department.java 有EmployeeMapper.java和EmployeeMapper.xml 现在新增:DepartmentMapper.j

上一节我们

有Employee.java和Department.java

有EmployeeMapper.java和EmployeeMapper.xml

现在新增:DepartmentMapper.java和DepartmentMapper.xml

EmployeeMapper接口中有这么一个方法:

public Employee getEmpByIDStep(Integer ID);

DepartmentMapper接口中有这么一个方法:

public Department getDeptByID(Integer ID);

DepartmentMapper.xml中:

<mapper namespace="com.gong.mybatis.dao.DepartmentMapper">    select ID="getDeptByID" resultType="com.gong.mybatis.bean.Department">        select * from tbl_department where ID=#{ID}    </select>mapper>

EmployeeMapper.xml中

="com.gong.mybatis.dao.EmployeeMapperPlus">        resultMap type="com.gong.mybatis.bean.Employee" ID="MySimpleByStep">        ID column="ID" property="ID"/>        result ="last_name"="lastname"association property="dept" select="com.gong.mybatis.dao.DepartmentMapper.getDeptByID"        column="d_ID">                    associationresultMap="getEmpByIDStep" resultMap        select * from tbl_employee where ID=#{ID}    >    >

说明:我们直接对association中的属性进行 *** 作。property属性还是指定所需要关联的Employee类中的成员属性的名称。select属性对应着DepartmentMapper.xml中的getDeptByID方法。column属性对应着传入到该方法中的参数,由于tbl_employee中的d_ID字段是和tbl_department中的ID字段相关联,我们可以传入d_ID。

最后进行测试:

package com.gong.mybatis.test;import java.io.IOException; java.io.inputStream; java.util.Map; org.apache.ibatis.io.Resources; org.apache.ibatis.session.sqlSession; org.apache.ibatis.session.sqlSessionFactory; org.apache.ibatis.session.sqlSessionFactoryBuilder; org.junit.Test; com.gong.mybatis.bean.Department; com.gong.mybatis.bean.Employee; com.gong.mybatis.dao.DepartmentMapper; com.gong.mybatis.dao.EmployeeMapperPlus;public class TestMybatis2 {        public sqlSessionFactory getsqlSessionFactory() throws IOException {        String resource = "mybatis-config.xml";        inputStream is = Resources.getResourceAsstream(resource);        return new sqlSessionFactoryBuilder().build(is);    }    @Test    voID test()  IOException {        sqlSessionFactory sqlSessionFactory = getsqlSessionFactory();        sqlSession openSession = sqlSessionFactory.openSession();        try {            EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.);            Employee employee = mapper.getEmpByIDStep(3);            System.out.println(employee);            System.out.println(employee.getDept());            openSession.commit();        } finally {            openSession.close();        }            }}

输出:

DEBUG 01-20 14:36:37,371 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 14:36:37,413 ====> Preparing: select * from tbl_department where ID=? (BaseJdbcLogger.java:145)
DEBUG 01-20 14:36:37,414 ====> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 14:36:37,418 <==== Total: 1 (BaseJdbcLogger.java:145)
DEBUG 01-20 14:36:37,419 <== Total: 1 (BaseJdbcLogger.java:145)
Employee [ID=3,lastname=小红,gender=0,email=xiaohong@qq.com,dept=Department [ID=1,deptname=开发部]]
Department [ID=1,deptname=开发部]

说明通过这种方式也是可以成功的。

总结

以上是内存溢出为你收集整理的mybatis文件映射之利用association进行关联查询分步查询(三) 全部内容,希望文章能够帮你解决mybatis文件映射之利用association进行关联查询之分步查询(三) 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1219043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存