Java SMM框架关联关系映射示例讲解

一、一对多关联关系

需求:通过订单号查询本次订单的详情信息以及对应的所有订单

1.1 逆向生成代码配置文件

generatorConfig.xml




    
    
    
    
    
    
        
        
            
             
        
        
        
        
        
            
            
        
        
        
        
        
            
            
            
            
            
            
            
            
        
        
        
            
            
        
        
        
        
        
        
            
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       

1.2 编写配置文件OrderMapper.xml




  
    
      
      
    
  
  
    order_id, order_no
  
  
    
    
    
      
      
      
      
    
  
  
  
  
    delete from t_hibernate_order
    where order_id = #{orderId,jdbcType=INTEGER}
  
  
    insert into t_hibernate_order (order_id, order_no)
    values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
  
  
    insert into t_hibernate_order
    
      
        order_id,
      
      
        order_no,
      
    
    
      
        #{orderId,jdbcType=INTEGER},
      
      
        #{orderNo,jdbcType=VARCHAR},
      
    
  
  
    update t_hibernate_order
    
      
        order_no = #{orderNo,jdbcType=VARCHAR},
      
    
    where order_id = #{orderId,jdbcType=INTEGER}
  
  
    update t_hibernate_order
    set order_no = #{orderNo,jdbcType=VARCHAR}
    where order_id = #{orderId,jdbcType=INTEGER}
  

1.3 建立Vo类

BookVo:

package com.xlb.ssm.model.vo;
import com.xlb.ssm.model.Order;
import com.xlb.ssm.model.OrderItem;
import java.util.List;
/**
 * Vo类不仅仅包含当前表的信息、还能包含相关联的表的信息
 *
 * 当前订单会有一个或者多个订单项,多个订单先必须要集合进行接受
 */
public class OrderVo extends Order {
    //需求1:通过订单号查询本次订单的详情信息及对应的所有订单
    private List orderItems;
    public List getOrderItems() {
        return orderItems;
    }
    public void setOrderItems(List orderItems) {
        this.orderItems = orderItems;
    }
}

BookItemVo:

package com.xlb.ssm.model.vo;
import com.xlb.ssm.model.Order;
import com.xlb.ssm.model.OrderItem;
import lombok.Data;
/**
 *
 * 某一个订单项一定属于某个订单的
 */
@Data
public class OrderItemVo extends OrderItem {
    private Order order;
    public Order getOrder() {
        return order;
    }
    public void setOrder(Order order) {
        this.order = order;
    }
}

添加@Data注释:省略了get、set、toString、构造方法。

Java SMM框架关联关系映射示例讲解_第1张图片

再然后分别在OrderMapper.java 和 OrderBiz 里面添加查询的方法

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

创建实现类OrderBizImpl

package com.xlb.ssm.biz.impl;
import com.xlb.ssm.biz.OrderBiz;
import com.xlb.ssm.mapper.OrderMapper;
import com.xlb.ssm.model.Order;
import com.xlb.ssm.model.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderBizImpl implements OrderBiz {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public int deleteByPrimaryKey(Integer orderId) {
        return 0;
    }
    @Override
    public int insert(Order record) {
        return 0;
    }
    @Override
    public int insertSelective(Order record) {
        return 0;
    }
    @Override
    public Order selectByPrimaryKey(Integer orderId) {
        return null;
    }
    @Override
    public OrderVo queryOrderVoByOrderId(Integer orderId) {
        return orderMapper.queryOrderVoByOrderId(orderId);
    }
    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return 0;
    }
    @Override
    public int updateByPrimaryKey(Order record) {
        return 0;
    }
}

测试类OrderBizImplTest

package com.xlb.ssm.biz.impl;
import com.xlb.ssm.biz.OrderBiz;
import com.xlb.ssm.model.Order;
import com.xlb.ssm.model.vo.OrderVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class OrderBizImplTest {
    @Autowired
    private OrderBiz orderBiz;
    //需求1:通过订单号查询本次订单的详情信息及对应的所有订单
    //select * from t_hibernate_order o,t_hibernate_order_item oi
    //where o.order_id = oi.oid and o.order_id = 9
    @Test
    public void queryOrderVoByOrderId() {
        OrderVo orderVo = orderBiz.queryOrderVoByOrderId(9);
        System.out.println(orderVo);
        //打印订单项
        System.out.println(orderVo.getOrderItems());
    }
}

结果

Java SMM框架关联关系映射示例讲解_第2张图片

二、多对对

需求:根据书籍id,查询书籍信息以及所属类别信息;

2.1 逆向生成代码

配置要生成的实体类:generatorConfig.xml




    
    
    
    
    
    
        
        
            
             
        
        
        
        
        
            
            
        
        
        
        
        
            
            
            
            
            
            
            
            
        
        
        
            
            
        
        
        
        
        
        
            
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
      

2.2 创建Vo类

CategoryVo

package com.xlb.ssm.model.vo;
import com.xlb.ssm.model.Category;
import com.xlb.ssm.model.Hbook;
import lombok.Data;
import lombok.ToString;
import java.util.List;
@Data
public class CategoryVo extends Category {
    private List hbooks;
    public List getHbooks() {
        return hbooks;
    }
    public void setHbooks(List hbooks) {
        this.hbooks = hbooks;
    }
}

HbookVo

package com.xlb.ssm.model.vo;
import com.xlb.ssm.model.Category;
import com.xlb.ssm.model.Hbook;
import lombok.Data;
import java.util.List;
@Data
public class HbookVo extends Hbook {
    private List categories;
    public List getCategories() {
        return categories;
    }
    public void setCategories(List categories) {
        this.categories = categories;
    }
}

创建实现类:HbookCategoryBizImpl

package com.xlb.ssm.biz.impl;
import com.xlb.ssm.biz.HbookCategoryBiz;
import com.xlb.ssm.mapper.HbookCategoryMapper;
import com.xlb.ssm.model.HbookCategory;
import com.xlb.ssm.model.vo.CategoryVo;
import com.xlb.ssm.model.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;
    @Override
    public int deleteByPrimaryKey(Integer bcid) {
        return 0;
    }
    @Override
    public int insert(HbookCategory record) {
        return 0;
    }
    @Override
    public int insertSelective(HbookCategory record) {
        return 0;
    }
    @Override
    public HbookCategory selectByPrimaryKey(Integer bcid) {
        return null;
    }
    @Override
    public int updateByPrimaryKeySelective(HbookCategory record) {
        return 0;
    }
    @Override
    public int updateByPrimaryKey(HbookCategory record) {
        return 0;
    }
    @Override
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }
    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}

然后再创建测试类进行测试:HbookCategoryBizImplTest

package com.xlb.ssm.biz.impl;
import com.xlb.ssm.biz.HbookCategoryBiz;
import com.xlb.ssm.model.vo.CategoryVo;
import com.xlb.ssm.model.vo.HbookVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class HbookCategoryBizImplTest {
    @Autowired
    private HbookCategoryBiz hbookCategoryBiz;
    @Test
    public void queryByBookId() {
        HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
        System.out.println(hbookVo);
    }
    @Test
    public void queryByCid() {
        CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
        System.out.println(categoryVo);
    }
}

结果

Java SMM框架关联关系映射示例讲解_第3张图片

到此这篇关于Java SMM框架关联关系映射示例讲解的文章就介绍到这了,更多相关SSM关联关系映射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Java SMM框架关联关系映射示例讲解)