文章目录
准备工作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 {
private SqlSession sqlSession
= MybatisUtil
.getSqlSession();
public boolean createUsersOrder(Users users
, Map
<Products,Integer> cart
){
String orderid
= OrderTools
.createOrderCode();
Orders orders
= new Orders();
orders
.setOrderid(orderid
);
orders
.setUid(users
);
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
= 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;
}
}
效果图展示