
mybatis
使用Mybatis進(jìn)行關(guān)聯(lián)查詢,之前發(fā)在我的CSDN博客中,現(xiàn)在搬到簡書上來。
數(shù)據(jù)庫關(guān)系圖

數(shù)據(jù)庫關(guān)系圖
如圖是一個博客系統(tǒng)的數(shù)據(jù)庫關(guān)系圖,用戶和博客是一對多的關(guān)系(一個用戶擁有多個博客),博客和文章是一對多的關(guān)系(一個博客擁有多篇文章)。
實體類
// MyUser類
public class MyUser{
private Integer id;
private String name;
// getter and setter
}
// MyBlog類
public class MyBlog {
private int id;
private String title;
private int userId;
// getter and setter ...
}
// MyPost類
public class MyPost {
private int id;
private String body;
private int blogId;
// getter and setter ...
}
// 博客信息類,包含用戶信息和文章列表,作為關(guān)聯(lián)查詢結(jié)果的實體類
public class MyBlogInfo {
private int blogId;
private String title;
private MyUser myUser;
private List<MyPost> myPostList;
// getter and setter ...
}
Dao類
@Component
public class MyBlogInfoDao {
@Autowired
private MyBlogInfoMapper myBlogInfoMapper;
public MyBlogInfo queryAllBlogInfo(int id)
{
return myBlogInfoMapper.queryAllBlogInfo(id);
}
}
mapper接口類
@Mapper
public interface MyBlogInfoMapper {
MyBlogInfo queryAllBlogInfo(int id);
List<MyPost> queryAllBlogInfo1(int id);
}
mapper映射文件
<?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.example.mapper.MyBlogInfoMapper">
<resultMap id="MyBlogInfo" type="com.example.bean.MyBlogInfo">
<id column="id" property="blogId"/>
<result column="title" property="title"/>
<association property="myUser" column="user_id" javaType="com.example.bean.MyUser">
<id column="id" property="id"/>
<result column="name" property="name"/>
</association>
<collection property="myPostList" column="blog_id" ofType="com.example.bean.MyPost" javaType="list">
<id column="post_id" property="id"/>
<result column="body" property="body"/>
<result column="blog_id" property="blogId"/>
</collection>
</resultMap>
<select id="queryAllBlogInfo" resultMap="MyBlogInfo">
SELECT
b.id,
b.title,
b.user_id,
u.id,
u.name,
p.id AS post_id,
p.body,
p.blog_id
FROM myblog b
LEFT OUTER JOIN myuser u ON b.user_id = u.id
LEFT OUTER JOIN mypost p ON p.blog_id = b.id
WHERE b.id = #{id}
</select>
</mapper>
controller測試類
@Controller
public class DemoController
{
@RequestMapping(value = "/queryAllBlogInfo")
@ResponseBody
public String queryAllBlogInfo(@RequestParam(value = "id") int id)
{
MyBlogInfo myBlogInfo = myBlogInfoDao.queryAllBlogInfo(id);
return "success!";
}
}
mapper映射文件中幾點(diǎn)注意
- 根據(jù)MyBlog表查詢MyUser,由于MyBlog表中持有MyUser表的主鍵,所以關(guān)聯(lián)寫法為:
<association property="myUser" column="user_id" javaType="com.example.bean.MyUser"> <id column="id" property="id"/> <result column="name" property="name"/> </association>- 根據(jù)MyBlog表查詢MyPost, 不同于查詢MyUser,這里MyPost表中持有MyBlog表主鍵,所以關(guān)聯(lián)寫法為:
<collection property="myPostList" column="blog_id" ofType="com.example.bean.MyPost" javaType="list"> <id column="post_id" property="id"/> <result column="body" property="body"/> <result column="blog_id" property="blogId"/> </collection>
特別注意:這里為 oftype。- mybatis多表關(guān)聯(lián)查詢(一對多,collection)時,如果兩個表的字段名字一樣,需要為重名字段指定別名,否則只能查詢到一條記錄。如上述例子中,MyBlog表和MyPost表中id字段重名,這里在select語句中將MyPost設(shè)別名post_id,同時修改
<collection><id column="post_id" property="id"/></collection>中id字段與之對應(yīng)。
備注
圖片源自網(wǎng)絡(luò),侵權(quán)必刪!