欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis不同Mapper文件引用resultMap实例代码

程序员文章站 2023-12-18 21:45:10
classesmapper.xml:

classesmapper.xml:

<resultmap type="classes" id="classesmap">
    <id property="id" column="c_id" javatype="int"/>
    <result property="name" column="c_name" javatype="string"/>
<!--  <association property="teacher" javatype="teacher" >
      <id property="id" column="t_id" javatype="int"/>
      <result property="name" column="t_name" javatype="string"/>
      <result property="age" column="t_age" javatype="int"/>
    </association> -->
    <association property="teacher" javatype="teacher" resultmap="com.lsj.test.mybatis.mapper.teachermapper.teachermap" ></association>
    <collection property="studentlist" oftype="student" resultmap="com.lsj.test.mybatis.mapper.studentmapper.studentmap" ></collection>   
  </resultmap>
  <sql id="selectallclassessql">
    select 
        c.id as c_id,c.name as c_name,teacher_id,
        t.id as t_id,t.name as t_name,t.age as t_age,
        s.id as stu_id,s.name as stu_name,s.classes_id 
    from classes c,teacher t,student s
  </sql>

teachermapper.xml的resultmap:

 <resultmap type="teacher" id="teachermap">
    <id property="id" column="t_id" javatype="int"/>
    <result property="name" column="t_name" javatype="string"/>
    <result property="age" column="t_age" javatype="int"/> 
  </resultmap>

利用命名空间+resultmapid就可以直接引用其他mapper文件的resultmap,其中resultmap的column在本文件中也有用,也可以对应sql语句中的column别名。

总结

以上所述是小编给大家介绍的mybatis 不同mapper文件引用resultmap,希望对大家有所帮助

上一篇:

下一篇: