sharp-database中的BaseDAOImpl实现一对一外键关联@OneToMany @ManyToOne

实体

http://xhope.top/wp-content/uploads/2022/05/image.png

IdCard.java

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @SuperBuilder
  6. @Table("t_person_id_card")
  7. public class IdCard extends BasePureEntity {
  8. private String idNum;
  9. private String address;
  10. /**
  11. * 一对一
  12. * oneToOne = true
  13. */
  14. @OneToMany(subTable = "t_person", joinValue = "id_card_id", cascadeSaveOrUpdate = true, oneToOne = true, reversePropertyName = "idCard")
  15. private Person person;
  16. }

Person.java

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @SuperBuilder
  6. @Table("t_person")
  7. public class Person extends BasePureEntity {
  8. private String name;
  9. @ManyToOne(parentTable = "t_person_id_card", value = "id_card_id", cascadeSaveOrUpdate = true)
  10. private IdCard idCard;
  11. }

运行 TableGenerator 生成SQL,参考sharp-database中的TableGenerator根据实体对象生成表

生成的SQL

  1. create table t_person_id_card
  2. (
  3. id bigint not null comment '主键'
  4. primary key,
  5. id_num varchar(32) null,
  6. address varchar(32) null,
  7. created_by bigint null,
  8. created_at datetime null,
  9. updated_by bigint null,
  10. updated_at datetime null,
  11. is_deleted bit null
  12. );
  13. create table t_person
  14. (
  15. id bigint not null comment '主键'
  16. primary key,
  17. name varchar(32) null,
  18. id_card_id bigint null,
  19. created_by bigint null,
  20. created_at datetime null,
  21. updated_by bigint null,
  22. updated_at datetime null,
  23. is_deleted bit null
  24. );

生成DAO

IdCardDAO

  1. @Repository
  2. public class IdCardDAO extends BaseDAOImpl<IdCard> {
  3. }

PersonDAO

  1. @Repository
  2. public class PersonDAO extends BaseDAOImpl<Person> {
  3. }

测试

  1. @SpringBootTest
  2. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
  3. public class PersonTest {
  4. @Autowired
  5. private PersonDAO personDAO;
  6. @Autowired
  7. private IdCardDAO idCardDAO;
  8. @Test
  9. @Order(0)
  10. public void savePerson() {
  11. Person person = Person.builder()
  12. .name("Rick")
  13. .idCard(IdCard.builder().idNum("32128787988762").address("江苏").build())
  14. .build();
  15. personDAO.insert(person);
  16. }
  17. @Test
  18. @Order(0)
  19. public void saveCardId() {
  20. Person person = Person.builder()
  21. .name("Rick")
  22. .build();
  23. IdCard idCard = IdCard.builder().idNum("32128787988762").address("陕西")
  24. .person(person)
  25. .build();
  26. idCardDAO.insert(idCard);
  27. }
  28. @Order(1)
  29. @Test
  30. public void findPersonById() {
  31. Person person = personDAO.selectById(552098712424472576L).get();
  32. assertThat(person.getName()).isEqualTo("Rick");
  33. assertThat(person.getIdCard().getIdNum()).isEqualTo("32128787988762");
  34. }
  35. @Order(2)
  36. @Test
  37. public void findIdCardById() {
  38. final IdCard idCard = idCardDAO.selectById(552098712365752320L).get();
  39. assertThat(idCard.getPerson().getName()).isEqualTo("Rick");
  40. assertThat(idCard.getIdNum()).isEqualTo("32128787988762");
  41. }
  42. }