一对多

Mysql一对多写法

可以看到在mysql里面是没有一对多效果的,一的一端数据会跟随多的一端重复。
但是我们在后端处理的时候,肯定是想要一对多关系数据的“一”不重复,这样比较好处理,对前端处理数据也比较友好
image.png

resultMap

配置一个resultMap,指明这个数组指向哪个类,数组名是什么,然后写清楚collection下分别有哪些参数,参数名和对应的数据库字段名。

  1. <resultMap id="resultTemplateInfo" type="com.sample.bean.TemplateCollect">
  2. <result property="TcollecName" column="Tcollec_name"></result>
  3. <result property="TcollecCount" column="Tcollec_count"></result>
  4. <result property="TcollecVolume" column="Tcollec_volume"></result>
  5. <collection property="procList" ofType="com.sample.bean.TemplateProc">
  6. <id property="TprocId" column="Tproc_id" ></id>
  7. <result property="TprocName" column="Tproc_name" ></result>
  8. <result property="TprocCount" column="Tproc_count" ></result>
  9. <result property="TprocVolume" column="Tproc_volume" ></result>
  10. </collection>
  11. </resultMap>
  12. <select id="selectTemplateCollec" resultMap="resultTemplateInfo" parameterType="String">
  13. SELECT
  14. Tcollec_name,
  15. Tcollec_count,
  16. Tcollec_volume,
  17. Tproc_name,
  18. Tproc_count,
  19. Tproc_volume,
  20. Tproc_unit
  21. FROM
  22. template_collect,
  23. template_proc,
  24. template
  25. WHERE
  26. template_name = "模板1"
  27. AND template_collect.template_id = template.template_id
  28. AND template_proc.Tcollec_id = template_collect.Tcollec_id
  29. </select>