考察:Mybatis-plus 使用 CRUD
操作数据为:tb_item表
1.商品表结构如下:
CREATE TABLE tb_item
(
id
varchar(20) NOT NULL COMMENT ‘商品id’,
name
varchar(200) NOT NULL COMMENT ‘SKU名称’,
price
bigint(20) NOT NULL COMMENT ‘价格(分)’,
stock
int(10) NOT NULL COMMENT ‘库存数量’,
image
varchar(200) DEFAULT NULL COMMENT ‘商品图片’,
category
varchar(200) DEFAULT NULL COMMENT ‘类目名称’,
brand
varchar(100) DEFAULT NULL COMMENT ‘品牌名称’,
spec
varchar(200) DEFAULT NULL COMMENT ‘规格’,
sold
int(11) DEFAULT ‘0’ COMMENT ‘销量’,
comment_count
int(11) DEFAULT ‘0’ COMMENT ‘评论数’,
isAD
tinyint(1) DEFAULT ‘0’ COMMENT ‘是否是推广广告,true/false’,
status
int(1) DEFAULT ‘1’ COMMENT ‘商品状态 1-正常,2-下架,3-删除’,
create_time
datetime DEFAULT NULL COMMENT ‘创建时间’,
update_time
datetime DEFAULT NULL COMMENT ‘更新时间’,
PRIMARY KEY (id
),
KEY status
(status
),
KEY updated
(update_time
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=’商品表’;
2.item-service中商品的实体类如下:
package com.hmall.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;
import lombok.Data;
import java.util.Date;
@Data
@TableName(“tb_item”)
public class Item {
@TableId(type = IdType.AUTO)
private Long id;//商品id
private String name;//商品名称
private Long price;//价格(分)
private Integer stock;//库存数量
private String image;//商品图片
private String category;//分类名称
private String brand;//品牌名称
private String spec;//规格
private Integer sold;//销量
private Integer commentCount;//评论数
private Integer status;//商品状态 1-正常,2-下架
@TableField(“isAD”)
private Boolean isAD;//商品状态 1-正常,2-下架
private Date createTime;//创建时间
private Date updateTime;//更新时间
}
3.在feing-api中也提供了Item,提供给其它微服务,用来接收商品信息
package com.hmall.common.dto;
import lombok.Data;
@Data
public class Item {
private Long id;//商品id
private String name;//商品名称
private Long price;//价格(分)
private Integer stock;//库存数量
private String image;//商品图片
private String category;//分类名称
private String brand;//品牌名称
private String spec;//规格
private Integer sold;//销量
private Integer commentCount;//评论数
private Integer status;//商品状态 1-正常,2-下架
private Boolean isAD;// 是否是广告
}
4.1需求与代码
返回值是分页结果,已经在feign-api项目中定义了:
package com.hmall.common.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/
分页结果对象
@param
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageDTO
/
总条数
/
private Long total;
/*
当前页数据
*/
private List
}
导入依赖: pom.xml
编写配置文件 : application.xml
server:
port: 8081
spring:
application:
name: itemservice
datasource:
url: jdbc:mysql://192.168.94.129:3306/hmall?useSSL=false
username: root
password: itcast142
driver-class-name: com.mysql.jdbc.Driver
cloud:
nacos:
server-addr: 192.168.94.129:8848 _# nacos地址
_rabbitmq:
host: 192.168.150.101
port: 5672
virtual-host: /
username: itcast
password: itcast142
mybatis-plus:
type-aliases-package: com.hmall.item.pojo
configuration:
map-underscore-to-camel-case: true
global-config:
db-config:
update-strategy: _not_null
_id-type: _auto
_logging:
level:
com.hmall: debug
pattern:
dateformat: HH:mm:ss:SSS
Controller层基础代码:
分页查询商品
ItemController代码:
@GetMapping(“/list”)
public PageDTO
// 分页查询
_Page
_return new PageDTO<>(result.getTotal(), result.getRecords());
}
Controller代码:
@GetMapping(“{id}”)
public Item queryItemById(@PathVariable(“id”) Long id) {
return itemService.getById(id);
}
Controller代码:
@PostMapping
public void saveItem(@RequestBody Item item) {
// 基本数据
_item.setCreateTime(new Date());
item.setUpdateTime(new Date());
item.setStatus(2);
// 新增
_itemService.save(item);
}
Controller代码:
@PutMapping(“/status/{id}/{status}”)
public void updateItemStatus(@PathVariable(“id”) Long id, @PathVariable(“status”) Integer status){
itemService.updateStatus(id, status);
}
Controller代码:
@PutMapping
public void updateItem(@RequestBody Item item) {
// 基本数据
_item.setUpdateTime(new Date());
// 不允许修改商品状态,所以强制设置为null,更新时,就会忽略该字段
item.setStatus(null);
// 更新
_itemService.updateById(item);
}
Controller 代码
@DeleteMapping(“{id}”)
public void deleteItemById(@PathVariable(“id”) Long id) {
itemService.removeById(id);
}