通過mybatis實現單表內一對多的數據展示


表:

  

需求:

  將表中的數據,按照一級二級分類返回給前端json數據

代碼實現:

  java代碼:

1 public class ResultIndustry {
2     private String industryFirst;//一級行業
3     private List<String> industrySecondList;//二級行業

 

  mybatis代碼:

 1 <select id="getResultIndustryList" resultMap="resultIndustryMap">
 2         SELECT DISTINCT industry_first,industry_second
 3         FROM results
 4         WHERE industry_second IS NOT NULL
 5         AND industry_second != ""
 6     </select>
 7 
 8     <resultMap id="resultIndustryMap" type="com.gy_resc.common.bean.ResultIndustry">
 9         <result property="industryFirst" column="industry_first" jdbcType="VARCHAR"/>
10         <collection property="industrySecondList" ofType="java.lang.String">
11             <result property="industrySecond" column="industry_second" jdbcType="VARCHAR"/>
12         </collection>
13     </resultMap>

原文地址 http://www.cnblogs.com/lukeheng/p/7435088.html

總結:

  其實就是一對多的封裝數據,從傳統的表關聯的一對多,聯想到單表內的一對多.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM