1 建表语句
create table product
(
product_id int auto_increment
primary key,
product_name varchar(255) null comment '产品名称',
product_type int null comment '产品类型'
);
2 pojo类
import cn.huawei.enum11.ChanpinType;
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 java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "product")
public class Product implements Serializable {
@TableId(value = "product_id", type = IdType.AUTO)
private Integer productId;
@TableField(value = "product_name")
private String productName;
/**
* 产品类型 用的枚举类型
*/
@TableField(value = "product_type")
private ChanpinType productType;
private static final long serialVersionUID = 1L;
}
3 枚举代码
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
@Getter
public enum ChanpinType {
// 产品类型
TUSHU(10, "图书"),
JIADIAN(15, "家电"),
FUZHUANG(20, "服装");
/**
* 在需要保存到 数据库 的值上面加上注解
*/
@EnumValue
private Integer code;
/**
* 需要在前端展示哪个值就在哪个属性上加上该注解
*/
//@JsonValue
private String name;
ChanpinType(Integer code, String name) {
this.code = code;
this.name = name;
}
}
4 yml文件修改
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
type-enums-package: 改成枚举的包名
说明:本demo使用的是mybatis-plus版本是3.4.0