电商项目专题(六)-整合MyBatisPlus

1.准备测试相关的数据

本例中以电商项目中的品牌表为例:
在这里插入图片描述

DROP TABLE IF EXISTS `tb_brand`;
CREATE TABLE `tb_brand`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '品牌名称',
  `image` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '品牌图片',
  `first_char` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '品牌首字母',
  `del` int(6) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 45 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

表中的测试数据:

INSERT INTO `tb_brand` VALUES (1, '联想', NULL, 'L', 0);
INSERT INTO `tb_brand` VALUES (2, '华为', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (3, '三星', NULL, 'S', 0);
INSERT INTO `tb_brand` VALUES (4, '小米', NULL, 'X', 0);
INSERT INTO `tb_brand` VALUES (5, 'OPPO', NULL, 'O', 0);
INSERT INTO `tb_brand` VALUES (6, '360', NULL, 'S', 0);
INSERT INTO `tb_brand` VALUES (7, '中兴', NULL, 'Z', 0);
INSERT INTO `tb_brand` VALUES (8, '魅族', NULL, 'M', 0);
INSERT INTO `tb_brand` VALUES (9, '苹果', NULL, 'P', 0);
INSERT INTO `tb_brand` VALUES (10, 'VIVO', NULL, 'V', 0);
INSERT INTO `tb_brand` VALUES (11, '诺基亚', NULL, 'N', 0);
INSERT INTO `tb_brand` VALUES (12, '锤子', NULL, 'C', 0);
INSERT INTO `tb_brand` VALUES (13, '长虹', NULL, 'C', 0);
INSERT INTO `tb_brand` VALUES (14, '海尔', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (15, '飞利浦', NULL, 'F', 0);
INSERT INTO `tb_brand` VALUES (16, 'TCL', NULL, 'T', 0);
INSERT INTO `tb_brand` VALUES (17, '海信11', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (18, '夏普', NULL, 'X', 0);
INSERT INTO `tb_brand` VALUES (19, '创维', NULL, 'C', 0);
INSERT INTO `tb_brand` VALUES (20, '东芝', NULL, 'D', 0);
INSERT INTO `tb_brand` VALUES (21, '康佳', NULL, 'K', 0);
INSERT INTO `tb_brand` VALUES (22, 'LG', NULL, 'L', 0);
INSERT INTO `tb_brand` VALUES (23, '美特斯邦威', NULL, 'M', 0);
INSERT INTO `tb_brand` VALUES (24, '森马', NULL, 'S', 0);
INSERT INTO `tb_brand` VALUES (25, '李维斯', NULL, 'L', 0);
INSERT INTO `tb_brand` VALUES (26, '杰克琼斯', NULL, 'J', 0);
INSERT INTO `tb_brand` VALUES (27, '浪琴牌', NULL, 'L', 0);
INSERT INTO `tb_brand` VALUES (28, 'Apple', NULL, 'A', 0);
INSERT INTO `tb_brand` VALUES (29, '惠普', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (30, '贵人鸟', NULL, 'G', 0);
INSERT INTO `tb_brand` VALUES (32, '阿迪达斯', NULL, 'A', 0);
INSERT INTO `tb_brand` VALUES (33, '哈哈', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (34, '呵呵', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (35, '测试', NULL, 'C', 0);
INSERT INTO `tb_brand` VALUES (38, '重新更新', NULL, 'C', 0);
INSERT INTO `tb_brand` VALUES (39, '新增', NULL, 'X', 0);
INSERT INTO `tb_brand` VALUES (40, '测试', NULL, 'C', 0);
INSERT INTO `tb_brand` VALUES (41, '天梭', NULL, 'T', 0);
INSERT INTO `tb_brand` VALUES (42, '花花公子', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (43, '恒都', NULL, 'H', 0);
INSERT INTO `tb_brand` VALUES (44, '大洋', NULL, 'D', 0);

2.整合MyBatis-Plus

2.1.添加POM引用



 
 <dependency>
     <groupId>com.baomidougroupId>
     <artifactId>mybatis-plus-boot-starterartifactId>
     <version>3.0.3version>
 dependency>

将项目中的MyBatis依赖更新为MyBatis Plus依赖

2.2.实体类

package com.yigou.item.pojo;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("tb_brand")
public class Brand {

  @TableId(value = "id",type= IdType.AUTO)
  private long id;
  private String name;
  private String image;
  private String firstChar;
  private long del;


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }


  public String getImage() {
    return image;
  }

  public void setImage(String image) {
    this.image = image;
  }


  public String getFirstChar() {
    return firstChar;
  }

  public void setFirstChar(String firstChar) {
    this.firstChar = firstChar;
  }


  public long getDel() {
    return del;
  }

  public void setDel(long del) {
    this.del = del;
  }

}

2.3.Mapper

package com.yigou.item.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yigou.item.pojo.Brand;

/**
 * @author bruceliu
 * @create 2019-09-13 14:57
 * @description
 */
public interface BrandMapper extends BaseMapper<Brand> {

}

2.4.service接口

package com.yigou.item.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yigou.item.mapper.BrandMapper;
import com.yigou.item.pojo.Brand;
import org.springframework.stereotype.Service;

/**
 * @author bruceliu
 * @create 2019-09-13 15:00
 * @description
 */
public interface BrandService extends IService<Brand> {


}

2.5.service实现类

package com.yigou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yigou.item.mapper.BrandMapper;
import com.yigou.item.pojo.Brand;
import com.yigou.item.service.BrandService;
import org.springframework.stereotype.Service;

/**
 * @author bruceliu
 * @create 2019-09-13 15:03
 * @description
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {


}

2.6.Controller

package com.yigou.item.controller;

import com.yigou.common.enums.ExceptionEnum;
import com.yigou.common.exception.YigouException;
import com.yigou.item.pojo.Brand;
import com.yigou.item.pojo.Item;
import com.yigou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author bruceliu
 * @create 2019-09-13 15:04
 * @description
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("list")
    public ResponseEntity<List<Brand>> list(Brand brand){
        List<Brand> brands = brandService.list(null);
        return ResponseEntity.status(HttpStatus.CREATED).body(brands);
    }
}

2.7.MyBatisPlus配置

package com.yigou.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author bruceliu
 * @create 2019-09-13 14:01
 * @description
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.yigou.item.mapper")
public class MyBatisPlusConfig {

    // mybatis-plus分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

}

3.测试

3.1.直接访问服务

访问地址: http://127.0.0.1:8081/brand/list
电商项目专题(六)-整合MyBatisPlus_第1张图片

3.2.通过网关访问服务

访问地址:http://127.0.0.1:10010/api/item-service/brand/list
电商项目专题(六)-整合MyBatisPlus_第2张图片

你可能感兴趣的:(电商专题)