你不得不知道的MyBatis基础知识之重写OrderService(5)

it2024-12-24  10

文章目录

准备工作OrdersDetailsMapper.xmlOrdersDetailsMapper.javaJyInfoMapper.xmlOrdersDetailsMapper.java 重写OrderService效果图展示


  凭借我们在上一节做好的订单Mapper,以及本节新添加的OrdersDetailsMapper和JyInfoMapper来完成对OrderService的重写

准备工作

OrdersDetailsMapper.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"> <mapper namespace="com.lazy.mappers.OrdersDetailsMapper"> <insert id="addOrdersDetails" useGeneratedKeys="true" keyProperty="oddid" keyColumn="oddid"> INSERT INTO orderdetails(orderid, pid, pnum) VALUES <foreach collection="details" item="item" separator=","> (#{item.orderid.orderid},#{item.pid.pid},#{item.pnum}) </foreach> </insert> </mapper>

OrdersDetailsMapper.java

package com.lazy.mappers; import com.lazy.domain.OrderDetails; import org.apache.ibatis.annotations.Param; import java.util.List; public interface OrdersDetailsMapper { int addOrdersDetails(@Param("details") List<OrderDetails> details); }

JyInfoMapper.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"> <mapper namespace="com.lazy.mappers.JyInfoMapper"> <insert id="addJyInfo"> INSERT INTO jyinfo(JYCODE, JYTYPE, JYMONEY) VALUES (#{jycode},#{jytype},#{jymoney}) </insert> </mapper>

OrdersDetailsMapper.java

package com.lazy.mappers; import com.lazy.domain.JyInfo; public interface JyInfoMapper { int addJyInfo(JyInfo info); }

重写OrderService

package com.lazy.service; import com.lazy.domain.*; import com.lazy.mappers.JyInfoMapper; import com.lazy.mappers.OrdersDetailsMapper; import com.lazy.mappers.OrdersMapper; import com.lazy.tools.DBHelper; import com.lazy.tools.OrderTools; import com.lazy.utils.MybatisUtil; import org.apache.ibatis.session.SqlSession; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; public class OrderService { /** * 1. 生成订单流水 * 2. 插入订单表 * 3. 插入订单明细 * 4. 插入交易明细表 * * 以上都是一个事务 * * Order * OrderDetails * Product * Users * JyInfo * **/ private SqlSession sqlSession = MybatisUtil.getSqlSession(); public boolean createUsersOrder(Users users, Map<Products,Integer> cart){ // 1. 生成订单流水 String orderid = OrderTools.createOrderCode(); // 2. 生成订单对象 Orders orders = new Orders(); orders.setOrderid(orderid); orders.setUid(users); // 3. 生成获得价格 float totalPrice = 0f; List<OrderDetails> details = new ArrayList<>(); Set<Products> keys = cart.keySet(); for (Products p: keys){ Integer num = cart.get(p); totalPrice = totalPrice + p.getPprice() * num.intValue(); OrderDetails detail = new OrderDetails(); detail.setOrderid(orders); detail.setPid(p); detail.setPnum(num.intValue()); details.add(detail); } orders.setDetails(details); orders.setOrderprice(totalPrice); // 创建JyInfo对象 JyInfo jyInfo = new JyInfo(); jyInfo.setJycode(OrderTools.createOrderCode()); jyInfo.setJytype(1); jyInfo.setJymoney(totalPrice); // 调用持久层操作 OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class); OrdersDetailsMapper ordersDetailsMapper = sqlSession.getMapper(OrdersDetailsMapper.class); JyInfoMapper jyInfoMapper = sqlSession.getMapper(JyInfoMapper.class); int flag = ordersMapper.addOrder(orders); int flag2 = ordersDetailsMapper.addOrdersDetails(details); int flag3 = jyInfoMapper.addJyInfo(jyInfo); if((flag+flag2+flag3) == 2+details.size()){ sqlSession.commit(); return true; }else { sqlSession.rollback(); } return false; } }

效果图展示

最新回复(0)