咕泡云商城-第二章商品模块-品牌模块
商品微服务-品牌模块
表结构和数据准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
DROP TABLE IF EXISTS `brand`; CREATE TABLE `brand` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '品牌id', `name` varchar(100) NOT NULL COMMENT '品牌名称', `image` varchar(1000) DEFAULT '' COMMENT '品牌图片地址', `initial` varchar(1) DEFAULT '' COMMENT '品牌的首字母', `sort` int(11) DEFAULT NULL COMMENT '排序', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='品牌表';
INSERT INTO `brand` VALUES ('1', '华为', 'https://sklll.oss-cn-beijing.aliyuncs.com/secby/eed72cc4-a9c1-4010-949a-03cef5b933d6.jpg', '', null); INSERT INTO `brand` VALUES ('2', '中兴', 'https://sklll.oss-cn-beijing.aliyuncs.com/secby/4fedb361-5ab3-4ad0-a667-580c1f37dff0.jpg', '', null); INSERT INTO `brand` VALUES ('3', '大疆', 'https://sklll.oss-cn-beijing.aliyuncs.com/secby/e8382c48-0487-4a9b-8fd0-a3716c3eea19.jpg', '', null);
|
项目整合mybatis-plus
引入依赖
在微服务公共依赖工程 mall-service-dependency中引入依赖
1 2 3 4 5 6
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency>
|
在 mall-api中引入如下依赖(编写JavaBean会用到MyBatis Plus的相关注解,引入依赖防止程序编译不通过):
1 2 3 4 5 6 7
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> <scope>provided</scope> </dependency>
|
在 mall-api创建 goods-api工程,用于创建 shop数据库表对应的实体Bean和Feign接口。
创建brand实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @Data @TableName("brand") public class BrandEntity {
@TableId(type= IdType.AUTO) private Integer id;
private String name;
private String image;
private String initial;
private Integer sort; }
|
在 mall-service中创建 goods-service微服务,用于操作 shop数据库。
- pom.xml代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>mall-service</artifactId> <groupId>com.chenghao.work</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>goods-service</artifactId>
<dependencies> <dependency> <groupId>com.chenghao.work</groupId> <artifactId>goods-api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies> </project>
|
- 构建bootstrap.yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| server: port: 8081
spring: application: name: mall-goods datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://101.34.5.93:3306/shop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: xxxxxx cloud: nacos: discovery: server-addr: 101.34.5.93:8848 mybatis-plus: mapper-locations: mapper/*.xml type-aliases-package: com.chenghao.work.mall.*.model configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl logging: pattern: console: "%msg%n"
|
- 构建启动类
GoodsServiceApplication
1 2 3 4 5 6 7 8 9 10 11 12
|
@SpringBootApplication @MapperScan(basePackages = "com.chenghao.work.mall.goods.mapper") @EnableDiscoveryClient public class GoodsServiceApplication {
public static void main(String[] args) { SpringApplication.run(GoodsServiceApplication.class,args); } }
|
- 启动后,观察nacos,发现成功注册上去了

根据mybatis-plus编写mapper,service,serviceImpl,利用http.client进行controller层测试
MyBatisPlus提供了很多通用方法:
1 2 3
| mapper(接口)->extends BaseMapper【增删改查】 service(接口)->extends IService【增删改查】 serviceImpl->extends ServiceImpl【增删改查】
|
Mapper创建,创建品牌的mapper继承BaseMapper
1 2 3 4 5 6 7
|
public interface BrandMapper extends BaseMapper<BrandEntity> { }
|
Service创建,创建品牌的service继承IService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public interface IBrandService extends IService<BrandEntity> {
Page<BrandEntity> queryPageList(Long currentPage, Long size, BrandEntity brand); }
|
ServiceImpl创建,创建品牌的serviceImpl继承ServiceImpl,实现IBrandService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Service public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandEntity> implements IBrandService {
@Resource private BrandMapper brandMapper;
@Override public Page<BrandEntity> queryPageList(Long currentPage, Long size, BrandEntity brand) { QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>(); if(StringUtils.isNotBlank(brand.getName())){ queryWrapper.like("name",brand.getName()); }
return brandMapper.selectPage(new Page<>(currentPage,size),queryWrapper); } }
|
Controller层,增删改方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
@RestController @RequestMapping("brand") public class BrandController {
@Resource private IBrandService brandService;
@PostMapping public RespResult add(@RequestBody BrandEntity brand){ brandService.save(brand); return RespResult.ok(); }
@PutMapping public RespResult update(@RequestBody BrandEntity brand){ brandService.updateById(brand); return RespResult.ok(); }
@DeleteMapping("/{id}") public RespResult delete(@PathVariable(value = "id") Integer id){ brandService.removeById(id); return RespResult.ok(); }
@PostMapping(value = "/list/{page}/{size}") public RespResult<Page<BrandEntity>> list( @PathVariable(value = "page")Long currentPage, @PathVariable(value = "size")Long size, @RequestBody(required = false) BrandEntity brand){ Page<BrandEntity> brandPage = brandService.queryPageList(currentPage,size,brand); return RespResult.ok(brandPage); } }
|
利用http.client进行测试,在项目的resources目录新建http目录,新建brand.http文件,这样有助与别人维护的时候直接进行测试,也不需要进行postman切换。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| ### 品牌新增操作 POST http://localhost:8081/brand Content-Type: application/json
{ "name":"小米", "image": "https://image.baidu.com/search", "initial": "", "sort": 1 }
### 品牌编辑操作 PUT http://localhost:8081/brand Content-Type: application/json
{ "id": 15, "name":"小米2", "image": "https://image.baidu.com/search", "initial": "", "sort": 1 }
### 品牌删除操作 DELETE http://localhost:8081/brand/14
### 品牌分页查询操作 POST http://localhost:8081/brand/list/1/10 Content-Type: application/json
{ "name": "小" }
|
代码均已经上传,可点击博客首页github按钮进行跳转,仓库为chenghaoshop