Mybatis 一对一 多对一

 <!-- <resultMap type="employee" id="emplist">
       <id property="ids" column="IDS"/>
       <result property="ename" column="ENAME"/>
       <result property="esex" column="ESEX"/>
       <result property="birthday" column="BIRTHDAY"/>
       <result property="address" column="ADDRESS"/>
       <result property="dept.id" column="ID"/>
       <result property="dept.dname" column="DNAME"/>
       <result property="dept.createtime" column="CREATETIME"/>
     </resultMap> -->

  这个是级联查询  一次性把两个表的内容都累出来

<!--<resultMap type="employee" id="emplist">
       <id property="ids" column="IDS"/>
       <result property="ename" column="ENAME"/>
       <result property="esex" column="ESEX"/>
       <result property="birthday" column="BIRTHDAY"/>
       <result property="address" column="ADDRESS"/>
       <association property="dept" resultMap="deptlist"></association>   
     </resultMap>
     <resultMap type="dept" id="deptlist">
        <result property="id" column="ID"/>
       <result property="dname" column="DNAME"/>
       <result property="createtime" column="CREATETIME"/>
     </resultMap> -->

  

 <!--  <resultMap type="employee" id="emplists">
       <association property="dept" column="deptid" select="mapper.DeptMapper.selectDept"></association>
     </resultMap>
     <select id="selectAllEmployee" resultMap="emplists" >
         SELECT * FROM EMPLOYEE E JOIN DEPT D ON E.DEPTID=D.ID 
     </select> -->



  <resultMap type="employee" id="myemp">
        <id column="ids" property="ids"/>
        <result column="ename" property="ename" />
        <result column="esex" property="esex" />
        <result column="birthday" property="birthday"/>
        <result column="address" property="address"/>
        <association property="dept" column="deptid" select="mapper.DeptMapper.selectDept"></association>
     </resultMap>
     
     <select id="selectEmployee" resultMap="myemp">
       select * from employee e where e.ids=#{id}
     </select>

  这两个都是分布查询   多对一也是这样的  只是association 换成collection

 <resultMap type="dept" id="mydept">
       <id property="id" column="id"/>
       <result property="dname" column="dname"/>
       <result property="createtime" column="createtime"/>
       <collection property="list" column="id" select="mapper.EmployeeMapper.selectOneEmployee"></collection>
    </resultMap>
    <select id="selectOneDept" parameterType="Integer"  resultMap="mydept">
        select * from dept d where d.id=#{id}
    </select>

  多对一查询

联合查询一般用分布查询  resultmap标签中 type类型就是想要查询的类型  id随便写 是resultmap的唯一标识,collection标签中 property 属性指定的是联合查询的是哪个 column传入的指定的值 工select查询

resultmap 是制定封装规则  所以定义哪个对象的封装规则 那么 这个标签的type属性就写哪个

mybatis 需要打印日志的时候可以导入mybatis 的日志包 放在lib文件夹下  然后再src文件夹下放一个log4g.

properties文件

原文地址:https://www.cnblogs.com/gaofangquan/p/7571751.html