文章目录
前言
一些java项目中常见的逻辑的具体操作
一、前端调用新增接口新增一个实体类数据,但有些实体类表中没有的参数需要保存到关系表中
1 VO简介
VO viewobject 就是接受页面传来的数据 封装对象
或者将业务处理完成的对象封装成页面要用的数据
2 问题所在
我们前端在新增规格参数时还没有把传来的groupid
传到属性 分组 realation表当中
现在我们来进行代码编写
我们首先来到attr实体类
发现里面没有Groupid字段
如果新增一个字段并加上 @TableField(exist = false)
会和数据库不对应 并且很不规范
3解决方案
创建一个attrVo来接受前端传来数据
其中新建一个属性
因为其不和数据库绑定 所以不用加很多数据库注解
package com.atguigu.gulimall.product.vo;
import lombok.Data;
@Data
public class AttrVo {
/**
* 属性id
*/
private Long attrId;
/**
* 属性名
*/
private String attrName;
/**
* 是否需要检索[0-不需要,1-需要]
*/
private Integer searchType;
/**
* 值类型[0-为单个值,1-可以选择多个值]
*/
private Integer valueType;
/**
* 属性图标
*/
private String icon;
/**
* 可选值列表[用逗号分隔]
*/
private String valueSelect;
/**
* 属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]
*/
private Integer attrType;
/**
* 启用状态[0 - 禁用,1 - 启用]
*/
private Long enable;
/**
* 所属分类
*/
private Long catelogId;
/**
* 快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整
*/
private Integer showDesc;
private Long attrGroupId;
}
我们的attr实体类属性就和数据库进行对应 不再进行更改
1 controller编写接受vo
/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:attr:save")
public R save(@RequestBody AttrVo attr){
attrService.saveAttr(attr);
return R.ok();
}
2 service
package com.atguigu.gulimall.product.service;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.gulimall.product.entity.AttrEntity;
import java.util.Map;
/**
* 商品属性
*
* @author yyl
* @email sunlightcs@gmail.com
* @date 2022-04-24 19:01:01
*/
public interface AttrService extends IService<AttrEntity> {
PageUtils queryPage(Map<String, Object> params);
void saveAttr(AttrVo attr);
}
2 serviceimpl
@Transactional
@Override
public void saveAttr(AttrVo attr) {
//创建一个dao实体类
AttrEntity attrEntity = new AttrEntity();
// attrEntity.setAttrName(attr.getAttrName());
//把传来的基本数据传给dao实体类
BeanUtils.copyProperties(attr,attrEntity);
//1、保存基本数据
this.save(attrEntity);
//2、保存关联关系
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
最终测试
发现关系增加到了表中
二 后端返回数据过多 VO只需其中几个字段 这时候使用lambda表达式处理这些List 封装成VO返回给前端
1 示例如下
@RequestMapping("/1")
public R smallVo(){
List<TestEntity> list = testService.list();
//通过Lambda表达式stream流做减法
//筛选出我们需要的数据并最终封装VO返回
List<TestVo> collect = list.stream().map(item -> {
TestVo testVo = new TestVo();
testVo.setName(item.getName());
return testVo;
}).collect(Collectors.toList());
return R.ok().put("data",collect);
}
三 VO所需的数据在一个表中没有全部有 需要进行连表查询(使用Mabatis-plus + lambda表达式 stream流处理集合 并且使用Mybatis-plus分页操作)
1 导入分页配置文件
package com.example.cat.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement //开启事务
@MapperScan("com.example.cat.dao")
public class MybatisPlusConfig {
//引入分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(true);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInterceptor.setLimit(1000);
return paginationInterceptor;
}
}
2 Controller层
/**
* 拼装前端所需要的VO数据
* @return
*/
@RequestMapping("/2/{userId}")
public R selectVo(@RequestParam Map<String, Object> params,
@PathVariable(value = "userId") Integer userId){
PageUtils page = testService.queryBaseTestPage(params,userId);
return R.ok().put("page", page);
}
3 Service层
package com.example.cat.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.cat.common.utils.PageUtils;
import com.example.cat.entity.TestEntity;
import java.util.Map;
public interface TestService extends IService<TestEntity> {
PageUtils queryBaseTestPage(Map<String, Object> params, Integer userId);
}
4 ServiceIMpl层
@Service
public class TestServiceImpl extends ServiceImpl<TestDao, TestEntity> implements TestService {
@Autowired
CourseDao courseDao;
@Autowired
StudentCourseRelationDao relationDao;
@Override
public PageUtils queryBaseTestPage(Map<String, Object> params, Integer userId) {
//1 创建实体类对应的queryWrapper
QueryWrapper<TestEntity> queryWrapper = new QueryWrapper<>();
//2 如果传入的id不为0 我们才限制id对应的实体类
if (userId != 0){
queryWrapper.eq("id",userId);
}
//3 得到params参数列表中的Key 如果不为空 进行规则匹配(返回name lIke Key或者是email like key)
String key = (String) params.get("key");
if(!StringUtils.isEmpty(key)){
//attr_id attr_name
queryWrapper.and((Wrapper)->{
Wrapper.like("name",key).or().like("email",key);
});
}
//4 调用mybatis-plus serviceImpl层的page方法返回Ipage对象
// 其参数是根据Query工具类生成的Page对象 和 queryWrapper
//把查询到的实体类封装进pageUtils里
IPage<TestEntity> page = this.page(new Query<TestEntity>().getPage(params), queryWrapper);
PageUtils pageUtils = new PageUtils(page);
List<TestEntity> records = page.getRecords();
List<TestRespVo> collect = records.stream().map((item) -> {
TestRespVo testRespVo = new TestRespVo();
StudentCourseRelationEntity relationEntity = relationDao.selectOne(
new QueryWrapper<StudentCourseRelationEntity>().eq("user_id", item.getId())
);
if (relationEntity!=null){
CourseEntity courseEntity = courseDao.selectOne(
new QueryWrapper<CourseEntity>().eq("cid", relationEntity.getCid())
);
BeanUtils.copyProperties(item, testRespVo);
testRespVo.setCname(courseEntity.getName());
return testRespVo;
}else {
BeanUtils.copyProperties(item, testRespVo);
testRespVo.setCname("暂无对应的课程信息");
return testRespVo;
}
}).collect(Collectors.toList());
pageUtils.setList(collect);
return pageUtils;
}
}