MyBatis处理表与表之间的关系

列子:
比如要在帖子回复表里显示其它两张相关联表的信息

处理的第一种方式:

 1)主要的数据实体类是ReplyInfo,相关联的实体表的数据是PostInfoUserInfo
   那么首先创建的是ReplyInfoView的实体对象。

package com.gxa.bj.modle
package com.gxa.bj.modle;
public class ReplyInfoView extends ReplyInfo{
//将其他两个表需要关联的字段直接写进来
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
private String postName;
}

要关联的哪些数据,就直接把该数据加入到继承类的字段里


2)编写相应的处理接口(Mapper接口):

package com.gxa.bj.dao.imp

ackage com.gxa.bj.dao.imp;
import com.gxa.bj.dao.IDaoBBs;
import com.gxa.bj.modle.ReplyInfo;
public interface ReplyMapper extends IDaoBBs<ReplyInfo>{  
}

3)编写相应的SQL配置文件(ReplyInfoMapper.xml):

package com.gxa.bj.modle

<?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.gxa.bj.dao.imp.ReplyMapper">
      <select id="getMolde" resultType="com.gxa.bj.modle.ReplyInfoView">
             select r.replyId,r.replyContent,r.replyTime,r.userId,r.postId,u.userName,p.postName
             from replyinfo r inner join userinfo u 
                  on r.userId=u.userId 
                  inner join postinfo p 
                  on p.postId=r.postId
             where r.replyId=#{id}                
      </select> 
</mapper>

4)将刚编写的mapper映射文件加入到mybatis-config.xml中:

 <mappers>
     <mapper resource="com/gxa/bj/modle/UserInfoMapper.xml"/>
     <mapper resource="com/gxa/bj/modle/ReplyInfoMapper.xml"/>
  </mappers>

5)编写测试类:

public static void main(String[] args) {
// TODO Auto-generated method stub
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
     SqlSession sqlSessin=sqlSessionFactory.openSession();
          ReplyMapper replyMapper=sqlSessin.getMapper(ReplyMapper.class);
     ReplyInfoView replyInfoView=(ReplyInfoView) replyMapper.getMolde("asdf1");
     System.out.println("回复的帖子的名字:"+replyInfoView.getPostName());
     System.out.println("回复的作者:"+replyInfoView.getUserName());
     System.out.println("回复的内容:"+replyInfoView.getReplyContent());
          sqlSessin.close();

}

处理的第二种方式:(更能体现实体与实体之间的关系)

1.一对多的情况。在多的实体对象里会出现一的实体对象的属性:
实现的原理:不改变原有的实体类。加入实体与实体之间的映射结果。
实现的步骤:

1)创建一个Mapper的映射文件(PostInfoMapper.xml)

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

package com.gxa.bj.modle
<?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.gxa.bj.dao.imp.PostMapper">
     <select id="getMolde" parameterType="java.lang.String" resultMap="PostInfoResult">
            select * from postinfo p inner join userinfo u
                  on p.userId=u.UserId
                  inner join typeinfo t
                  on p.typeId=t.typeId
                  where p.postId=#{id}
     </select>
     <resultMap type="com.gxa.bj.modle.PostInfo" id="PostInfoResult">
                <result column="postId"  property="postId"></result>
                <result column="postName"  property="postName"></result>
                <result column="postContent"  property="postContent"></result>
                <result column="postTime"  property="postTime"></result>
                <result column="userId"  property="userId"></result>
                <result column="typeId"  property="typeId"></result>
                <association property="postTypeInfo" fetchType="eager" javaType="com.gxa.bj.modle.TypeInfo" foreignColumn="typeId">
                           <id column="typeId" property="typeId"></id>
                           <result column="typeContent" property="typeContent"></result>
                </association>
                 <association property="postUserInfo" fetchType="eager" javaType="com.gxa.bj.modle.UserInfo" foreignColumn="userId">
                           <id column="userId" property="userId"></id>
                           <result column="typeName" property="typeName"></result>
                </association>
     </resultMap>
</mapper>


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

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

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

column:数据库的列名

property:类的字段名

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

将刚编写的mapper映射文件加入到mybatis-config.xml中:

测试类:(1对多)

