2.后端:订单管理
2.1 环境搭建
2.1.1 SQL语句
create database day045; use day045; create table user( uid varchar(20) primary key, username varchar(50) ); create table `order`( oid varchar(20) primary key, price double, user_id varchar(20) ); insert into user(uid,username) values('u001','jack'),('u002','rose'); insert into `order`(oid,price,user_id) values('x001',55,'u001'); insert into `order`(oid,price,user_id) values('x002',34,'u001'); insert into `order`(oid,price,user_id) values('x003',754,'u001'); insert into `order`(oid,price,user_id) values('x004',3453,'u002'); insert into `order`(oid,price,user_id) values('x005',222,'u002');
2.1.2 构建项目
-
项目名:day052_ssm
-
pom文件:lombok、mysql driver、MyBatis、web、test、swagger
-
yml文件:端口、数据源dataSource *、日志
-
启动类:
-
拷贝配置类:swagger相关
-
测试类:TestMapper、TestService (可选)
-
项目名:day052_ssm
-
pom文件:lombok、mysql driver、MyBatis、web、test、swagger
<!--确定spring boot版本--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <mybatis.starter.version>1.3.2</mybatis.starter.version> <mysql.version>5.1.32</mysql.version> <swagger.version>2.7.0</swagger.version> </properties> <dependencies> <!--web开发启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--test开发启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- mybatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.starter.version}</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> </dependencies>
-
yml文件:端口、数据源dataSource *、日志
#端口号 server: port: 8080 #数据源(连接池) spring: datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/day045?useUnicode=true&characterEncoding=utf8 username: root password: '1234' #javabean与表驼峰映射 mybatis: configuration: map-underscore-to-camel-case: true #日志,给com.czxy.mapper包设置自己的日志级别 logging: level: com: czxy: mapper: debug
-
启动类:
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
拷贝配置类:swagger相关
测试类:TestMapper、TestService (可选)
*
package com.czxy.mapper; import com.czxy.OrderApplication; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = OrderApplication.class) public class TestOrderMapper { }
2.2 订单管理
2.2.1 JavaBean
package com.czxy.domain; import lombok.Data; @Data public class Order { private String oid; private Integer price; private String userId; }
2.2.2 基本结构
-
编写OrderMapper接口
-
编写OrderService接口
-
编写OrderServiceImpl实现类
-
编写OrderController
-
编写OrderMapper接口
package com.czxy.mapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface OrderMapper { }
-
编写OrderService接口
package com.czxy.service; public interface OrderService { }
-
编写OrderServiceImpl实现类
package com.czxy.service.impl; import com.czxy.mapper.OrderMapper; import com.czxy.service.OrderService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @Service @Transactional public class OrderServiceImpl implements OrderService { @Resource private OrderMapper orderMapper; }
-
编写OrderController
package com.czxy.controller; import com.czxy.service.OrderService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/order") public class OrderController { @Resource private OrderService orderService; }
2.2.3 查询所有
-
编写OrderMapper接口中的selectAll方法
-
编写OrderService接口中的selectAll方法
-
编写OrderServiceImpl实现类中的selectAll方法
-
编写OrderController中的selectAll方法
-
编写OrderMapper接口中的selectAll方法
package com.czxy.mapper; import com.czxy.domain.Order; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface OrderMapper { /** * 查询所有 * @return */ @Select("select * from `order`") public List<Order> selectAll(); }
-
编写OrderService接口中的selectAll方法
package com.czxy.service; import com.czxy.domain.Order; import java.util.List; public interface OrderService { /** * 查询所有 * @return */ public List<Order> selectAll(); }
- 编写OrderServiceImpl实现类中的selectAll方法
package com.czxy.service.impl; import com.czxy.domain.Order; import com.czxy.mapper.OrderMapper; import com.czxy.service.OrderService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; @Service @Transactional public class OrderServiceImpl implements OrderService { @Resource private OrderMapper orderMapper; @Override public List<Order> selectAll() { //查询 List<Order> orderList = orderMapper.selectAll(); //返回 return orderList; } }
- 编写OrderController中的selectAll方法
package com.czxy.controller; import com.czxy.domain.Order; import com.czxy.service.OrderService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("/order") public class OrderController { @Resource private OrderService orderService; @GetMapping public ResponseEntity<List<Order>> selectAll() { //查询 List<Order> orderList = orderService.selectAll(); //返回 return ResponseEntity.ok(orderList); } }
- 测试
2.2.4 查询详情
-
编写OrderMapper接口中的selectById方法
-
编写OrderService接口中的selectById方法
-
编写OrderServiceImpl实现类中的selectById方法
-
编写OrderController中的selectById方法
-
编写OrderMapper接口中的selectById方法
/** * 查询详情 * @param oid * @return */ @Select("select * from `order` where oid=#{oid}") public Order selectById(@Param("oid") String oid);
- 编写OrderService接口中的selectById方法
/** * 通过id查询详情 * @param oid * @return */ public Order selectById(String oid);
- 编写OrderServiceImpl实现类中的selectById方法
@Override public Order selectById(String oid) { //查询 Order order = orderMapper.selectById(oid); //返回 return order; }
- 编写OrderController中的selectById方法
/** * 查询详情 * @param oid * @return */ @GetMapping("/{oid}") public ResponseEntity<Order> selectById(@PathVariable("oid") String oid) { //查询 Order order = orderService.selectById(oid); //返回 return ResponseEntity.ok(order); }
- 测试