• MyBatis 关系映射


    对象级联(一对一)四种方式(有两张表,一个学生表,一个地址表,一个学生对应一个地址)

    实体

    /**
    *简化写
    **/
    
    public class Student {
    
    	private Integer id;
    	private String name;
    	private Integer age;
    	private Address address;
    	
    	
    }
    
    public class Address {
    
    	private Integer id;
    	private String sheng;
    	private String shi;
    	private String qu;
    	
    	}
    	
    

      

    1.0 (不推荐使用)

    <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"/>  addressId是学生表的外键
    		<result property="address.sheng" column="sheng"/>
    		<result property="address.shi" column="shi"/>
    		<result property="address.qu" column="qu"/>
    	</resultMap>
    

      

    2.0(不推荐)

          <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> 
    

     

    3.0(不推荐)

          <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>
    

      

    4.0(推荐)

        <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.java.mappers.AddressMapper.findById"></association> addressId外键
    	</resultMap>
    

      

    package com.java.mappers;
    
    import com.java.model.Address;
    
    public interface AddressMapper {
    
    	public Address findById(Integer id);
    
    }
    

      

     

  • 相关阅读:
    和阿文一起学H5-文字云制作
    uml与数据库设计
    设计模式——面向对象设计原则
    koajs框架解决的问题
    mongodb
    javascript 获取select选中text,2种方法
    带参数跳转
    express表单提交和参数接收4种方式
    ejs模版的4种输出方式
    nodejs事件
  • 原文地址:https://www.cnblogs.com/nidegui/p/11083619.html
Copyright © 2020-2023  润新知