PostMapper postMapper=sqlSessin.getMapper(PostMapper.class);
   PostInfo p=postMapper.getMolde("qwer1");
   System.out.println("帖子名称:"+p.getPostName());
   System.out.println("作者:"+p.getPostUserInfo().getUserName());
   System.out.println("贴子类型:"+p.getPostTypeInfo().getTypeContent());


2.多对1的情况
所实现的映射关系是:在帖子分类的实体对象里包含的是帖子的实体对象的集合。

实现的步骤:

1)首先建立实体关系的映射,那么就需要在分类的实体里加入帖子的集合。代码如下:

package com.gxa.bj.modle

public class PostInfo {
private String postId;
private Integer typeId;
private UserInfo postUserInfo;//表示所对应用户的实体对象
private TypeInfo postTypeInfo;
public UserInfo getPostUserInfo() {
return postUserInfo;
}
public void setPostUserInfo(UserInfo postUserInfo) {
this.postUserInfo = postUserInfo;
}
public TypeInfo getPostTypeInfo() {
return postTypeInfo;
}
public void setPostTypeInfo(TypeInfo postTypeInfo) {
this.postTypeInfo = postTypeInfo;
}

public Integer getTypeId() {
return typeId;
}
public void setTypeId(Integer typeId) {
this.typeId = typeId;
}
public String getPostId() {
return postId;
}
public void setPostId(String postId) {
this.postId = postId;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
public String getPostContent() {
return postContent;
}
public void setPostContent(String postContent) {
this.postContent = postContent;
} 
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
private String postName;
private String postContent;

private Integer userId;
    public String getPostTime() {
return postTime;
}
public void setPostTime(String postTime) {
this.postTime = postTime;
}
private String postTime;
}


2)创建一个实现接口PostMapper:package com.gxa.bj.dao.imp

package com.gxa.bj.dao.imp;
import com.gxa.bj.dao.IDaoBBs;
import com.gxa.bj.modle.PostInfo;
public interface PostMapper extends IDaoBBs<PostInfo>{

}

3)编写相应的SQL配置文件:PostInfoMapper.xml 

<?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.gxa.bj.dao.imp.PostMapper">
     <select id="getMolde" parameterType="java.lang.String" resultMap="PostInfoResult">
            select * from postinfo p inner join userinfo u
                  on p.userId=u.UserId
                  inner join typeinfo t
                  on p.typeId=t.typeId
                  where p.postId=#{id}
     </select>
     <resultMap type="com.gxa.bj.modle.PostInfo" id="PostInfoResult">
                <result column="postId"  property="postId"></result>
                <result column="postName"  property="postName"></result>
                <result column="postContent"  property="postContent"></result>
                <result column="postTime"  property="postTime"></result>
                <result column="userId"  property="userId"></result>
                <result column="typeId"  property="typeId"></result>
                <association property="postTypeInfo" fetchType="eager" javaType="com.gxa.bj.modle.TypeInfo" foreignColumn="typeId">
                           <id column="typeId" property="typeId"></id>
                           <result column="typeContent" property="typeContent"></result>
                </association>
                 <association property="postUserInfo" fetchType="eager" javaType="com.gxa.bj.modle.UserInfo" foreignColumn="userId">
                           <id column="userId" property="userId"></id>
                           <result column="typeName" property="typeName"></result>
                </association>
     </resultMap>
</mapper>


注意:collection表示的是集合的数据。property表示所对应的集合的属性名。

      ofType表示集合里的数据的类型。resultMap表示的是调用的是其它的结果映射集。

      由于该结果映射集不在同一个文件下,那么就需要引入其它文件下的结果集。

      其它结果集的命名空间+结果集的名称。比如:com.gxa.bj.dao.imp.TitleInfoMapper.TitleInfoResult

要添加ReplyInfo表进去的时候操作同上

测试类:(多对1)

TypeMapper typeMapper=sqlSessin.getMapper(TypeMapper.class);
    TypeInfo t=typeMapper.getMolde(2);
    System.out.println("帖子分类名称:"+t.getTypeContent());
    System.out.println("帖子里的集合数据:");
    List<PostInfo> listPost=t.getPostinfos();
    for(PostInfo p:listPost){
     System.out.println("帖子名称:"+p.getPostName()+"\t");
    System.out.print("作者:"+p.getPostUserInfo().getUserName()+"\t");
    System.out.print("贴子类型:"+p.getPostTypeInfo().getTypeContent()+"\t");
    }





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值