文章目录
一、一对一映射
//实体列 class Tb_blog/TbBlog
private long blogId;
private String blogTitle;
private String blogContent;
private Date createTime;
private String blogType;
private String sId;
private Tb_author author;
List<TbAuthor> tbAuthorList;
//要写get、set方法 此处省略....
//实体类 class TbAuthor
private long id;
private String username;
private String password;
private String email;
private String address;
private String phone;
private TbBlog tbBlog;
private List<TbBlog> tbBlogList;
//要写get、set方法 此处省略....
1.resultMap标签配置
<!-- xml -->
<resultMap id="blogMap" type="Tb_blog" >
<id column="blogId" property="blogId"/>
<result column="blogTitle" property="blogTitle"/>
<result column="blogContent" property="blogContent"/>
<result column="blogType" property="blogType"/>
<result column="createTime" property="createTime"/>
<result column="sId" property="sId"/>
<result column="id" property="author.id"/> <!-- 映射第二张表的实体类属性 -->
<result column="username" property="author.username"/>
<result column="password" property="author.password"/>
<result column="email" property="author.email"/>
</resultMap>
<select id="selectBlogAndAuthor" resultMap="blogMap">
select * from tb_blog g inner join tb_author r
on g.blogId = r.id
</select>
2.association标签配置
<!-- xml -->
<resultMap id="blogMap" type="Tb_blog" >
<id column="blogId" property="blogId"/>
<result column="blogTitle" property="blogTitle"/>
<result column="blogContent" property="blogContent"/>
<result column="blogType" property="blogType"/>
<result column="createTime" property="createTime"/>
<!-- 一对一高效率写法 association一对一关联 property属性为实体类中的第二张表的属性名 -->
<association property="tb_author" javaType="Tb_author"><!--javaType属性为 返回的实体类对象 -->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="email" property="email"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="selectBlogAndAuthor" resultMap="blogMap">
select * from tb_blog g inner join tb_author r on g.blogId = r.id
</select>
3.association多表一对一嵌套查询
//AuthorMapper.interface
//!通过id 和映射文件中 association的column属性的值sId关联 来嵌套查询 嵌套查询的第二条sql语句都要写条件来关联第一张表
List<TbAuthor> selectAuthorandBlogAssociation(int id);
//BlogMapper.interface
List<TbBlog> selectBlogAndAuthorAssociation();
<!-- AuthorMapper.xml -->
<select id="selectAuthorandBlogAssociation" resultType="com.xqh.pojo.TbAuthor">
select * from tb_author where id=#{id}
</select>
<!-- BlogMapper.xml -->
<resultMap id="mapAssociation" type="TbBlog">
<id property="blogId" column="blogId"/>
<id property="blogTitle" column="blogTitle"/>
<id property="blogContent" column="blogContent"/>
<id property="createTime" column="createTime"/>
<id property="blogType" column="blogType"/>
<id property="sId" column="sId"/>
//一对一 association.property属性 用的是单个对象
<association property="tbAuthor" column="sId" javaType="TbAuthor"
select="com.xqh.mapper.AuthorMapper.selectAuthorandBlogAssociation">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="phone" column="phone"/>
<result property="email" column="email"/>
<result property="address" column="address"/>
</association>
</resultMap>
<select id="selectBlogAndAuthorAssociation" resultMap="mapAssociation">
select * from tb_blog
</select>
二、一对多查询
//实体列
private long id;
private String username;
private String password;
private String email;
private String address;
private String phone;
private TbBlog tbBlog;
private List<TbBlog> tbBlogList;
1.collection标签多表连接查询(一对多)
//AuthorMapper.interface
// 一对多sql笛卡尔积多表查询
List<TbAuthor> selectAuthor_BlogList2();
<!-- xml -->
<!-- 一对多sql笛卡尔积多表查询-->
<resultMap id="mapCollection2" type="TbAuthor">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="address" column="address"/>
//一对多 collection.property用的是集合对象 来返回多条数据
<collection property="tbBlogList" column="id" ofType="TbBlog">
<id property="blogId" column="blogId"/>
<result property="blogTitle" column="blogTitle"/>
<result property="blogContent" column="blogContent"/>
<result property="blogType" column="blogType"/>
<result property="createTime" column="vreateTime"/>
<result property="sId" column="sId"/>
</collection>
</resultMap>
<select id="selectAuthor_BlogList2" resultMap="mapCollection2">
select * from tb_author inner join tb_blog tb on tb_author.id = tb.sId
</select>
2.collection多表嵌套查询(一对多)
//AuthorMapper.interface
List<TbAuthor> selectAuthor_BlogList();
//BlogMapper.interface
// 一对多嵌套查询 id是给限制 不然一个作者全部博客
List<TbBlog> selectBlogAndAuthor(int id);
<!-- BlogMapper.xml-->
<select id="selectBlogAndAuthor" resultType="com.xqh.pojo.TbBlog">
select * from tb_blog where sId = #{id}
</select>
<!-- xml -->
<!-- AuthorMapper.xml-->
<resultMap id="mapCollection" type="TbAuthor">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="address" column="address"/>
<collection property="tbBlogList" column="id"
select="com.xqh.mapper.BlogMapper.selectBlogAndAuthor"
fetchType="lazy">
</collection>
</resultMap>
<select id="selectAuthor_BlogList" resultMap="mapCollection">
select * from tb_author
</select>
三、总结
1.多表查询一对一映射
association标签
- 不嵌套 property=当前实体类中的第二种表的属性名 javaType=返回的实体类
- 嵌套 多加两个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值)
2.多表查询一对多
collection标签
- 不嵌套 property=当前实体类中的第二种表的属性名 ofType=返回是实体类
property=当前实体类中的第二种表的属性名 javaType=返回的实体类 - 嵌套 多加两个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值)
2.多表查询一对多
collection标签
- 不嵌套 property=当前实体类中的第二种表的属性名 ofType=返回是实体类
- 嵌套 多加一个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值) [ofType = collection一对多嵌套查询 嵌套查询所有结果 不需写返回类型因为 select已经映射]