mybatis 关联

一对多的映射:

<?xml version="1.0" encoding="UTF-8" ?>    

<!DOCTYPE mapper    

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="hwt.Mapper.DepMapper">

<!-- 注意,以下的typeEmpDep是在configuration中配置了别名 -->

<resultMap type="Dep" id="depResult">

<id property="depid" column="DEPID"/>

<result property="depname" column="DEPNAME"/>

<!-- 一对多映射,property:pojo类中对应的属性,

   ofType:对应的类型;

   cloumn:根据此字段来进行查询,在hwt.Mapper.EmpMapper.getEmpsByDepId会根据column来查找

   select:根据什么来查询主要是由namespace+selectId -->

<collection property="emps" ofType="Emp" column="depid" select="hwt.Mapper.EmpMapper.getEmpsByDepId"></collection>

         <!-- 也可以通过这种方式进行配置 -->

<!--<collection property="emps" ofType="Emp">

<id property="empId" column="EMPID"/>

<result property="empname" column="EMPNAME"/>

 </collection> -->

</resultMap>

</mapper> 

 

对应的pojo

import java.util.List;

public class Dep {

private int depid;

private String depname;

private List<Emp> emps//作为一对多的字段使用

public int getDepid() {

return depid;

}

public void setDepid(int depid) {

this.depid = depid;

}

public String getDepname() {

return depname;

}

public void setDepname(String depname) {

this.depname = depname;

}

public List<Emp> getEmps() {

return emps;

}

public void setEmps(List<Emp> emps) {

this.emps = emps;

}

}

 

 

多对一的映射关系:

<?xml version="1.0" encoding="UTF-8" ?>    

<!DOCTYPE mapper    

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="hwt.Mapper.EmpMapper">

<!-- 注意,以下的typeEmpDep是在configuration中配置了别名 -->

   <resultMap id="empResult" type="Emp"> 

  <id property="empid" column="empid" />

  <result property="empname" column="empname"/>

  <!-- 多对一的关系,注意resultMap的名字为 引用的namespace+resultMap的Id -->

  <association property="dep" column="depid" javaType="Dep" resultMap="hwt.Mapper.DepMapper.depResult"/>

   </resultMap>

   

   <select id="getAllEmps" resultMap="empResult">

<![CDATA[

select * from emp e join dep d on e.depid = d.depid

]]>

  </select>

 

  <!-- 这个是部门对象的id查找emp对象的语句,在一对多的 collection 的 select配置-->

  <select id="getEmpsByDepId" parameterType="int" resultType="Emp">

    <![CDATA[select * from emp where depid = #{id}]]>

   </select>

  

   <!-- resultMap设置为上面的结果集,便能得到多对一的关系,但是sql要使用连接查询 -->

   <select id="getEmpsByName" parameterType="string" resultMap="empResult">

        <!-- 模糊查询的方法:1,使用${};

                       2,CONCAT(CONCAT('%', #{empname}), '%'); -->

    <![CDATA[select * from emp e join dep d on e.depid = d.depid where empname like '%${empname}%']]>

   </select>

</mapper> 

 

 

对应的实体类:

package hwt.pojo;

public class Emp {

private int empid;

private String empname;

private Dep dep//部门类,作为多对一的字段

public int getEmpid() {

return empid;

}

public void setEmpid(int empid) {

this.empid = empid;

}

public String getEmpname() {

return empname;

}

public void setEmpname(String empname) {

this.empname = empname;

}

public Dep getDep() {

return dep;

}

public void setDep(Dep dep) {

this.dep = dep;

}

}

 

 

对于延迟加载,需要在configuration.xml中配置:

<!-- lazyLoadingEnabled:true使用延迟加载,false禁用延迟加载。默认为true;

                aggressiveLazyLoading:

true启用时,当延迟加载开启时访问对象中一个懒对象属性时,                                                                                                      将完全加载这个对象的所有懒对象属性。

false,当延迟加载时,按需加载对象属性(即访问对象中一个懒对象属性,不会加载对象中其他的懒对象属性)。

                                                                                                   默认为true  -->

  <setting name="lazyLoadingEnabled" value="true"/>

  <setting name="aggressiveLazyLoading" value="false"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值