day02_springboot综合案例

day02_springboot综合案例

订单操作

查询所有订单

查询所有订单流程

查询订单,要把订单对应的产品也要查出来

day02_springboot综合案例_第1张图片

day02_springboot综合案例_第2张图片day02_springboot综合案例_第3张图片

Orders实体类

@Data
public class Orders {
    private String id;
    private String orderNum;
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
    private Date orderTime;
    private String orderTimeStr;
    private int orderStatus;
    private String orderStatusStr;
    private int peopleCount;
    private Product product;
    private List<Traveller> travellers;
    private Member member;
    private Integer payType;
    private String payTypeStr;
    private String orderDesc;
    //用于添加订单时,添加产品,会员
    private Integer productId;
    private Integer memberId;

    public String getOrderTimeStr() {
        // 对日期格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (null != orderTime) {
            orderTimeStr = dateFormat.format(orderTime);
        }
        return orderTimeStr;
    }

    public String getPayTypeStr() {
        if (payType == 0){
            payTypeStr = "支付宝";
        } else if(payType == 1){
            payTypeStr = "微信";
        }else if(payType == 2){
            payTypeStr = "其他";
        }
        return payTypeStr;
    }


    public String getOrderStatusStr() {
        if (orderStatus == 0){
            orderStatusStr = "未支付";
        } else if(orderStatus == 1){
            orderStatusStr = "已支付";
        }
        return orderStatusStr;
    }
}

编写OrdersMapper

public interface OrderMapper {
    /**
     * 分页查询所有订单
     */
    List<Orders> findAll();

编写OrdersService


public interface OrderService {

    /**
     * 分页查询所有订单
     * @param page
     * @param size
     * @return
     */
    List<Orders> findAll(Integer page, Integer size);
}

@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询订单
     * @return
     */
    @Override
    public List<Orders> findAll() {
        return this.orderMapper.findAll();
    }
}

编写OrdersController

@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 查询所有订单
     */
    @RequestMapping("findAll")
    public String findAll( Model model ,
                                 @RequestParam(value = "page",defaultValue = "1") Integer page,
                                 @RequestParam(value = "size",defaultValue = "5") Integer size){

        PageHelper.startPage(page,size);
        List<Orders> orders = this.orderService.findAll();

        PageInfo pageInfo = new PageInfo(orders);

        model.addAttribute("pageInfo",pageInfo);
        return "order-list";
    }
}

编写OrdersMapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.yanqi.ssm.mapper.OrderMapper">
    
    
    <resultMap id="orders" type="Orders" autoMapping="true">
        <id column="id" property="id" />
        <association property="product" javaType="Product" autoMapping="true">
            <id property="id" column="id"/>
        association>
    resultMap>
    <select id="findAll" resultMap="orders">
      	SELECT * FROM orders o , product p WHERE o.productId = p.id
    select>
    
mapper>

测试

day02_springboot综合案例_第4张图片

订单详情

查询订单详情流程

查看订单详情,需要查看 产品 会员 旅客

day02_springboot综合案例_第5张图片

-- sql 分析
	SELECT
		o.orderNum as "订单号",
		p.productName as "路线",
		m.nickName as "下单人",
		t.name as "游客"
	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 = 5

day02_springboot综合案例_第6张图片

Member实体类

package cn.yanqi.ssm.pojo;

import lombok.Data;


@Data
public class Member {
    private String id;
    private String mname;
    private String nickname;
    private String mphoneNum;
    private String email;
}

Traveller实体类

package cn.yanqi.ssm.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class Traveller {
    private String id;
    private String name;
    private String sex;
    private String phoneNum;
    private Integer credentialsType;
    private String credentialsTypeStr;
    private String credentialsNum;
    private Integer travellerType;
    private String travellerTypeStr;

    public String getCredentialsTypeStr() {
        if(credentialsType != null){
            // 证件类型 0:身份证 1:护照 2:军官证
            if (credentialsType == 0) {
                credentialsTypeStr = "身份证";
            } else if (credentialsType == 1) {
                credentialsTypeStr = "护照";
            } else if (credentialsType == 2) {
                credentialsTypeStr = "军官证";
            }
        }
        return credentialsTypeStr;
    }

    public String getTravellerTypeStr() {
        if( travellerType != null){
            // 证件类型 0:成人 1:儿童
            if (travellerType == 0) {
                travellerTypeStr = "成人";
            } else if (travellerType == 1) {
                travellerTypeStr = "儿童";
            }

        }
        return travellerTypeStr;
    }
}

编写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">
        
        <id property="id" column="oid"/>

        
        <association property="product" javaType="Product" autoMapping="true">
            <id property="id" column="pid"/>
        association>

        
        <association property="member" javaType="Member" autoMapping="true">
            <id property="id" column="mid"/>
        association>

        
        <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>

