咕泡云商城-第二章商品模块-品牌模块

商品微服务-品牌模块

表结构和数据准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- ----------------------------
-- Table structure for brand
-- ----------------------------
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='品牌表';

-- ----------------------------
-- Records of brand
-- ----------------------------
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
<!--MyBatis Plus-->
<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
<!--MyBatis Plus-->
<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 {
/**
* 品牌ID
*/
@TableId(type= IdType.AUTO)
private Integer id;
/**
* 品牌名字
*/
private String name;
/**
* 品牌图片
*/
private String image;
/**
* 品牌首字母
*/
private String initial;
/**
* 品牌排序
*/
private Integer sort;
}

mall-service中创建 goods-service微服务,用于操作 shop数据库。
  1. 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>
  1. 构建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
# nacos 注册地址
cloud:
nacos:
discovery:
server-addr: 101.34.5.93:8848

mybatis-plus:
#复杂的操作可能需要自己写SQL,SQL可以写到xml文件中,这里指定和Dao对应的xml文件,此时我们需要在resources中创建一个mapper目录。
mapper-locations: mapper/*.xml
# 指定JavaBean的别名包,和MyBatis用法一样
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"
  1. 构建启动类 GoodsServiceApplication
1
2
3
4
5
6
7
8
9
10
11
12
/**
* @author chenghao
*/
@SpringBootApplication
@MapperScan(basePackages = "com.chenghao.work.mall.goods.mapper")
@EnableDiscoveryClient
public class GoodsServiceApplication {

public static void main(String[] args) {
SpringApplication.run(GoodsServiceApplication.class,args);
}
}
  1. 启动后,观察nacos,发现成功注册上去了

  1. 根据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
/**
* @name: BrandMapper
* @author: chenghao
* @date: 2023/1/12
*/
public interface BrandMapper extends BaseMapper<BrandEntity> {
}

Service创建,创建品牌的service继承IService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @name: BrandService
* @author: chenghao
* @date: 2023/1/12
*/
public interface IBrandService extends IService<BrandEntity> {
/**
*
* @param currentPage 当前页面
* @param size 页面大小
* @param brand 品牌实体
* @return 品牌分页对象
*/
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
/**
* @name: BrandServiceImpl
* @author: chenghao
* @date: 2023/1/12
*/
@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
/**
* @name: BrandController
* @author: chenghao
* @date: 2023/1/12
*/
@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