myBitas如何多表查询
sql查询中经常会有多表查询的功能场景。
实现效果:
例如: 主表ddpk_topic 附表: ddpk_topic_img 外键(topic_id)
实现代码:
ddpktopic:
@Alias("ddpktopic")
public class ddpktopic {
private Integer id;
private String name;
private String introduce;
private Integer praisecount;
private String videourl;
private List<ddpktopicimg> ddpktopicimgs;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public Integer getPraisecount() {
return praisecount;
}
public void setPraisecount(Integer praisecount) {
this.praisecount = praisecount;
}
public String getVideourl() {
return videourl;
}
public void setVideourl(String videourl) {
this.videourl = videourl;
}
public List<ddpktopicimg> getDdpktopicimgs() {
return ddpktopicimgs;
}
public void setDdpktopicimgs(List<ddpktopicimg> ddpktopicimgs) {
this.ddpktopicimgs = ddpktopicimgs;
}
@Override
public String toString() {
return "ddpktopic{" +
"id=" + id +
", name='" + name + '\'' +
", introduce='" + introduce + '\'' +
", praisecount=" + praisecount +
", videourl='" + videourl + '\'' +
", ddpktopicimgs=" + ddpktopicimgs +
'}';
}
}
ddpktopicimg:
@Alias("ddpktopicimg")
public class ddpktopicimg {
private Integer id;
private String topic_id;
private String imgurl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTopic_id() {
return topic_id;
}
public void setTopic_id(String topic_id) {
this.topic_id = topic_id;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
@Override
public String toString() {
return "ddpktopicimg{" +
"id=" + id +
", topic_id='" + topic_id + '\'' +
", imgurl='" + imgurl + '\'' +
'}';
}
}
ddpkMapper.xml:
<resultMap id="TopicResultMap" type="ddpktopic" >
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="introduce" property="introduce"/>
<result column="praisecount" property="praisecount"/>
<result column="videourl" property="videourl"/>
<!-- 关联查询 -->
<!-- 关联查询话题图片-->
<collection property="ddpktopicimgs" javaType="java.util.List" ofType="ddpktopicimg">
<result column="imgurl" property="imgurl"/>
</collection>
</resultMap>
<!--<!– 获取所有话题信息–>-->
<select id="getAllTopic" resultType="list" resultMap="TopicResultMap" >
SELECT * FROM ddpk_topic t,ddpk_topic_img t2 WHERE t.id=t2.topic_id
</select>
<insert id="addddpkvisitinfo" parameterType="ddpkvisitinfo" >
insert into ddpk_visitinfo(name,create_date) values(#{name},#{create_date});
</insert>