JavaEE-SSM-订单管理-后端(一)

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
    JavaEE-SSM-订单管理-后端(一)_第1张图片

  • pom文件:lombok、mysql driver、MyBatis、web、test、swagger

        
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.2.5.RELEASEversion>
            <relativePath/>
        parent>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <mybatis.starter.version>1.3.2mybatis.starter.version>
            <mysql.version>5.1.32mysql.version>
            <swagger.version>2.7.0swagger.version>
        properties>
    
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>${mybatis.starter.version}version>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
    
            
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger2artifactId>
                <version>${swagger.version}version>
            dependency>
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger-uiartifactId>
                <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相关

JavaEE-SSM-订单管理-后端(一)_第2张图片

测试类:TestMapper、TestService (可选)
JavaEE-SSM-订单管理-后端(一)_第3张图片
*

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

    JavaEE-SSM-订单管理-后端(一)_第4张图片

  • 编写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);
    }
}

  • 测试
    JavaEE-SSM-订单管理-后端(一)_第5张图片

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方法
    JavaEE-SSM-订单管理-后端(一)_第6张图片
    /**
     * 通过id查询详情
     * @param oid
     * @return
     */
    public Order selectById(String oid);
  • 编写OrderServiceImpl实现类中的selectById方法
    JavaEE-SSM-订单管理-后端(一)_第7张图片
    @Override
    public Order selectById(String oid) {
        //查询
        Order order = orderMapper.selectById(oid);
        //返回
        return order;
    }
  • 编写OrderController中的selectById方法
    JavaEE-SSM-订单管理-后端(一)_第8张图片
    /**
     * 查询详情
     * @param oid
     * @return
     */
    @GetMapping("/{oid}")
    public ResponseEntity<Order> selectById(@PathVariable("oid") String oid) {
        //查询
        Order order = orderService.selectById(oid);
        //返回
        return ResponseEntity.ok(order);
    }
  • 测试
    JavaEE-SSM-订单管理-后端(一)_第9张图片

你可能感兴趣的:(java-ee,java)