乐优商城sql,表tb_spec_param,tb_spec_group和tb_spu_detail,和对应实体类
视频缺失的表
表的结构和实体类是暂停视频,跟着敲出来的,数据则是我根据表tb_specification的specifications
,创建一个对应实体类,然后用java代码生成的,除了tb_spec_group和tb_spec_param之外,我还对tb_spu_detail,扩充了generic
通用规格参数和special
这两个字段,然后通过tb_spec_group和tb_spec_param这两张表,对这两个新字段数据进行填充,这些后面做搜索都可以用到
1.SpecGroup
@Data
@Table(name = "tb_spec_group")
public class SpecGroup {
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
private Long cid;
private String name;
}
2.SpecParam
@Data
@Table(name = "tb_spec_param")
public class SpecParam {
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
private Long cid;
private Long groupId;
private String name;
@Column(name = "`numeric`")
private Boolean numeric;
private String unit;
private Boolean generic;
private Boolean searching;
private String segments;
}
3.sql文件
链接:https://pan.baidu.com/s/1wJpvFC8oNjU7-P7DrxXDTg
提取码:cqbp
复制这段内容后打开百度网盘手机App,操作更方便哦
对tb_spec_group和tb_spec_param转化过程
1…随机选取表tb_specification的specifications
的一个数据,通过观察可以知道这个是一个List集合,集合装着的对应的类型对象
2.数据对应的类
@Data
public class JsonObject {
private String group;//组名
private List<Param> params;//param数组
private Boolean empty;//未知,原来没加,然后空指针异常,在某条数据上发现的
}
@Data
class Param {
private String k;//param名
private Boolean searchable;//可否搜索
private Boolean global;//是否通用属性参数
private Boolean numerical;//是否是数字类型参数
private String unit;//数字类型参数的单位,非数字类型可以为空
private String v;//param值,后面用到
private List<String> options;//待选项集合
}
3.表tb_spec_group添加数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpecGroupMapperTest {
@Autowired
private SpecificationMapper specificationMapper;
@Autowired
private SpecGroupMapper specGroupMapper;
@Test
public void test(){
//从表‘tb_specification’获取规格参数集合
List<Specification> specifications = specificationMapper.selectAll();
//遍历规格参数集合
for (Specification specification : specifications) {
//获取具体规格参数
String spec = specification.getSpec();
//将规格参数转为对象
List<JsonObject> jsonObjects = JsonUtils.parseList(spec, JsonObject.class);
for (JsonObject jsonObject : jsonObjects) {
String group = jsonObject.getGroup();
SpecGroup specGroup = new SpecGroup();
specGroup.setId(null);
specGroup.setCid(specification.getCategoryId());
specGroup.setName(group);
int insert = specGroupMapper.insert(specGroup);
System.out.println("insert = " + insert);
}
}
}
}
结果:
4.表tb_spec_param添加数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpecparamMapperTest {
@Autowired
private SpecParamMapper specParamMapper;
@Autowired
private SpecGroupMapper specGroupMapper;
@Autowired
private SpecificationMapper specificationMapper;
@Test
public void test(){
//从表‘tb_specification’获取规格参数集合
List<Specification> specifications = specificationMapper.selectAll();
int count = 0;
//遍历规格参数集合
for (Specification specification : specifications) {
//获取具体规格参数
String spec = specification.getSpec();
//将规格参数转为对象
List<JsonObject> jsonObjects = JsonUtils.parseList(spec, JsonObject.class);
for (JsonObject jsonObject : jsonObjects) {
String group = jsonObject.getGroup();
SpecGroup specGroup = new SpecGroup();
specGroup.setName(group);
//获取规格参数group
List<SpecGroup> groupList = specGroupMapper.select(specGroup);
//组id
Long groupId = null;
//因为specGroup有两个相同组名,需要进行判断
if (groupList.size() == 1){
groupId = groupList.get(0).getId();
}else {
if (count == 0){
count++;
groupId = groupList.get(0).getId();
}else {
groupId = groupList.get(1).getId();
}
}
//遍历规格参数params
for (Param param : jsonObject.getParams()) {
SpecParam specParam = new SpecParam();
specParam.setId(null);
specParam.setCid(specification.getCategoryId());
specParam.setGroupId(groupId);
specParam.setName(param.getK());
//有些数据是空的需要先过滤一下
specParam.setGeneric(param.getGlobal() == null ? false : param.getGlobal());
specParam.setNumeric(param.getNumerical() == null ? false : param.getNumerical());
specParam.setSearching(param.getSearchable());
specParam.setUnit(param.getUnit() == null ? "" : param.getUnit());
//表‘tb_specification’中没有这个数据,先设置为空字符串,后面再手动设置
specParam.setSegments("");
int insert = specParamMapper.insert(specParam);
System.out.println("insert = " + insert);
}
}
}
}
}
结果(Segments这个字段是根据视频 手动添加的):
最后
因为是第一次写博客,而且数据是通过转换得来的,可能会有些问题(目前暂时没遇到),还有一个表tb_spu_detail新增字段数据的添加过程,这次没写,下次有时间再补充,如果有什么问题的话,欢迎大家随时指正,谢谢!