Mybatis的collection和association用法

1.表实体之间的关系

Mybatis的collection和association用法_第1张图片

 

2.实体类

@Data
@EqualsAndHashCode(callSuper = false)
public class TestOne implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;    //
    private String nickname;    //
    private List testTwos;
}


@Data
@EqualsAndHashCode(callSuper = false)
public class TestTwo implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;    //
    private String nickname;    //
    private Integer oneId;    //
    private TestOne testOne;
}

3.Association:  一对一查询的方式


        
        
        
        
            
            
        
    

    

 

4.Collection:  一对多查询的方式


        
        
        
            
            
            
        
    

    

 

5.建表语句

CREATE TABLE `test_one` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nickname` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;



CREATE TABLE `test_two` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nickname` varchar(255) NOT NULL,
  `one_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `test_two_ibfk_1` (`one_id`),
  CONSTRAINT `test_two_ibfk_1` FOREIGN KEY (`one_id`) REFERENCES `test_one` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

 

你可能感兴趣的:(java,mybatis)