- 前言
- 一、前端调用新增接口新增一个实体类数据,但有些实体类表中没有的参数需要保存到关系表中
- 1 VO简介
- 2 问题所在
- 3解决方案
- 二 后端返回数据过多 VO只需其中几个字段 这时候使用lambda表达式处理这些List 封装成VO返回给前端
- 1 示例如下
- 三 VO所需的数据在一个表中没有全部有 需要进行连表查询(使用Mabatis-plus + lambda表达式 stream流处理集合 并且使用Mybatis-plus分页操作)
- 1 导入分页配置文件
- 2 Controller层
- 3 Service层
- 4 ServiceIMpl层
前言
一些java项目中常见的逻辑的具体操作
一、前端调用新增接口新增一个实体类数据,但有些实体类表中没有的参数需要保存到关系表中 1 VO简介
VO viewobject 就是接受页面传来的数据 封装对象
或者将业务处理完成的对象封装成页面要用的数据
我们前端在新增规格参数时还没有把传来的groupid
传到属性 分组 realation表当中
现在我们来进行代码编写
我们首先来到attr实体类
发现里面没有Groupid字段
如果新增一个字段并加上 @TableField(exist = false)
会和数据库不对应 并且很不规范
创建一个attrVo来接受前端传来数据
其中新建一个属性
因为其不和数据库绑定 所以不用加很多数据库注解
package com.atguigu.gulimall.product.vo;
import lombok.Data;
@Data
public class AttrVo {
private Long attrId;
private String attrName;
private Integer searchType;
private Integer valueType;
private String icon;
private String valueSelect;
private Integer attrType;
private Long enable;
private Long catelogId;
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; public interface AttrService extends IService{ PageUtils queryPage(Map 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);
}
最终测试
发现关系增加到了表中
@RequestMapping("/1")
public R smallVo(){
List list = testService.list();
//通过Lambda表达式stream流做减法
//筛选出我们需要的数据并最终封装VO返回
List 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层
@RequestMapping("/2/{userId}")
public R selectVo(@RequestParam Map 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 IService4 ServiceIMpl层{ PageUtils queryBaseTestPage(Map params, Integer userId); }
@Service public class TestServiceImpl extends ServiceImplimplements TestService { @Autowired CourseDao courseDao; @Autowired StudentCourseRelationDao relationDao; @Override public PageUtils queryBaseTestPage(Map params, Integer userId) { //1 创建实体类对应的queryWrapper QueryWrapper 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 page = this.page(new Query ().getPage(params), queryWrapper); PageUtils pageUtils = new PageUtils(page); List records = page.getRecords(); List collect = records.stream().map((item) -> { TestRespVo testRespVo = new TestRespVo(); StudentCourseRelationEntity relationEntity = relationDao.selectOne( new QueryWrapper ().eq("user_id", item.getId()) ); if (relationEntity!=null){ CourseEntity courseEntity = courseDao.selectOne( new QueryWrapper ().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; } }



