mybatis关联关系映射

一对多的关联关系

  1. 首先用逆向生成工具生成要用的几张表



    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        

        
        
        
        
        
        
        
        
        
        
        
        
        

        

        

运行后自动生成的实体类,接口和xml文件:
mybatis关联关系映射_第1张图片

  1. 在对应的类中添加引用
    OrderVo.java
package com.xfz.ssm.model.vo;

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

import java.util.List;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 19:12
 */
public class OrderVo extends Order {
    private List orderItems;

    public List getOrderItems() {
        return orderItems;
    }

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

OrderItemVo.java

package com.xfz.ssm.model.vo;

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

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 19:18
 */
public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

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

  1. 在xml中添加映射
    OrderMapper.xml
 
    
    
    
      
      
      
      
    
  

OrderItemMapper.xml


    
    
    
    
    
      
      
    
  
  1. 测试
    4.1 添加连表查询的sql语句
    OrderMapper.xml

OrderItemMapper.xml


4.2 OrderMapper.java

package com.xfz.ssm.mapper;

import com.xfz.ssm.model.Order;
import com.xfz.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;

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);

    OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
}

OrderItemMapper.java

package com.xfz.ssm.mapper;

import com.xfz.ssm.model.OrderItem;
import com.xfz.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;

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);

    OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
}

4.3 写一个接口(OneToManyService)

package com.xfz.ssm.service;

import com.xfz.ssm.model.vo.OrderItemVo;
import com.xfz.ssm.model.vo.OrderVo;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 19:41
 */
public interface OneToManyService {
    OrderVo queryOrderVoByOrderId(Integer orderId);

    OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
}

4.4 实现所写接口(OneToManyServiceImpl )

package com.xfz.ssm.service.impl;

import com.xfz.ssm.mapper.OrderItemMapper;
import com.xfz.ssm.mapper.OrderMapper;
import com.xfz.ssm.model.vo.OrderItemVo;
import com.xfz.ssm.model.vo.OrderVo;
import com.xfz.ssm.service.OneToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 19:59
 */
@Service
public class OneToManyServiceImpl implements OneToManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Override
    public OrderVo queryOrderVoByOrderId(Integer orderId) {
        return orderMapper.queryOrderVoByOrderId(orderId);
    }

    @Override
    public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
        return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    }
}

4.5 编写测试类(OneToManyServiceImplTest )

package com.xfz.ssm.service.impl;

import com.xfz.ssm.SpringBaseTest;
import com.xfz.ssm.model.OrderItem;
import com.xfz.ssm.model.vo.OrderItemVo;
import com.xfz.ssm.model.vo.OrderVo;
import com.xfz.ssm.service.OneToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 20:05
 */
public class OneToManyServiceImplTest extends SpringBaseTest {

    @Autowired
    private OneToManyService oneToManyService;
    @Test
    public void queryOrderVoByOrderId() {
        OrderVo orderVo=oneToManyService.queryOrderVoByOrderId(8);
        System.out.println(orderVo.getOrderNo());
        for (OrderItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }
    }

    @Test
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = oneToManyService.queryOrderItemVoByOrderItemId(36);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder().getOrderNo());
    }
}

4.6 测试结果
mybatis关联关系映射_第2张图片
mybatis关联关系映射_第3张图片

多对多的关联关系

  1. 在对应的类中添加引用
    HbookVo.java
package com.xfz.ssm.model.vo;

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

import java.util.List;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 20:18
 */
public class HbookVo extends Hbook {
    private List categories;

    public List getCategories() {
        return categories;
    }

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

CategoryVo.java

package com.xfz.ssm.model.vo;

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

import java.util.List;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 20:26
 */
public class CategoryVo extends Category {
    private List hbooks;

    public List getHbooks() {
        return hbooks;
    }

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

  1. 在中间类的xml中添加映射和sql语句(HbookCategoryMapper)



  
    
      
      
      
    
  

  
    
    
    
    
      
      
    
  

  
    
    
    
      
      
      
    
  

  
    bcid, bid, cid
  
  
  
    delete from t_hibernate_book_category
    where bcid = #{bcid,jdbcType=INTEGER}
  
  
    insert into t_hibernate_book_category (bcid, bid, cid
      )
    values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
      )
  
  
    insert into t_hibernate_book_category
    
      
        bcid,
      
      
        bid,
      
      
        cid,
      
    
    
      
        #{bcid,jdbcType=INTEGER},
      
      
        #{bid,jdbcType=INTEGER},
      
      
        #{cid,jdbcType=INTEGER},
      
    
  
  
    update t_hibernate_book_category
    
      
        bid = #{bid,jdbcType=INTEGER},
      
      
        cid = #{cid,jdbcType=INTEGER},
      
    
    where bcid = #{bcid,jdbcType=INTEGER}
  
  
    update t_hibernate_book_category
    set bid = #{bid,jdbcType=INTEGER},
      cid = #{cid,jdbcType=INTEGER}
    where bcid = #{bcid,jdbcType=INTEGER}
  

  
  
  


  1. HbookCategoryMapper .java
package com.xfz.ssm.mapper;

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

public interface HbookCategoryMapper {
    int deleteByPrimaryKey(Integer bcid);

    int insert(HbookCategory record);

    int insertSelective(HbookCategory record);

    HbookCategory selectByPrimaryKey(Integer bcid);

    int updateByPrimaryKeySelective(HbookCategory record);

    int updateByPrimaryKey(HbookCategory record);

    HbookVo queryByBookId(@Param("bookId") Integer bookId);

    CategoryVo queryByCid(@Param("cid") Integer cid);
}
  1. 写一个service接口(ManyToManyService )
package com.xfz.ssm.service;

import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 21:00
 */
public interface ManyToManyService {
    HbookVo queryByBookId(Integer bookId);

    CategoryVo queryByCid(Integer cid);
}

  1. 实现所写的接口(ManyToManyServiceImpl )
package com.xfz.ssm.service.impl;

import com.xfz.ssm.mapper.HbookCategoryMapper;
import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;
import com.xfz.ssm.service.ManyToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 21:03
 */
@Service
public class ManyToManyServiceImpl implements ManyToManyService {
    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;
    @Override
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }

    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}

  1. 编写测试类(ManyToManyServiceImplTest )
package com.xfz.ssm.service.impl;

import com.xfz.ssm.SpringBaseTest;
import com.xfz.ssm.model.Category;
import com.xfz.ssm.model.Hbook;
import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;
import com.xfz.ssm.service.ManyToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author xfz
 * @site www.xfz.com
 * @company zking
 * @create  2019-11-21 21:06
 */
public class ManyToManyServiceImplTest extends SpringBaseTest {

    @Autowired
    private ManyToManyService manyToManyService;
    @Test
    public void queryByBookId() {
        HbookVo hbookVo = manyToManyService.queryByBookId(8);
        System.out.println(hbookVo.getBookName());
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category.getCategoryName());
        }
    }

    @Test
    public void queryByCid() {
        CategoryVo categoryVo = manyToManyService.queryByCid(8);
        System.out.println(categoryVo.getCategoryName());
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook.getBookName());
        }
    }
}
  1. 测试结果
    mybatis关联关系映射_第4张图片
    mybatis关联关系映射_第5张图片

你可能感兴趣的:(上课内容)