SpringBoot 项目
main函数中 SpringBoot加载启动类
SpringApplication.run(TpmOta1Application.class, args);
@SpringBoot注解的作用: 实例化Bean
@MapperScan(“com.ota.demo.mapper”)的作用:MyBatis需要找到CURD的接口。
@SpringBootApplication
@MapperScan("com.ota.demo.mapper")
//该注解用于找到mapper包中操作数据库的接口
public class TpmOta1Application {
SpringBoot项目结构
访问控制部分:
包名:package com.ota.demo.controller;
在该部分自动装配 服务接口Bean,根据用户不同的请求调用相应服务!自动装配会找到IOrderListService的实现类,即从服务实现部分查找(最终接口引用指向实现类的对象)。
@RestController
@RequestMapping("/order-list")
@EmployeeAnnotation(value = "订单管理")
@CustomerAnnotation(value = "订单相关模块")
public class OrderListController {
@Autowired
private IOrderListService orderListService;
@GetMapping(“/ofCustomer”)作用:匹配前端的需求,调用对应的服务。
如:
GET /order-list/ofCustomer?beginDateScope=12¤tPage=1&size=10&customerId=123&id=12&status=12 HTTP/1.1
Get请求,请求参数用@RequestParam可以设置默认值
@ApiOperation("根据顾客id,订单号,预约时间,订单状态,获取订单(带有状态名)")
@GetMapping("/ofCustomer")
public RespPageBean getOrderOfCustomer(@RequestParam(defaultValue = "1") Integer currentPage
, @RequestParam(defaultValue = "10") Integer size,String customerId, Integer id
,LocalDateTime[] beginDateScope, Integer status){
return orderListService.getOrderOfCustomer(currentPage,size,customerId,id,beginDateScope,status);
}
服务接口
package com.ota.demo.service;
服务接口继承了MyBatis框架的顶级Service接口,其作用在于
在底层MyBatis框架中IService接口的会调用实现类 IServiceImpl中自动装配的BaseMapper,完成对数据库的CRUD。
因而用户只需要写好pojo对象,mapper.xml、数据访问层的接口
public interface IOrderListService extends IService<OrderList> {
服务实现部分(业务逻辑实现)
package com.ota.demo.service.impl;
Pojo : Plain Ordinary Java Object
数据访问层
查询数据库基于
package com.ota.demo.mapper;
该包中的接口如订单操作接口:OrderListMapper 继承了 MyBatis框架中的BaseMapper,则OrderListMapper无需在mapper.xml文件写SQL语句,即可实现基本的CRUD功能。
关于复杂的查询语句,可以在OrderListMapper类中声明方法,然后在对应的mapper.xml文件中写SQL语句 和 定义返回结果:
<resultMap type="com.ota.demo.pojo.OrderList"
id="OrderOfCustomer" extends="BaseResultMap">
<!-- 一对一查询 -->
<association property="customer" javaType="com.ota.demo.pojo.Customer">
<result column="cname" property="name" />
</association>
<association property="dicStatus" javaType="com.ota.demo.pojo.DicStatus">
<result column="dname" property="name"/>
</association>
<association property="product" javaType="com.ota.demo.pojo.Product">
<result column="pname" property="name" />
</association>
</resultMap>
<!--根据顾客id,订单号,预约时间,订单状态,获取订单(带有状态名)-->
<select id="getOrderOfCustomer" resultMap="OrderOfCustomer">
SELECT
o.*,
c.name as cname,
d.name as dname,
p.name as pname
FROM
order_list AS o,
customer as c,
dic_status as d,
product as p
WHERE
1
and c.phone=o.userphone
and o.status=d.code
and p.id=o.productid
<if test="null!=customerId and ''!=customerId">
and c.id=#{customerId}
</if>
<if test="null!=id">
and o.id=#{id}
</if>
<if test="null!=status">
and o.status=#{status}
</if>
<if test="null!=beginDateScope and beginDateScope.length==2">
and o.estimatedUseTime between #{beginDateScope[0]} and #{beginDateScope[1]}
</if>
order by o.estimatedUseTime desc
</select>