MyBatis 表与表之间的关系 1对多的情况

1.一对多的情况。在多的实体对象里会出现一的实体对象的属性:

帖子分类          帖子

1)            n

实现的原理:不改变原有的实体类。加入实体与实体之间的映射结果。

实现的步骤:

1) 在帖子类里创建用户类的实体对象和分类实体对象,代码如下:

package com.model;
//帖子
public class Theme {
private String themeId;//帖子编号
private String themeName;//帖子名称
private String themeContent;//帖子内容
private Integer UserId;//用户编号
private String themeTime;//发帖时间
private Integer sortId;//分类编号
private UserInfo themeUserInfo;//表示的是所对应的用户的实体对象
private Sort themeSort;

public Sort getThemeSort() {
return themeSort;
}
public void setThemeSort(Sort themeSort) {
this.themeSort = themeSort;
}
public UserInfo getThemeUserInfo() {
return themeUserInfo;
}
public void setThemeUserInfo(UserInfo themeUserInfo) {
this.themeUserInfo = themeUserInfo;
}
public Integer getSortId() {
return sortId;
}
public void setSortId(Integer sortId) {
this.sortId = sortId;
}
public String getThemeId() {
return themeId;
}
public void setThemeId(String themeId) {
this.themeId = themeId;
}
public String getThemeName() {
return themeName;
}
public void setThemeName(String themeName) {
this.themeName = themeName;
}
public String getThemeContent() {
return themeContent;
}
public void setThemeContent(String themeContent) {
this.themeContent = themeContent;
}
public String getThemeTime() {
return themeTime;
}
public void setThemeTime(String themeTime) {
this.themeTime = themeTime;
}
public Integer getUserId() {
return UserId;
}
public void setUserId(Integer userId) {
UserId = userId;
}


}


2)创建一个Mapper的映射文件。ThemeMapper.xml.

  该文件里除了相对应的多表查询的SQL语句之外,还具有结果集的映射配置。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.imp.ThemeMapper">
 <select id="getModel" resultType="java.lang.String" resultMap="ThemeResult">
    select *
    from Theme t inner join userInfo u on t.userId=u.userId
    inner join sort s on t.sortId=s.sortId
    where t.themeId=#{id}
  </select>
  <resultMap type="com.model.Theme" id="ThemeResult">
      <result column="themeId" property="themeId"></result>
      <result column="themeName" property="themeName"></result>
      <result column="themeContent" property="themeContent"></result>
      <result column="userId" property="userId"></result>
      <result column="themeTime" property="themeTime"></result>
      <result column="sortId" property="sortId"></result>
      <association property="themeUserInfo" javaType="com.model.UserInfo" foreignColumn="userId">
      <id column="userId" property="userId"></id>
      <result column="userName" property="userName"></result>
      </association>
      <association property="themeSort" javaType="com.model.Sort" foreignColumn="sortId">
      <id column="sortId" property="sortId"></id>
      <result column="sortName" property="sortName"></result>
      </association>
  </resultMap>
  
</mapper>

其中:resultMap这个节点作为结果集的配置节点。其中的id属性就是它的引用Id(其它地方引用它的时候,是通过这个id名来引用的)。

<result>节点就是对应到数据库里的表的列名和实体对象的字段。

比如:<result column="titleName" property="titleName"></result> 

column:数据库的列名

property:类的字段名

association节点表示关联的实体对象。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值