Mybatisアソシエーション関係マッピング

1.1対多の関係

まず、逆生成ツールを使用して、t_hibernate_order、t_hibernate_order_itemを生成します

これら2つのテーブルに対応するモデルとマッパー

エンティティクラス

package com.xieminglu.ssm.model.vo;

import com.xieminglu.ssm.model.Order;
import com.xieminglu.ssm.model.OrderItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 11:45
 */
public class OrderVo extends Order {
    private List<OrderItem> orderItems=new ArrayList<>();

    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }
}

package com.xieminglu.ssm.model.vo;

import com.xieminglu.ssm.model.Order;
import com.xieminglu.ssm.model.OrderItem;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 18:29
 */
public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "OrderItemVo{" +
                "order=" + order +
                '}';
    }
}

OrderMapper.xml

<resultMap id="OrderVoMap" type="com.xieminglu.ssm.model.vo.OrderVo">
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <!--<result property="orderItems"></result>-->
    <collection property="orderItems" ofType="com.xieminglu.ssm.model.OrderItem">
      <result property="orderItemId" column="order_item_id"></result>
      <result property="productId" column="product_id"></result>
      <result property="quantity" column="quantity"></result>
      <result property="oid" column="oid"></result>
    </collection>
  </resultMap>

  <select id="selectByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer" >
    select * from t_hibernate_order o,t_hibernate_order_item oi where    o.order_id=oi.oid
 and oi.oid=#{orderId}
  </select>

OrderMapper.java

package com.xieminglu.ssm.mapper;

import com.xieminglu.ssm.model.Order;
import com.xieminglu.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderMapper {
    int deleteByPrimaryKey(Integer orderId);

    int insert(Order record);

    int insertSelective(Order record);

    Order selectByPrimaryKey(Integer orderId);

    int updateByPrimaryKeySelective(Order record);

    int updateByPrimaryKey(Order record);

    List<OrderVo> selectByOrderId(@Param("orderId") Integer orderId);
}

OrderItemMapper.xml

<resultMap id="OrderItemVoMap" type="com.xieminglu.ssm.model.vo.OrderItemVo">
    <result property="orderItemId" column="order_item_id"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
    <result property="oid" column="oid"></result>
    <!--<result property="orderItems"></result>-->
    <association property="order" javaType="com.xieminglu.ssm.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </association>
  </resultMap>

 <select id="selectByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer" >
    select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid
 and oi.order_item_id=#{orderItemId}
  </select>

OrderItemMapper.java

package com.xieminglu.ssm.mapper;

import com.xieminglu.ssm.model.OrderItem;
import com.xieminglu.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderItemMapper {
    int deleteByPrimaryKey(Integer orderItemId);

    int insert(OrderItem record);

    int insertSelective(OrderItem record);

    OrderItem selectByPrimaryKey(Integer orderItemId);

    int updateByPrimaryKeySelective(OrderItem record);

    int updateByPrimaryKey(OrderItem record);

    List<OrderItemVo> selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
}

OneToManyService.java

package com.xieminglu.ssm.service;

import com.xieminglu.ssm.model.vo.OrderItemVo;
import com.xieminglu.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 18:49
 */
public interface OneToManyService {
    List<OrderVo> selectByOrderId(Integer orderId);
    List<OrderItemVo> selectByOrderItemId(Integer orderItemId);
}

package com.xieminglu.ssm.service.impl;

import com.xieminglu.ssm.mapper.CategoryMapper;
import com.xieminglu.ssm.mapper.HbookMapper;
import com.xieminglu.ssm.model.vo.CategoryVo;
import com.xieminglu.ssm.model.vo.HbookVo;
import com.xieminglu.ssm.service.ManyToMany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 20:17
 */
@Service
public class ManyToManyImpl implements ManyToMany {
    @Autowired
    private HbookMapper hbookMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public HbookVo selectByBid(Integer bid) {
        return hbookMapper.selectByBid(bid);
    }

    @Override
    public CategoryVo selectByCid(Integer cid) {
        return categoryMapper.selectByCid(cid);
    }
}

OneToManyServiceImplTest.java

package com.xieminglu.ssm.service.impl;

import com.xieminglu.ssm.SpringJunitBaseTest;
import com.xieminglu.ssm.model.OrderItem;
import com.xieminglu.ssm.model.vo.OrderItemVo;
import com.xieminglu.ssm.model.vo.OrderVo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 19:36
 */
public class OneToManyServiceImplTest extends SpringJunitBaseTest {
@Autowired
private OneToManyServiceImpl oneToManyService;
    @Test
    public void selectByOrderId() {
        List<OrderVo> orderVos = oneToManyService.selectByOrderId(8);
        OrderVo orderVo=orderVos.get(0);
        System.out.println(orderVo);
        for (OrderItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }

    }

    @Test
    public void selectByOrderItemId() {
        List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36);
        OrderItemVo orderItemVo = orderItemVos.get(0);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());
    }
}

ここに写真の説明を挿入

2.多対多の関係

まず、逆生成ツールを使用して、t_hibernate_book、t_hibernate_book_category、t_hibernate_category、これら2つのテーブルに対応するモデルとマッパーを生成します。

