mybatis级联

关于mybatis级联问题

1、一对多

比如现在有这样俩个表
学生表
院系表
院系与学生是一对多的关系 院系表的主键是学生表的外键。所以在写pojo类的时候需要 增加一个list属性来存放相关级联表的属性。那么便可以编写相关的
mapper.xml文件。下面给出大致过程

<mapper namespace="DeptNS">

    <resultMap id="DeptMap" type="Dept" >
        <id column="dname" property="dName"></id>
        <result property="note" column="d_note" ></result>
        <collection property="list" column="dname" select="StudentNS.findStudentByDept" ></collection>
    </resultMap>


    <select id="findDept" parameterType="String" resultMap="DeptMap" >
        SELECT  * FROM  dept WHERE  dname=#{dname}
    </select>

</mapper>

这个是StudentNS.findStudentByDept

  <select id="findStudentByDept" parameterType="String" resultType="Student">
        SELECT  sno as "sno",s_name as "sName",s_age as "sAge",s_sex as "sSex",s_dept as "sDept" FROM  student WHERE  s_dept=#{deptName}
    </select>

2、一对一关联
现在还是以学生和学院表为例
需求是:在以学号查学生的同时,会同时查到外键关联的学员表
这时候我们 的关系就是一对一
首先写好pojo类。查询首先是根据sno查学生信息然后根据查到的s_dept 再去查dept表的内容最后封装为一个Student 对象。
mapper中主要用的association标签
映射器代码如下

<resultMap id="StudentMap" type="Student" >
        <id column="sno" property="sno"></id>
        <result property="sName" column="s_name" ></result>
        <result property="sAge" column="s_age" ></result>
        <result property="sSex" column="s_sex" ></result>
       <result property="sDept" column="s_dept"></result>
        <association property="dept" column="s_dept" select="DeptNS.findDeptOnly">

        </association>

   </resultMap>


    <select id="findStudent" parameterType="Integer"  resultMap="StudentMap">
        SELECT  sno,s_name ,s_age ,s_sex ,s_dept  FROM  student
              WHERE sno=#{id}

    </select>

    <select id="findDeptOnly" parameterType="string" resultType="Dept">
        SELECT  dname as "dName",d_note as "note" FROM  dept WHERE  dname=#{str}
    </select>
MyBatis级联查询是指在查询操作中,通过一次查询关联的多个表格,并将结果映射到一个复杂的对象中。通常情况下,关联查询需要多次查询数据库并手动组装结果,而级联查询则通过MyBatis的特性来自动完成这个过程。 在MyBatis中,通过`<association>`元素处理一对一级联关系。例如,我们有两个表格`orders`和`users`,其中`orders`表格中有一个`user_id`字段,指向`users`表格中的`id`字段。我们可以通过以下方式进行一对一级联查询: ```xml <!-- 定义resultMap --> <resultMap id="orderResultMap" type="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <result property="userId" column="user_id"/> <!-- 一对一级联查询 --> <association property="user" column="user_id" select="com.example.mapper.UserMapper.selectById"/> </resultMap> <!-- 定义select语句 --> <select id="selectOrder" resultMap="orderResultMap"> select * from orders where id = #{id} </select> ``` 在上面的例子中,我们定义了一个`orderResultMap`的`resultMap`,其中通过`<association>`元素定义了一对一级联查询。`<association>`元素的`property`属性指定了要级联的属性名,`column`属性指定了关联的字段名,`select`属性指定了要执行的查询语句。 需要注意的是,在`<association>`元素中,我们可以通过`select`属性指定要执行的查询语句,也可以通过`resultMap`属性指定要使用的`resultMap`。如果使用`select`属性,MyBatis会自动执行查询语句并将结果映射到对应的属性中;如果使用`resultMap`属性,MyBatis会使用指定的`resultMap`进行映射。 除了一对一级联查询,MyBatis还支持一对多和多对多级联查询,分别通过`<collection>`和`<association>`元素实现。具体用法可以参考MyBatis官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值