day02_springboot综合案例(三)

简介: day02_springboot综合案例

day02_springboot综合案例(二)https://developer.aliyun.com/article/1433622


编写Mapper

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

编写Mapper.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="cn.yanqi.mapper.TravellerMapper">
  <select id="findAll" resultType="Traveller">
    select * from traveller
  </select>
</mapper>
<?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="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

<?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="cn.yanqi.mapper.OrderMapper">
  <!--根据添加orderNum查询订单id-->
  <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>
<?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="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
<?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="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>


目录
相关文章
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
|
11月前
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
2414 1
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
|
11月前
|
Web App开发 JavaScript Java
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
这篇文章是关于如何使用Spring Boot整合Elasticsearch,并通过REST客户端操作Elasticsearch,实现一个简单的搜索前后端,以及如何爬取京东数据到Elasticsearch的案例教程。
691 0
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
|
12月前
|
消息中间件 Java Kafka
springboot项目启动报错-案例情景介绍
springboot项目启动报错-案例情景介绍
508 2
|
缓存 NoSQL Java
案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
233 5
|
JSON 前端开发 Java
Springboot mvc开发之Rest风格及RESTful简化开发案例
Springboot mvc开发之Rest风格及RESTful简化开发案例
185 2
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
179 2
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
179 0
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
IDE Java Maven
Springboot中Processor注解概念以及实战案例
【5月更文挑战第28天】在Spring Boot中,没有直接名为Processor的注解。不过,你可能是在谈论与Spring Boot相关的注解处理器(Annotation Processors)的概念,尤其是在处理自定义注解或@ConfigurationProperties注解时的情况。
681 1