zoukankan      html  css  js  c++  java
  • SSM多表查询

    因为在订单类(Order)中涉及产品类(Product)的产品名称(ProductName)和产品价格(ProductPrice),所以需要使用联合查询。

    1.项目结构:

    2.重新定义的实体类代码OrderProduct:

    public class OrderProduct extends Orders {
    //    此处继承Orders类并重新定义Product类
        private Product product;
    
        public Product getProduct() {
            return product;
        }
    
        public void setProduct(Product product) {
            this.product = product;
        }
        @Override
        public String toString() {
            return "OrderProduct{" +
                    "product=" + product +
                    '}';
        }
    }

    3.OrderService代码

    List<OrderProduct> findAll(int page, int size);
    //int page,int size为分页,如不用可以删除

    4.OrderServiceImpl代码

    @Service
    public class OrdersServiceImpl implements OrdersService {
       @Autowired
       private OrdersMapper ordersMapper;
        @Override
        public List<OrderProduct> findAll(int page, int size) {
        //此处为开启分页插件,page size为分页参数,如不用可以删除
            PageHelper.startPage(page,size);
            return this.ordersMapper.findAll();
        }
    }

    5.OrderMapper代码

    public interface OrdersMapper {
        int deleteByPrimaryKey(String id);
    
        int insert(Orders record);
        //查询所有
        List<OrderProduct> findAll();
        
        Orders selectByPrimaryKey(String id);
        
        int updateByPrimaryKey(Orders record);
    }

    6.OrderMapper.xml

    <resultMap id="OrderProduct" type="com.hhxy.plx.domain.OrderProduct">
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="ordernum" property="ordernum" jdbcType="VARCHAR" />
        <result column="ordertime" property="ordertime" jdbcType="TIMESTAMP" />
        <result column="peoplecount" property="peoplecount" jdbcType="INTEGER" />
        <result column="orderdesc" property="orderdesc" jdbcType="VARCHAR" />
        <result column="paytype" property="paytype" jdbcType="INTEGER" />
        <result column="orderstatus" property="orderstatus" jdbcType="INTEGER" />
        <result column="productid" property="productid" jdbcType="VARCHAR" />
        <result column="memberid" property="memberid" jdbcType="VARCHAR" />
        <association property="product" javaType="com.hhxy.plx.domain.Product">
          <id column="id" property="id" jdbcType="VARCHAR" />
          <result column="productName" property="productName" jdbcType="VARCHAR" />
          <result column="productPrice" property="productPrice" jdbcType="DECIMAL" />
        </association>
      </resultMap>
    <select id="findAll" resultMap="OrderProduct" >
    select
    a.* ,
    b.productName,
    b.productPrice
    # 这里为查询a的所有,b中的名字和价格
    from
    orders as a
    # 定义别名order为a
    left outer join
    product as b
    # 定义别名order为a
    on
    a.productid=b.id
    # a中的productid为b中的id

    </select>

    7.OrderController代码

     @Autowired
       private OrdersService ordersService;
        @RequestMapping("/findAll.do")
        public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,
                                    @RequestParam(name = "size",required = true,defaultValue = "4")int size, 
                        Model model)throws Exception{ ModelAndView mv = new ModelAndView(); List<OrderProduct> orderProductsList=ordersService.findAll(page, size); PageInfo pageinfo =new PageInfo(orderProductsList); model.addAttribute("pageinfo",pageinfo); model.addAttribute("orderProductsList",orderProductsList); mv.setViewName("orders-list"); return mv; }

    8.jsp页面

                                   <tbody>
                                        <c:forEach items="${pageinfo.list}" var="orders">
                                            <tr>
                                                <td><input name="ids" type="checkbox"></td>
                                                <td>${orders.id }</td>
                                                <td>${orders.ordernum }</td>
                                                <td>${orders.product.productName }</td>
                                                <td>${orders.product.productPrice }</td>
                                                <td>${orders.ordertimestr }</td>
                                                <td class="text-center">${orders.orderstatusstr }</td>
                                                <td class="text-center">
                                                    <button type="button" class="btn bg-olive btn-xs">订单</button>
                                                    <button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'">详情</button>
                                                    <button type="button" class="btn bg-olive btn-xs">编辑</button>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                    </tbody>

    9.结束。

  • 相关阅读:
    Django基础之cookie
    Django基础之redirect()
    Django基础之render()
    Django基础之JsonResponse对象
    Django基础之response对象
    scrapy框架自定制命令
    数据分析案例-拉勾网招聘信息
    爬虫之单线程多任务异步抓取
    数据分析之matplotlib使用
    数据分析之numpy使用
  • 原文地址:https://www.cnblogs.com/pansin/p/12755567.html
Copyright © 2011-2022 走看看