Mybatis一对多查询resultMap示例
几个实体说明,get set方法省略
1.最终返回list<CustomersTable>
private static final long serialVersionUID = 8405954804351004292L; private String customerId; private String customerName; private List<CustomerTel> customerTels; private Integer source; private PurchaseIntention purchaseIntention; private List<Transcations> transcations; private String serverName; private String serverOrg; private String sourceName; private String sourcePhone; private String userID;
private Integer customerTelId; private String customerTel; private String phone;PurchaseIntention实体:
private Integer purposeId; private String totalPrice; private String counties; private String houseType;Transcations实体
private static final long serialVersionUID = 8993887463088848093L; private Integer transcationId; private Integer buildingHousingId; private String buildingHousingName; private Integer isTeam; private Integer status; private String type; private List<LogJsonEntity> logJsons; private String sourceName; private String sourcePhone;
实现:
mybatis配置文件+mapper中的resultmap配置
<resultMap type="CustomersTable" id="ResultList"> <!-- <constructor> --> <!-- </constructor> --> <id property="userID" column="userID"></id> <result property="customerId" column="customerId" /> <result property="customerName" column="customerName" /> <result property="source" column="source" /> <association property="purchaseIntention" column="purchaseIntention" javaType="com.bean.pojo.PurchaseIntention"> <id property="purposeId" column="purposeId"/> <result property="totalPrice" column="totalPrice"/> <result property="counties" column="counties"/> </association> <collection property="customerTels" ofType="com.bean.pojoCustomerTel"> <id property="customerTelId" column="customerTelId"/> <result property="customerTel" column="customerTel"/> <result property="phone" column="phone"/> </collection> <collection property="transcations" ofType="Transcations"> <id property="transcationId" column="transcationId"/> <result property="buildingHousingId" column="buildingHousingId"/> <result property="buildingHousingName" column="buildingHousingName"/> <result property="isTeam" column="totalPrice"/> <result property="status" column="status"/> <result property="type" column="type"/> <collection property="logJsons" ofType="LogJson"> <id property="logId" column="logId"></id> <result property="logJson" column="logJson"></result> </collection> </collection> <!-- <discriminator javaType="LogJsonEntity"> --> <!-- <case value=""></case> --> <!-- </discriminator> --> </resultMap>
必须按照
constructor-->id-->result--->association。。。顺序写入
SQL:
<select id="queryList" resultMap="<span style="font-family: Arial, Helvetica, sans-serif;">ResultList</span><span style="font-family: Arial, Helvetica, sans-serif;">" ></span> SELECT .... </select>
结果:
{ code: "0", msg: "success", data: [ { customerId: "00000870", customerName: "HYS1321516t83", userID: "HYS001491", customerTels: [ { id: 859, phone: "13215151515" } ], purchaseIntention: "", transcations: [ { transcationId: "2374", buildingHousingId: "104", buildingHousingName: "(巫)", isTeam: "0", status: "140", type: "new", logJsons: [ { time: "1445932243435", status: "140" }, { time: "1445932116275", status: "101" }, { time: "1445932116290", status: "130" } ] }, { transcationId: "2373", buildingHousingId: "104", buildingHousingName: "(巫)", isTeam: "0", status: "130", type: "new", logJsons: [ { time: "1445931966483", status: "101" }, { time: "1445931966521", status: "130" } ] } ], source: "5", serverName: "AP", serverOrg: "中心" } ] }
原csdn博客地址:http://write.blog.csdn.net/postlist