mybatis:级联(一对一)

以下知识来源说明:http://www.java1234.com/

MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:

  • property:对象属性的名称
  • javaType:对象属性的类型
  • column:所对应的外键字段名称
  • select:使用另一个查询封装的结果

entity类:Student

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	private Address address;
    
        Getters and Setters……

        toString……
}

entity类:Address

public class Address {

	private Integer id;
	private String sheng;
	private String shi;
	private String qu;

        Getters and Setters……

        toString……
}

 Mapping:方法一

<resultMap type="Student" id="StudentResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    
    <result property="address.id" column="addressId"/>
    <result property="address.sheng" column="sheng"/>
    <result property="address.shi" column="shi"/>
    <result property="address.qu" column="qu"/>
</resultMap>

 Mapping:方法二

 <resultMap type="Address" id="AddressResult">
    <result property="id" column="id"/>
    <result property="sheng" column="sheng"/>
    <result property="shi" column="shi"/>
    <result property="qu" column="qu"/>
</resultMap>

<resultMap type="Student" id="StudentResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" resultMap="AddressResult"/>
</resultMap> 

 Mapping:方法三

<resultMap type="Student" id="StudentResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" javaType="Address">
        <result property="id" column="id"/>
        <result property="sheng" column="sheng"/>
        <result property="shi" column="shi"/>
        <result property="qu" column="qu"/>
    </association>
</resultMap> 

 Mapping:方法四

StudentMapper.xml

<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" column="addressId" select="com.java1234.mappers.AddressMapper.findById"></association>
</resultMap>

<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
    select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>

AddressMapper.xml

<resultMap type="Address" id="AddressResult">
    <result property="id" column="id"/>
    <result property="sheng" column="sheng"/>
    <result property="shi" column="shi"/>
    <result property="qu" column="qu"/>
</resultMap>

<select id="findById" parameterType="Integer" resultType="Address">
    select * from t_address where id=#{id}
</select>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis支持通过级联查询来实现一对一关系的查询。在进行一对一级联查询时,需要使用MyBatis提供的`association`标签来配置关联关系。 首先,需要在映射文件中定义两个相关的结果映射。一个是主实体对象的映射,另一个是关联实体对象的映射。例如,我们有一个订单(Order)实体和一个用户(User)实体,订单有一个外键指向用户表。 ```xml <!-- 订单实体映射 --> <resultMap id="orderResultMap" type="com.example.Order"> <id property="id" column="order_id" /> <result property="orderNo" column="order_no" /> <association property="user" javaType="com.example.User"> <id property="id" column="user_id" /> <result property="username" column="username" /> <result property="email" column="email" /> </association> </resultMap> <!-- 用户实体映射 --> <resultMap id="userResultMap" type="com.example.User"> <id property="id" column="user_id" /> <result property="username" column="username" /> <result property="email" column="email" /> </resultMap> ``` 然后,在查询订单时,使用`association`标签配置一对一关联查询: ```xml <select id="getOrderById" resultMap="orderResultMap"> SELECT o.id as order_id, o.order_no, u.id as user_id, u.username, u.email FROM orders o INNER JOIN users u ON o.user_id = u.id WHERE o.id = #{orderId} </select> ``` 这样,当执行`getOrderById`查询时,MyBatis会自动进行一对一级联查询,将订单对象的关联用户对象填充进去。 注意:以上示例中的表名、列名等需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值