1. 程式人生 > >springmvc-mybatis之一對多對映

springmvc-mybatis之一對多對映

1.首先來個需求

1.一個訂單表和一個訂單詳情表,一個訂單可以有多個訂單詳情,訂單詳情裡面主要記錄了購買商品的名稱以及數量。那麼,現在要查詢一個訂單的詳細情況。也就是說這是一對多查詢,從訂單表裡用其id查詢訂單詳情表的所有記錄。

2. 實現

2.1首先實現mepper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- 開發規範:  1.名稱空間必須與包名一樣  -->
<mapper namespace="com.ssm.mapping.OrderMapper"> <!-- 查詢一個訂單以及訂單詳情 一個訂單可以有多個訂單詳情 也就是一對多關係 --> <!-- 定義resultmap --> <resultMap type="order" id="orderAndOrderDetail"> <id column="id" property="id"/> <result
column="user_id" property="user_id"/>
<result column="number" property="number"/> <result column="create_time" property="create_time"/> <result column="note" property="note"/> <!--這裡的id使用items_id標示不一樣的商品詳情--> <collection property="list"
ofType="orderDetail">
<id column="items_id" property="id"/> <result column="items_id" property="items_id"/> <result column="items_num" property="items_num"/> </collection> </resultMap> <select id="findOrderAndOrderDetail" resultMap="orderAndOrderDetail"> SELECT orders.*, orderdetail.items_id, orderdetail.items_num FROM orders, orderdetail WHERE orderdetail.orders_id = orders.id AND orders.id=3 </select> </mapper>

2.2實現mapper.java

package com.ssm.mapping;

import com.ssm.model.Order;

public interface OrderMapper {
    //一個訂單可有多個訂單詳情函式名必須與mapper.xml中的id一致,
    public Order findOrderAndOrderDetail();

}

2.3實現springmvc的控制器

package com.ssm.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssm.mapping.OrderMapper;
import com.ssm.model.Order;

@Controller
@RequestMapping("/order")
public class OrderController {
    //自動注入
    @Autowired
    private OrderMapper ordermapper;

    @RequestMapping(value="/findorder",method=RequestMethod.GET)
    public String findOrderAndOrderDetail(HttpServletRequest request)
    {
        Order order = ordermapper.findOrderAndOrderDetail();
        request.setAttribute("order", order);

        return "order_detail";
    }

}

2.4編寫spring配置檔案ordermapper生成例項,感覺這裡應該有更好的方法實現,不然每次都要配置太麻煩,留著以後解決

<!-- 基於mapper代理的dao 開發不需要dao實現類   不能與原始dao介面開發一起使用  會造成原始的dao開發無法對映到相應的xml-->
    <bean id="ordermapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 構造方法需要兩個引數sqlsessionfactory 與  mapperInterface -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        <property name="mapperInterface" value="com.ssm.mapping.OrderMapper"></property>
    </bean>

    <!-- 頁面處理器根據返回的字串尋找jsp檔案-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value="/WEB-INF/views/"></property>
        <property name = "suffix" value = ".jsp"></property>
    </bean>

2.5jsp頁面

 <body>
    <p>訂單id:${requestScope.order.id}</p>
    <p>訂單number:${requestScope.order.number}</p>
    <p>訂單user_id:${requestScope.order.user_id}</p>
    <p>訂單create_time:${requestScope.order.create_time}</p>
    <p>訂單note:${requestScope.order.note}</p>
    <table border="1">
        <tr><th>商品id</th><th>商品數量</th></tr>
            <c:forEach items="${requestScope.order.list}" var="orderdetail">
                <tr>
                    <td>${orderdetail.items_id}</td>
                    <td>${orderdetail.items_num}</td>
                </tr>
            </c:forEach>
    </table>
  </body>

2.6如果需要使用mybatis的配置,如使用別名,那麼加上mybatis的配置檔案

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <!-- 批量定義別名 別名為類名 首字母大小寫均可-->
        <package name="com.ssm.model"/>
    </typeAliases>

</configuration>

以上,還有一些關於myeclipse除錯快捷鍵
F5 --單步除錯進入函式內部,注意使用,進入jar包內部就沒必要了。
F6 --單步除錯不進入函式內部,
F7 --由函式內部返回到呼叫處。
F8--一直執行到下一個斷點。