你知道mybatis一对多查询应该要怎样才能够实现吗?很多刚接触mybatis的朋友对于这个问题都不是太了解,那么希望下面的文章可以帮你解决。
mybatis关联查询实现一对多
场景:
一个项目是查询管理人集合,与此同时,每一个管理人还存在多个出资人,要查询一个管理人列表,每一个管理人又包含了出资人列表,采用mybatis关联查询实现返回数据。
下面是实现的方式:
在实体当中,定义包含出资人集合,并创建get set方法;
public class FundManager extends BaseEntity implements Serializable { /** 默认字段,禁止更改 **/ @Fieldmeta(name = "主键", logflag = 0) private String id; @Fieldmeta(name = "管理人名称", logflag = 0) private String name; @Fieldmeta(name = "管理人注册地址", logflag = 0) private String reginAdress; @Fieldmeta(name = "法定代表人(委派代表)", logflag = 0) private String legalName; @Fieldmeta(name = "法人是否取得基金从业资格证", logflag = 0) private Integer legalFq; @Fieldmeta(name = "风控负责人", logflag = 0) private String riskName; @Fieldmeta(name = "风控负责人是否取得基金从业资格证", logflag = 0) private Integer riskFq; @Fieldmeta(name = "核心团队", logflag = 0) private String keyman; @Fieldmeta(name = "驻青岛人员", logflag = 0) private String qdman; @Fieldmeta(name = "是否在中基协备案", logflag = 0) private Integer record; @Fieldmeta(name = "累计管理基金数量", logflag = 0) private Integer fundNum; @Fieldmeta(name = "累计管理基金总规模", logflag = 0) private BigDecimal totalFundMoney; @Fieldmeta(name = "累计投资项目数量", logflag = 0) private Integer investProjectNum; @Fieldmeta(name = "累计投资项目总金额", logflag = 0) private BigDecimal totalProjectMoney; @Fieldmeta(name = "累计退出项目数量", logflag = 0) private Integer exitProjectNum; @Fieldmeta(name = "实现上市退出及收益率的项目数量", logflag = 0) private Integer listProjectNum; @Fieldmeta(name = "退出项目的投资总金额", logflag = 0) private BigDecimal exitProjectMoney; @Fieldmeta(name = "退出项目的总金额", logflag = 0) private BigDecimal totalExitMoney; @Fieldmeta(name = "曾经投资青岛项目数量", logflag = 0) private Integer investQdNum; @Fieldmeta(name = "投资青岛项目总金额", logflag = 0) private BigDecimal totalQdMoney; @Fieldmeta(name = "成功项目1", logflag = 0) private String aprojectName; @Fieldmeta(name = "回报倍数1", logflag = 0) private BigDecimal amultiplier; @Fieldmeta(name = "成功项目2", logflag = 0) private String bprojectName; @Fieldmeta(name = "回报倍数2", logflag = 0) private BigDecimal bmultiplier; @Fieldmeta(name = "成功项目3", logflag = 0) private String cprojectName; @Fieldmeta(name = "回报倍数3", logflag = 0) private BigDecimal cmultiplier; @Fieldmeta(name = "社会信用代码", logflag = 0) private String unifiedNumber; @Fieldmeta(name = "成立时间", logflag = 0) private Date setupDate; @Fieldmeta(name = "联系人", logflag = 0) private String contacts; @Fieldmeta(name = "联系电话", logflag = 0) private String contactTel; @Fieldmeta(name = "管理人AMAC登记号", logflag = 0) private String amacNumber; @Fieldmeta(name = "管理人登记时间", logflag = 0) private Date recordDate; @Fieldmeta(name = "管理公司认缴注册资本", logflag = 0) private BigDecimal planAmount; @Fieldmeta(name = "管理公司实缴资本", logflag = 0) private BigDecimal amount; @Fieldmeta(name = "管理员是否新设", logflag = 0) private Integer isNew; @Fieldmeta(name = "详细介绍", logflag = 0) private String information; @Fieldmeta(name = "GP是否同基金管理人(是or否)", logflag = 0) private Integer managerIsGp; @Fieldmeta(name = "是否gp", logflag = 0) private Integer gpFlage; @Fieldmeta(name = "申报id", logflag = 0) private String gpNotifyId; /** 以下是公共扩展属性(若有较多非共性扩展属性,需定义在子类中) **/ private List < Investorinfo > investorinfos; /** 以下是默认get/set方法,一般情况下禁止更改 **/ /** 注意: **/ /** 1,若使用数据字典翻译,建议增加getXXXXValue方法,页面调用使用xXXXXValue **/ /** 2,若字段用于显示时,存在一些特殊处理逻辑,建议增加getXXXXForView方法,页面调用使用xXXXXForView **/ public void setId(String id) { this.id = id; } public String getId() { return this.id; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setReginAdress(String reginAdress) { this.reginAdress = reginAdress; } public String getReginAdress() { return this.reginAdress; } public void setLegalName(String legalName) { this.legalName = legalName; } public String getLegalName() { return this.legalName; } public void setLegalFq(Integer legalFq) { this.legalFq = legalFq; } public Integer getLegalFq() { return this.legalFq; } public void setRiskName(String riskName) { this.riskName = riskName; } public String getRiskName() { return this.riskName; } public void setRiskFq(Integer riskFq) { this.riskFq = riskFq; } public Integer getRiskFq() { return this.riskFq; } public void setKeyman(String keyman) { this.keyman = keyman; } public String getKeyman() { return this.keyman; } public void setQdman(String qdman) { this.qdman = qdman; } public String getQdman() { return this.qdman; } public void setRecord(Integer record) { this.record = record; } public Integer getRecord() { return this.record; } public void setFundNum(Integer fundNum) { this.fundNum = fundNum; } public Integer getFundNum() { return this.fundNum; } public void setTotalFundMoney(BigDecimal totalFundMoney) { this.totalFundMoney = totalFundMoney; } public BigDecimal getTotalFundMoney() { return this.totalFundMoney; } public void setInvestProjectNum(Integer investProjectNum) { this.investProjectNum = investProjectNum; } public Integer getInvestProjectNum() { return this.investProjectNum; } public void setTotalProjectMoney(BigDecimal totalProjectMoney) { this.totalProjectMoney = totalProjectMoney; } public BigDecimal getTotalProjectMoney() { return this.totalProjectMoney; } public void setExitProjectNum(Integer exitProjectNum) { this.exitProjectNum = exitProjectNum; } public Integer getExitProjectNum() { return this.exitProjectNum; } public void setListProjectNum(Integer listProjectNum) { this.listProjectNum = listProjectNum; } public Integer getListProjectNum() { return this.listProjectNum; } public void setExitProjectMoney(BigDecimal exitProjectMoney) { this.exitProjectMoney = exitProjectMoney; } public BigDecimal getExitProjectMoney() { return this.exitProjectMoney; } public void setTotalExitMoney(BigDecimal totalExitMoney) { this.totalExitMoney = totalExitMoney; } public BigDecimal getTotalExitMoney() { return this.totalExitMoney; } public void setInvestQdNum(Integer investQdNum) { this.investQdNum = investQdNum; } public Integer getInvestQdNum() { return this.investQdNum; } public void setTotalQdMoney(BigDecimal totalQdMoney) { this.totalQdMoney = totalQdMoney; } public BigDecimal getTotalQdMoney() { return this.totalQdMoney; } public void setAprojectName(String aprojectName) { this.aprojectName = aprojectName; } public String getAprojectName() { return this.aprojectName; } public void setAmultiplier(BigDecimal amultiplier) { this.amultiplier = amultiplier; } public BigDecimal getAmultiplier() { return this.amultiplier; } public void setBprojectName(String bprojectName) { this.bprojectName = bprojectName; } public String getBprojectName() { return this.bprojectName; } public void setBmultiplier(BigDecimal bmultiplier) { this.bmultiplier = bmultiplier; } public BigDecimal getBmultiplier() { return this.bmultiplier; } public void setCprojectName(String cprojectName) { this.cprojectName = cprojectName; } public String getCprojectName() { return this.cprojectName; } public void setCmultiplier(BigDecimal cmultiplier) { this.cmultiplier = cmultiplier; } public BigDecimal getCmultiplier() { return this.cmultiplier; } public void setUnifiedNumber(String unifiedNumber) { this.unifiedNumber = unifiedNumber; } public String getUnifiedNumber() { return this.unifiedNumber; } public void setSetupDate(Date setupDate) { this.setupDate = setupDate; } public Date getSetupDate() { return this.setupDate; } public void setContacts(String contacts) { this.contacts = contacts; } public String getContacts() { return this.contacts; } public void setContactTel(String contactTel) { this.contactTel = contactTel; } public String getContactTel() { return this.contactTel; } public void setAmacNumber(String amacNumber) { this.amacNumber = amacNumber; } public String getAmacNumber() { return this.amacNumber; } public void setRecordDate(Date recordDate) { this.recordDate = recordDate; } public Date getRecordDate() { return this.recordDate; } public void setPlanAmount(BigDecimal planAmount) { this.planAmount = planAmount; } public BigDecimal getPlanAmount() { return this.planAmount; } public void setAmount(BigDecimal amount) { this.amount = amount; } public BigDecimal getAmount() { return this.amount; } public void setIsNew(Integer isNew) { this.isNew = isNew; } public Integer getIsNew() { return this.isNew; } public void setInformation(String information) { this.information = information; } public String getInformation() { return this.information; } public void setManagerIsGp(Integer managerIsGp) { this.managerIsGp = managerIsGp; } public Integer getManagerIsGp() { return this.managerIsGp; } public void setGpFlage(Integer gpFlage) { this.gpFlage = gpFlage; } public Integer getGpFlage() { return this.gpFlage; } public String getGpNotifyId() { return gpNotifyId; } public void setGpNotifyId(String gpNotifyId) { this.gpNotifyId = gpNotifyId; } /** 扩展get/set方法 **/ public List < Investorinfo > getInvestorinfos() { return investorinfos; } public void setInvestorinfos(List < Investorinfo > investorinfos) { this.investorinfos = investorinfos; } }
mybatis.xml文件当中实现
定义基本查询语句查询管理人集合,返回resultMap结果集,在resultMap返回结果当中,进行关联关系映射查询,collection实现一对多查询;
在这当中id标签是查询fundManager的id在管理时的别名和关联字短。
例如:on t.id=s.id
在collection标签当中通过select来指定关联的查询语句,但是这次关联的2个查询分别属于不同的xml文件,那么,这个时候要怎样实现在一个mapper.xml文件当中引用另外一个mapper.xml文件的查询语句呢。
采用命名空间加.id属性对应的名方式
fundManagerMapper.xml
<!-- #######【开始】扩展代码区,可自由改动。####### --> <resultMap id="ResultMapWithInvestor" type="com.qk.gpportal.core.fundmanager.entity.FundManager" extends="BaseResultMap"> <id property="id" column="id"/> <collection property="investorinfos" ofType="com.qk.gpportal.core.investorinfo.entity.Investorinfo" column="id" select="com.qk.gpportal.core.investorinfo.dao.InvestorinfoMapper.getInvestors"></collection> </resultMap> <select id="getFundManagerBygpNotifyId" parameterType="java.lang.String" resultMap="ResultMapWithInvestor"> select <include refid="Columns"/> FROM g_fund_manager WHERE gp_notify_id= #{gpNotifyId,jdbcType=VARCHAR} </select> <!-- #######【结束】扩展代码区,可自由改动。####### -->
investorinfoMapper.xml id的名字通过在fundmanagerMapper.xml文件当中表空间方式.id名称方式引用,能够实现不同.xml方法之间的引用。
<select id="getInvestors" resultMap="BaseResultMap" parameterType="java.lang.String"> select <include refid="Columns"/> from g_investorinfo where fund_manager_id = #{id} </select>
关于mybatis实现一对多查询的方式就给你介绍到这里了,你还想了解更多相关知识吗?请继续关注本站来了解吧!更多java常见问题及解决方法可以为你分享哦。
推荐阅读: