MyBatis 多对多关系映射

来源:互联网 发布:win7碎片整理软件 编辑:程序博客网 时间:2024/06/06 15:46

 显示mysql数据库中图书表的所有图书,并显示出该图书所属的类别(一本书可能同时属于多个类别)


-- 建立图书表(图书编号,图书名字)create table book(    bid int primary key auto_increment,    bname varchar(20)                                     );-- 建立类别表(类别编号,类别名字)create table category(    cid int primary key auto_increment,    cname varchar(20));-- 建立中间表(图书编号,类别编号)create table middle(    m_bid int,    m_cid int,    constraint fk_bid foreign key(m_bid) references book(bid),    constraint fk_cid foreign key(m_cid) references category(cid));--添加数据insert into category values (default,'java');insert into category values (default,'c++');insert into category values (default,'mysql');insert into book values (default,'SQL技术');insert into book values (default,'SSM+MySQL详解');insert into book values (default,'C++和java对比');insert into middle values (1,3);insert into middle values (2,1);insert into middle values (2,3);insert into middle values (3,2);insert into middle values (3,1);select * from  book;select * from category;select * from middle;
创建图书实体类

public class Book {private Integer bid;private String bname;private List<Category> categories;  .....}

创建类别实体类

public class Category {private Integer cid;private String cname;   .....}

DAO接口BookMapper

public interface BookMapper {public List<Book> all() throws Exception;}

图书的映射文件

<?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.hlx.dao.BookMapper"><!-- 查询 resultMap哦! --><select id="all" resultMap="BookResultMap">select * from book b,middle m,category c where b.bid=m.m_bid and c.cid=m_cid</select><!-- 使用resultMap映射实体类和字段之间的一一对应关系 --><resultMap type="Book" id="BookResultMap"><id property="bid" column="bid" />  <!-- pk --><result property="bname" column="bname" /> <!-- cname --><!-- ofType指定students集合中的对象类型 --><collection property="categories" ofType="Category"><id property="cid" column="cid" />  <!-- pk --><result property="cname" column="cname" /> <!-- cname --></collection></resultMap></mapper>

测试数据:

@Testpublic void all() throws Exception {// 1.会话对象SqlSession session = MybatisUtil.getSession(true);// 2.获得接口对象BookMapper mapper = session.getMapper(BookMapper.class);// 3.调用方法List<Book> c1 =mapper.all();for (Book book : c1) {System.out.println(book.getBname());for (Category ca : book.getCategories()) {System.out.println(ca.getCname());}System.out.println();}// 4.关闭会话session.close();}

效果:



总结

resultType 

          作用:将查询结果按照sql列名pojo属性名一致性映射到pojo中。

          场合:常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。 

resultMap

       使用association和collection完成一对一、一对多、多对多高级映射(对结果有特殊的映射要求)。

association:

  • 作用:将关联查询信息映射到一个pojo对象中。
  • 场合:为了方便查询关联信息可以使用association将关联图书信息映射为类别对象的pojo属性中,比如:查询图书及关联类别信息。

使用resultType无法将查询结果映射到pojo对象的pojo属性中,根据对结果集查询遍历的需要选择使用resultType还是resultMap。

collection:

  • 作用:将关联查询信息映射到一个list集合中。
  • 场合:为了方便查询遍历关联信息可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块及模块下的菜单,可使用collection将模块映射到模块list中,将菜单列表映射到模块对象的菜单list属性中,这样的作的目的也是方便对查询结果集进行遍历查询。如果使用resultType无法将查询结果映射到list集合中。