一、准备实体类
Blog.java
package com.dmx.demo.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.List;
@Getter
@Setter
@ToString
public class Blog {
private int id;
private String title;
private String content;
private List<Comment> comments;
}
Comment.java
package com.dmx.demo.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Comment {
private int id;
private String comment;
private Blog blog;
}
二、准备两张表( blog 、 comment)
blog表
commet表
三、一对多查询
根据博客id查询所有的评论
在BlogMapper.xml中
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2015-2019 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dmx.demo.mapper.BlogMapper">
<!-- 返回resultMap 【第二步】 -->
<resultMap id="getBlogs" type="blog">
<result column="id" property="id"></result>
<result column="title" property="title"></result>
<result column="content" property="content"></result>
<!-- 从com.dmx.demo.mapper.CommentMapper.getCommentByBId获取comments 【第三步】 -->
<!-- 这里的comments为Blog中的属性 (List<Comment> comments) -->
<!-- getCommentByBId内容 -->
<!-- <select id="getCommentByBId" resultType="comment">-->
<!-- select * from comment where bid=#{bid};-->
<!-- </select>-->
<!-- #{bid}这里指的是 该实例的属性id 根据博客的id查询所有评论-->
<collection column="id" property="comments" select="com.dmx.demo.mapper.CommentMapper.getCommentByBId">
</collection>
</resultMap>
<!-- 实际调用的查询入口 【第一步】 -->
<select id="getBlogList" resultMap="getBlogs">
select * from blog;
</select>
<select id="getBlogById" resultType="blog">
select * from blog where id=#{id};
</select>
</mapper>
四、一对一查询
根据评论的bid查询评论所属的博客
在CommentMapper.xml中
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2015-2019 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dmx.demo.mapper.CommentMapper">
<!-- 返回的resultMap 【第二步】 -->
<resultMap id="getComments" type="comment">
<result column="id" property="id"></result>
<result column="comment" property="comment"></result>
<!-- 从com.dmx.demo.mapper.BlogMapper.getBlogById获取blog-->
<!-- getBlogById内容-->
<!-- <select id="getBlogById" resultType="blog">-->
<!-- select * from blog where id=#{id};-->
<!-- </select>-->
<!-- 这里的#{id}就是 该实例的属性bid 根据bid查询博客-->
<association column="bid" property="blog" select="com.dmx.demo.mapper.BlogMapper.getBlogById">
</association>
</resultMap>
<!-- 实际调用的查询入口 【第一步】-->
<select id="getCommentList" resultMap="getComments">
select * from comment;
</select>
<select id="getCommentByBId" resultType="comment">
select * from comment where bid=#{bid};
</select>
</mapper>
五、执行查询
根据博客id查询所有评论
package com.dmx.demo.service;
import com.dmx.demo.mapper.BlogMapper;
import com.dmx.demo.model.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BlogService {
@Autowired
private BlogMapper blogMapper;
public List<Blog> getBlogs(){
return blogMapper.getBlogList();
}
}
根据评论的bid查询博客
package com.dmx.demo.service;
import com.dmx.demo.mapper.CommentMapper;
import com.dmx.demo.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
public List<Comment> getComments(){
return commentMapper.getCommentList();
}
}