您的位置 首页 教育

SSM??关联关系映射

目录一、一对多关联关系1.1 逆向生成代码配置文件1.2 编写配置文件:OrderMapper.xml1.3 建立Vo类二、多对对2.1 逆向生成代码2.2 创建Vo类一、一对多关联关系 需求:通过订单号查询本次订单的详情信息以及对应的所有订单 1.1 逆向生成代码配置文件 generatorConfig.xml 1.2 编写配置文件:OrderMapper.xml order_id, orde

目录

  • 一、一对多关联关系
    • 1.1 逆向生成代码配置文件
    • 1.2 编写配置文件:OrderMapper.xml
    • 1.3 建立Vo类
  • 二、多对对
    • 2.1 逆向生成代码
    • 2.2 创建Vo类

一、一对多关联关系

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

1.1 逆向生成代码配置文件

generatorConfig.xml



    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        

        
        
        
        
        
        <!--
        
        
        
        <!--<!–  –>-->
        
        <!--<!–  –>-->
        

       
        
 
        
        

    


1.2 编写配置文件:OrderMapper.xml



  
    
      
      
    
  
  
    order_id, order_no
  
 
  
    
    
    
      
      
      
      
    
 
  
 
  
    select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid and o.order_id = #{orderId}
  
 
  
    select 
    
    from t_hibernate_order
    where order_id = #{orderId,jdbcType=INTEGER}
  
  
    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;
 

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、构造方法。

再然后分别在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());
    }
 
}

结果

二、多对对

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

2.1 逆向生成代码

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



    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        

        
        
        
        
        
        <!--
        
        
        
        <!--<!–  –>-->
        
        <!--<!–  –>-->
        

      
        
 
        
        
 
        
        

————————————————
版权声明:本文为CSDN博主「_Leaf1217」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://www.0514zx.com/hao123/202208182035181109/202208182035181110

    

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

结果

本文来自网络,不代表0514资讯网立场,转载请注明出处:https://www.0514zx.com/info/65835.html
0514zx.com

作者: 0514zx.com

优质职场领域创作者
联系我们

联系我们

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部
CMS教程网 趣丁号猫咪日记趣文库每日星座运势电脑技术初夏网站长分类目录丝路百科趣学百科趣科技号热门榜论文之家问百科网