打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MyBatis多表关联查询
userphoto

2022.09.01 江苏

关注

1:最简单粗暴的笨办法就是自定义一个智能的实体类,把多表查询需要的字段列出属性,注意实体类的属性名和数据库字段名保持一致

2:两个表一对多关联

实体类

  1. public class BasicInfo{
  2. private Integer basicInfoId;
  3. private Integer projectId;//项目ID
  4. private String keyNo;//公司内部关联主键
  5. private String name;//企业名称
  6. private String no;//注册号
  7. private String belongOrg;//登记机关
  8. private String operName;//法定代表人
  9. private String startDate;//成立日期
  10. private String endDate;//注销/吊销日期
  11. private String status;//登记状态(存续、在业、注销、迁入、吊销、迁出、停业、清算)
  12. private String province;//所在省份缩写
  13. private String updateDate;//更新日期
  14. private String creditCode;//统一社会信用代码
  15. private String registCapi;//注册资本
  16. private String econKind;//类型
  17. private String address;//住所
  18. private String scope;//经营范围
  19. private String termStart;//营业期限自
  20. private String teamEnd;//营业期限至
  21. private String checkDate;//核准日期
  22. private String sysTime;//
  23. private List<ChangeRecords> changeRecordsList; //变更记录
  24. .....
  1. public class ChangeRecords {
  2. private Integer changeRecordsId;
  3. private Integer basicInfoId;
  4. private String projectName;
  5. private String beforeContent;
  6. private String afterContent;
  7. private String changeDate;
  8. public Integer getChangeRecordsId() {
  9. return changeRecordsId;
  10. }
  11. public void setChangeRecordsId(Integer changeRecordsId) {
  12. this.changeRecordsId = changeRecordsId;
  13. }
  14. public String getProjectName() {
  15. return projectName;
  16. }
  17. public void setProjectName(String projectName) {
  18. this.projectName = projectName;
  19. }
  20. public String getBeforeContent() {
  21. return beforeContent;
  22. }
  23. public void setBeforeContent(String beforeContent) {
  24. this.beforeContent = beforeContent;
  25. }
  26. public String getAfterContent() {
  27. return afterContent;
  28. }
  29. public void setAfterContent(String afterContent) {
  30. this.afterContent = afterContent;
  31. }
  32. public String getChangeDate() {
  33. return changeDate;
  34. }
  35. public void setChangeDate(String changeDate) {
  36. this.changeDate = changeDate;
  37. }
  38. public Integer getBasicInfoId() {
  39. return basicInfoId;
  40. }
  41. public void setBasicInfoId(Integer basicInfoId) {
  42. this.basicInfoId = basicInfoId;
  43. }
  44. }

xml文件

  1. <resultMap id="BaseResultMap" type="com.cybernaut.core.model.BasicInfo" >
  2. <id column="basic_info_id" property="basicInfoId" jdbcType="INTEGER" />
  3. <result column="project_id" property="projectId" jdbcType="INTEGER" />
  4. <result column="key_no" property="keyNo" jdbcType="VARCHAR" />
  5. <result column="name" property="name" jdbcType="VARCHAR" />
  6. <result column="no" property="no" jdbcType="VARCHAR" />
  7. <result column="belong_org" property="belongOrg" jdbcType="VARCHAR" />
  8. <result column="oper_name" property="operName" jdbcType="VARCHAR" />
  9. <result column="start_date" property="startDate" jdbcType="VARCHAR" />
  10. <result column="end_date" property="endDate" jdbcType="VARCHAR" />
  11. <result column="status" property="status" jdbcType="VARCHAR" />
  12. <result column="province" property="province" jdbcType="VARCHAR" />
  13. <result column="update_date" property="updateDate" jdbcType="VARCHAR" />
  14. <result column="credit_code" property="creditCode" jdbcType="VARCHAR" />
  15. <result column="regist_capi" property="registCapi" jdbcType="VARCHAR" />
  16. <result column="econ_kind" property="econKind" jdbcType="VARCHAR" />
  17. <result column="address" property="address" jdbcType="VARCHAR" />
  18. <result column="scope" property="scope" jdbcType="VARCHAR" />
  19. <result column="term_start" property="termStart" jdbcType="VARCHAR" />
  20. <result column="team_end" property="teamEnd" jdbcType="VARCHAR" />
  21. <result column="check_date" property="checkDate" jdbcType="VARCHAR" />
  22. <result column="sys_time" property="sysTime" jdbcType="VARCHAR" />
  23. <!-- 变更记录 -->
  24. <collection property="changeRecordsList" ofType="com.cybernaut.core.model.ChangeRecords" column="basic_info_id"
  25. select="change_records.selectByChangeRecordsId"></collection>
  26. </resultMap>
  27. <sql id="Base_Column_List" >
  28. basic_info_id, project_id, key_no, name, no, belong_org, oper_name, start_date, end_date,
  29. status, province, update_date, credit_code, regist_capi, econ_kind, address, scope,
  30. term_start, team_end, check_date, sys_time
  31. </sql>

3:一对一

basic实体类里面的list改成单个对象

xml文件里<collection...>改成

<association property="对象名称" javaType="
com.cybernaut.core.model.ChangeRecords
" column="basic_info_id" select="change_records.selectSupervisor"/>


另外一种方式
<association property="对象名称" javaType="
com.cybernaut.core.model.ChangeRecords"
resultMap="changeRecordsResultMap"/></resultMap><!--教师实体映射--><resultMap id="changeRecordsResultMap" type="com.cybernaut.core.model.ChangeRecords"><id property="id" column="t_id"/><result property="name" column="t_name"/><result property="gender" column="t_gender"/><result property="researchArea" column="t_research_area"/><result property="title" column="t_title"/></resultMap>





本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
ibatis调用oracle的函数,存储过程的方法 IN 和OUT /游标
hibernate 一对多测试-----笔记
mybatis 一对多,多对一配置
Hibernate 组合映射使用
Hibernate4组件映射
Hibernate left join(左连接)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服