测试

day02_springboot综合案例_第7张图片

新增订单-查询产品,会员,游客

添加订单,需要在订单中选择,产品(路线),会员, 游客

day02_springboot综合案例_第8张图片

编写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();
    }
}

编写Mapper

@Repository
public interface TravellerMapper {
    /**
     * 查询游客
     * @return
     */
    List<Traveller> findAll();
}
public interface MemberMapper {

    /**
     * 查询所有会员
     * @return
     */
    List<Member> findAll();
}

编写Mapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.yanqi.mapper.TravellerMapper">

	<select id="findAll" resultType="Traveller">
		select * from traveller
	select>

mapper>

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.yanqi.mapper.MemberMapper">

	<select id="findAll" resultType="Member">
		select * from member
	select>

mapper>

新增订单

编写OrderController

/**
 * @Author: yanqi
 * @Date: 9:20
 * @Desc: 订单模块
 */
@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private TravellerService travellerService;

    @Autowired
    private OrderAndTravellerService orderAndTravellerService;


    /**
     * 新增订单
     * @param orders
     * @return
     */
    @PostMapping("save")
    public String save(Orders orders){

        //添加订单信息
        this.orderService.save(orders);

        //根据添加订单orderNum 查询订单id  select id from orders where orderNum = #{orderNum}
        Integer OrderId = orderService.findByOrderNum(orders.getOrderNum());

        //中间表order_traveller 添加   参数1:订单id   参数2:旅客ids
        this.orderAndTravellerService.add(OrderId,orders.getTravellerId());

        return "redirect:findAll";
    }

}

编写Service


@Service
public class OrderServiceImpl  implements OrderService {

    @Autowired
    public OrderMapper orderMapper;
	/**
     * 新增订单
     * @param orders
     */
    @Override
    public void save(Orders orders) {
        this.orderMapper.save(orders);
    }
}

@Service
public class OrderServiceImpl  implements OrderService {

    @Autowired
    public OrderMapper orderMapper;
    
	/**
     * 根据添加orderNum查询订单id
     * @param orderNum
     * @return
     */
    @Override
    public Integer findByOrderNum(String orderNum) {
        return this.orderMapper.findByOrderNum(orderNum);
    }
}    
/**
 * @Author: yanqi
 * @Date: 14:41
 * @Desc: 中间表order_traveller 添加   参数1:订单id   参数2:旅客ids
 */
@Service
public class OrderAndTravellerServiceImpl implements OrderAndTravellerService {

    @Autowired
    private OrderAndTravellerMapper orderAndTravellerMapper;

    //中间表order_traveller 添加   参数1:订单id   参数2:旅客ids
    @Override
    public void add(Integer orderId, List<Integer> travellerIds) {
        this.orderAndTravellerMapper.add(orderId,travellerIds);
    }
    
}

编写Mapper

@Repository
public interface OrderMapper {

   /**
     * 新增订单
     * @param orders
     */
    void save(Orders orders);
    
     /**
     * 根据添加orderNum查询订单id
     * @param orderNum
     * @return
     */
    Integer findByOrderNum(String orderNum);
}
@Repository
public interface OrderAndTravellerMapper {

	//中间表order_traveller 添加   参数1:订单id   参数2:旅客ids
    void add(@Param("orderId") Integer orderId,@Param("travellerId") List<Integer> travellerId );
}

编写Mapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.yanqi.mapper.OrderMapper">

	
	<select id="findByOrderNum" resultType="integer">
		select id from orders where orderNum = #{orderNum}
	select>


	
	<insert id="save">
		insert into orders(
		orderNum,
		orderTime,
		peopleCount,
		orderDesc,
		payType,
		orderStatus,
		productId,
		memberId)
		values(#{orderNum},
		#{orderTime},
		#{peopleCount},
		#{orderDesc},
		#{payType},
		#{orderStatus},
		#{productId},
		#{memberId})
	insert>
mapper>

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.yanqi.mapper.OrderAndTravellerMapper">

	<insert id="add">
		INSERT INTO order_traveller (orderId, travellerId)
		VALUES
		<foreach collection="travellerId" item="travellerId" separator=",">
			(#{orderId}, #{travellerId})
		foreach>
	insert>


mapper>

=“save”>
insert into orders(
orderNum,
orderTime,
peopleCount,
orderDesc,
payType,
orderStatus,
productId,
memberId)
values(#{orderNum},
#{orderTime},
#{peopleCount},
#{orderDesc},
#{payType},
#{orderStatus},
#{productId},
#{memberId})




```xml




	
		INSERT INTO order_traveller (orderId, travellerId)
		VALUES
		
			(#{orderId}, #{travellerId})
		
	



你可能感兴趣的:(Java,spring,boot,后端,java)