天天看點

mybatis 關聯查詢實作一對多

場景:最近接到一個項目是查詢管理人集合  同時每一個管理人還存在多個出資人   要查詢一個管理人清單  每個管理人又包含了出資人清單 采用mybatis關聯查詢實作傳回資料。

實作方式:

1 .在實體中定義包含出資人集合  并建立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;
    }


}      

View Code

2. mybatis.xml檔案中實作  定義基本查詢語句查詢管理人集合  傳回resultMap 結果集  在resultMap傳回結果中進行關聯關系映射查詢,collection實作一對多查詢

其中<id>标簽為查詢fundManager 的id 在管理時候的别名與關聯字短  比如 on t.id=s.id  在collection 标簽中通過select 屬性來指定關聯的查詢語句  但是本次我關聯的兩個

查詢分别屬于不同的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>
<!-- #######【結束】擴充代碼區,可自由改動。####### -->      

View Code

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>      

View Code