day02_springboot综合案例(一)https://developer.aliyun.com/article/1433621
编写OrderMapper
public interface OrderMapper { /** * 分页查询所有订单 */ List<Orders> findAll(); /** * 查询订单详情 * @param orderId * @return */ Orders findById(Integer orderId); }
编写OrderService
public interface OrderService { /** * 分页查询所有订单 * @param page * @param size * @return */ List<Orders> findAll(Integer page, Integer size); /** * 查询订单详情 * @param orderId * @return */ Orders findById(Integer orderId); }
@Service public class OrderServiceImpl implements OrderService { @Autowired private OrderMapper orderMapper; /** * 查询订单详情 * @param id * @return */ @Override public Orders findById(Integer id) { return this.orderMapper.findById(id); } }
编写OrdersController
@Controller @RequestMapping("order") public class OrderController { @Autowired private OrderService orderService; /** * 查询订单详情 * @param id * @return */ @RequestMapping("findById") public String findById(Model model ,@RequestParam("id") Integer id){ Orders orders = this.orderService.findById(id); model.addAttribute("order",orders); return "order-show"; } }
编写OrdersMapper.xml
<!--查询订单详情--> <resultMap id="findByIdresultMap" type="Orders" autoMapping="true"> <!--映射本身orders--> <id property="id" column="oid"/> <!--映射Product--> <association property="product" javaType="Product" autoMapping="true"> <id property="id" column="pid"/> </association> <!--映射member--> <association property="member" javaType="Member" autoMapping="true"> <id property="id" column="mid"/> </association> <!--映射traveller--> <collection property="travellers" javaType="List" ofType="Traveller" autoMapping="true"> <id property="id" column="tid"/> </collection> </resultMap> <select id="findById" resultMap="findByIdresultMap"> SELECT *, o.id as oid, p.id AS pid, m.id AS mid, t.id AS tid FROM orders o LEFT JOIN product p ON o.productId = p.id LEFT JOIN member m ON o.memberId = m.id LEFT JOIN order_traveller ot ON ot.orderId = o.id LEFT JOIN traveller t ON ot.travellerId = t.id WHERE o.id = #{id} </select>
测试
新增订单-查询产品,会员,游客
添加订单,需要在订单中选择,产品(路线),会员, 游客
编写OrdersController
@Controller @RequestMapping("order") public class OrderController { @Autowired private ProductService productService; @Autowired private MemberService memberService; @Autowired private TravellerService travellerService; /** * 新增订单,同时查询出有哪些 product,有哪些 member ,添加的游客是哪些人 * @return */ @GetMapping("add") public String add(Model model){ //下拉查询 product, 在产品中已写过方法 List<Product> productList = this.productService.findAll(); //下拉查询 member List<Member> memberList = this.memberService.findAll(); //下拉查询 Traveller List<Traveller> travellerList = this.travellerService.findAll(); //显示到order-add.html model.addAttribute("productList",productList); model.addAttribute("memberList",memberList); model.addAttribute("travellerList",travellerList); return "order-add"; } }
编写Service
@Service public class MemberServiceImpl implements MemberService { @Autowired private MemberMapper memberMapper; /** * 查询所有 * @return */ @Override public List<Member> findAll() { return this.memberMapper.findAll(); } }
@Service public class TravellerServiceImpl implements TravellerService { @Autowired private TravellerMapper travellerMapper; /** * 查询游客 * @return */ @Override public List<Traveller> findAll() { return travellerMapper.findAll(); } }
day02_springboot综合案例(三)https://developer.aliyun.com/article/1433623