zoukankan      html  css  js  c++  java
  • 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);
    
    }
    

      

     

  • 相关阅读:
    前端工程部署
    xcode使用入门
    effective java
    java测试用例的编写
    记录一次随意操作数据库,插入新数据,导致与程序添加新数据时,引起的主键值重复问题。More than one row with the given identifier was found: 1690
    Spring.Net中的依赖注入(DI)
    记一次使用js中的this关键字在ajax中使用,因传入的this对象不同而引起的问题。
    代理模式
    异常: 指定的转换无效
    log4net.config配置启用的几种方式
  • 原文地址:https://www.cnblogs.com/nidegui/p/11083619.html
Copyright © 2011-2022 走看看