エンティティクラス

package com.xieminglu.ssm.model.vo;

import com.xieminglu.ssm.model.Category;
import com.xieminglu.ssm.model.Hbook;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 19:52
 */
public class HbookVo extends Hbook {
    private List<Category> categories=new ArrayList<>();

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

}

package com.xieminglu.ssm.model.vo;

import com.xieminglu.ssm.model.Category;
import com.xieminglu.ssm.model.Hbook;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 19:53
 */
public class CategoryVo extends Category {
    private List<Hbook> hbooks=new ArrayList<>();

    public List<Hbook> getHbooks() {
        return hbooks;
    }

    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}

HbookCategoryMapper.xml

 <resultMap id="HbookVoMap" type="com.xieminglu.ssm.model.vo.HbookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <result property="price" column="price"></result>
    <!--<result property="orderItems"></result>-->
    <collection property="categories" ofType="com.xieminglu.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

<resultMap id="CategoryVoMap" type="com.xieminglu.ssm.model.vo.CategoryVo">
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <!--<result property="orderItems"></result>-->
    <collection property="hbooks" ofType="com.xieminglu.ssm.model.Hbook">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
      <result property="price" column="price"></result>
    </collection>
  </resultMap>

  <select id="selectByBid" resultMap="HbookVoMap" parameterType="java.lang.Integer" >
    select * from t_hibernate_book b,t_hibernate_book_category bc,
t_hibernate_category c where b.book_id=bc.bid and bc.cid=c.category_id
and b.book_id=#{bid}
  </select>

  <select id="selectByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer" >
    select * from t_hibernate_book b,t_hibernate_book_category bc,
t_hibernate_category c where b.book_id=bc.bid and bc.cid=c.category_id
and c.category_id=#{cid}
  </select>

HbookMapper.java

package com.xieminglu.ssm.mapper;

import com.xieminglu.ssm.model.Hbook;
import com.xieminglu.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;

public interface HbookMapper {
    int deleteByPrimaryKey(Integer bookId);

    int insert(Hbook record);

    int insertSelective(Hbook record);

    Hbook selectByPrimaryKey(Integer bookId);

    int updateByPrimaryKeySelective(Hbook record);

    int updateByPrimaryKey(Hbook record);

    HbookVo selectByBid(@Param("bid")Integer bid);
}

CategoryMapper.java

package com.xieminglu.ssm.mapper;

import com.xieminglu.ssm.model.Category;
import com.xieminglu.ssm.model.vo.CategoryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryMapper {
    int deleteByPrimaryKey(Integer categoryId);

    int insert(Category record);

    int insertSelective(Category record);

    Category selectByPrimaryKey(Integer categoryId);

    int updateByPrimaryKeySelective(Category record);

    int updateByPrimaryKey(Category record);

    CategoryVo selectByCid(@Param("cid") Integer cid);
}

ManyToMany.java

package com.xieminglu.ssm.service;

import com.xieminglu.ssm.model.vo.CategoryVo;
import com.xieminglu.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 20:15
 */
public interface ManyToMany {
    HbookVo selectByBid(@Param("bid")Integer bid);
    CategoryVo selectByCid(@Param("cid") Integer cid);
}

package com.xieminglu.ssm.service.impl;

import com.xieminglu.ssm.mapper.CategoryMapper;
import com.xieminglu.ssm.mapper.HbookMapper;
import com.xieminglu.ssm.model.vo.CategoryVo;
import com.xieminglu.ssm.model.vo.HbookVo;
import com.xieminglu.ssm.service.ManyToMany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 20:17
 */
@Service
public class ManyToManyImpl implements ManyToMany {
    @Autowired
    private HbookMapper hbookMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public HbookVo selectByBid(Integer bid) {
        return hbookMapper.selectByBid(bid);
    }

    @Override
    public CategoryVo selectByCid(Integer cid) {
        return categoryMapper.selectByCid(cid);
    }
}

ManyToManyImplTest.java

package com.xieminglu.ssm.service.impl;

import com.xieminglu.ssm.SpringJunitBaseTest;
import com.xieminglu.ssm.model.Category;
import com.xieminglu.ssm.model.Hbook;
import com.xieminglu.ssm.model.vo.CategoryVo;
import com.xieminglu.ssm.model.vo.HbookVo;
import com.xieminglu.ssm.service.ManyToMany;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author 迷鹿小女子
 * @site www.baidu.com
 * @company xxx公司
 * @create  2019-10-23 20:18
 */
public class ManyToManyImplTest extends SpringJunitBaseTest {

    @Autowired
    private ManyToMany manyToMany;
    @Test
    public void selectByBid() {
        HbookVo hbookVo = manyToMany.selectByBid(8);
        System.out.println(hbookVo);
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category);
        }

    }

    @Test
    public void selectByCid() {
        CategoryVo categoryVo = this.manyToMany.selectByCid(8);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook);
        }
    }
}

ここに写真の説明を挿入

おすすめ

転載: blog.csdn.net/xieminglu/article/details/102710424