mybatis的一對多查詢
創建實體類對象
class XXX{
string xxx,
string xxx,
List<MMM> mmm,
}
mybatis xml 文件的編寫兩種方式
<!-- 第一種方式 黑域名一對多的關系 -->
<select id="getListDns" resultMap="listDns">
SELECT t1.*,t2.DnsServerIp ,t2.ParseIp ,t2.DnsSrcPort ,t2.VisitTime ,t2.FtpIp ,t2.FtpdataPath
FROM BlackDomainIP t1,BlackDomain t2
WHERE t1.VisitIp = t2.VisitIp AND t1.VisitMac = t2.VisitMac AND t1.DnsIp = t2.DnsIp
</select>
<resultMap type="com.gh.order.model.blackdnsip.DnsIpModel" id="listDns">
<id column="VisitIp" property="VisitIp"/>
<id column="DnsIp" property="DnsIp"/>
<id column="VisitMac" property="VisitMac"/>
<result column="RuleType" property="RuleType"/>
<result column="RuleDes" property="RuleDes"/>
<result column="VisitNumber" property="VisitNumber"/>
<result column="Remark" property="Remark"/>
<result column="FirstTime" property="FirstTime"/>
<result column="LastTime" property="LastTime"/>
<result column="CaptrueName" property="CaptrueName"/>
<!-- 一對多 -->
<!-- <association property="listdnsmodel" javaType="com.gh.order.model.blackdnsip.DnsModel">
<result column="DnsServerIp" property="DnsServerIp"/>
<result column="ParseIp" property="ParseIp"/>
<result column="DnsSrcPort" property="DnsSrcPort"/>
<result column="VisitTime" property="VisitTime"/>
<result column="FtpIp" property="FtpIp"/>
<result column="FtpdataPath" property="FtpdataPath"/>
</association>
</resultMap> -->
<!-- 第二種查詢方式 一對多 -->
<resultMap type="com.gh.order.model.blackdnsip.DnsIpModel" id="listDns">
<id column="VisitIp" property="VisitIp"/>
<id column="DnsIp" property="DnsIp"/>
<id column="VisitMac" property="VisitMac"/>
<result column="RuleType" property="RuleType"/>
<result column="RuleDes" property="RuleDes"/>
<result column="VisitNumber" property="VisitNumber"/>
<result column="Remark" property="Remark"/>
<result column="FirstTime" property="FirstTime"/>
<result column="LastTime" property="LastTime"/>
<result column="CaptrueName" property="CaptrueName"/>
<!-- 一對多 -->
<collection property="listdnsmodel" javaType="ArrayList" column= "{prop1=VisitIp,prop2=DnsIp,prop3=VisitMac}" ofType="com.gh.order.model.blackdnsip.DnsModel" select="selectPostsForBlog"/>
</resultMap>
<select id="getListDns" resultMap="listDns">
SELECT * FROM BlackDomainIP
</select>
<select id="selectPostsForBlog" resultType="com.gh.order.model.blackdnsip.DnsModel">
SELECT DnsServerIp ,ParseIp ,DnsSrcPort ,VisitTime ,FtpIp ,FtpdataPath FROM BlackDomain
WHERE VisitIp =#{prop1} AND VisitMac = #{prop3} AND DnsIp = #{prop2}
</select>
mybatis的一對多查詢