mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
[重磅更新] 重写项目整体结构 数据处理下沉至 Mapper 符合 MVC 规范 减少循环依赖
This commit is contained in:
@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
import com.ruoyi.demo.service.ITestDemoService;
|
||||
import com.ruoyi.demo.mapper.TestDemoMapper;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -29,7 +29,10 @@ import java.util.List;
|
||||
@RequestMapping("/demo/batch")
|
||||
public class TestBatchController extends BaseController {
|
||||
|
||||
private final ITestDemoService iTestDemoService;
|
||||
/**
|
||||
* 为了便于测试 直接引入mapper
|
||||
*/
|
||||
private final TestDemoMapper testDemoMapper;
|
||||
|
||||
/**
|
||||
* 新增批量方法 可完美替代 saveBatch 秒级插入上万数据 (对mysql负荷较大)
|
||||
@ -44,7 +47,7 @@ public class TestBatchController extends BaseController {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
|
||||
}
|
||||
return toAjax(iTestDemoService.saveAll(list) ? 1 : 0);
|
||||
return toAjax(testDemoMapper.insertBatch(list) ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,7 +63,7 @@ public class TestBatchController extends BaseController {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
|
||||
}
|
||||
iTestDemoService.saveAll(list);
|
||||
testDemoMapper.insertBatch(list);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
TestDemo testDemo = list.get(i);
|
||||
testDemo.setTestKey("批量新增或修改").setValue("批量新增或修改");
|
||||
@ -68,7 +71,7 @@ public class TestBatchController extends BaseController {
|
||||
testDemo.setId(null);
|
||||
}
|
||||
}
|
||||
return toAjax(iTestDemoService.saveOrUpdateAll(list) ? 1 : 0);
|
||||
return toAjax(testDemoMapper.insertOrUpdateBatch(list) ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,8 +81,8 @@ public class TestBatchController extends BaseController {
|
||||
@DeleteMapping()
|
||||
// @DS("slave")
|
||||
public AjaxResult<Void> remove() {
|
||||
return toAjax(iTestDemoService.remove(new LambdaQueryWrapper<TestDemo>()
|
||||
.eq(TestDemo::getOrderNum, -1L)) ? 1 : 0);
|
||||
return toAjax(testDemoMapper.delete(new LambdaQueryWrapper<TestDemo>()
|
||||
.eq(TestDemo::getOrderNum, -1L)));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ public class TestDemoController extends BaseController {
|
||||
ExcelResult<TestDemoImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), TestDemoImportVo.class, true);
|
||||
List<TestDemoImportVo> volist = excelResult.getList();
|
||||
List<TestDemo> list = BeanUtil.copyToList(volist, TestDemo.class);
|
||||
iTestDemoService.saveAll(list);
|
||||
iTestDemoService.saveBatch(list);
|
||||
return AjaxResult.success(excelResult.getAnalysis());
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import com.ruoyi.common.core.mapper.BaseMapperPlus;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
import com.ruoyi.demo.domain.vo.TestDemoVo;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@ -20,7 +20,7 @@ import java.util.List;
|
||||
* @author Lion Li
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
public interface TestDemoMapper extends BaseMapperPlus<TestDemo> {
|
||||
public interface TestDemoMapper extends BaseMapperPlus<TestDemoMapper, TestDemo, TestDemoVo> {
|
||||
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "dept_id"),
|
||||
|
@ -2,8 +2,9 @@ package com.ruoyi.demo.mapper;
|
||||
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import com.ruoyi.common.core.mapper.BaseMapperPlus;
|
||||
import com.ruoyi.demo.domain.TestTree;
|
||||
import com.ruoyi.demo.domain.vo.TestTreeVo;
|
||||
|
||||
/**
|
||||
* 测试树表Mapper接口
|
||||
@ -15,6 +16,6 @@ import com.ruoyi.demo.domain.TestTree;
|
||||
@DataColumn(key = "deptName", value = "dept_id"),
|
||||
@DataColumn(key = "userName", value = "user_id")
|
||||
})
|
||||
public interface TestTreeMapper extends BaseMapperPlus<TestTree> {
|
||||
public interface TestTreeMapper extends BaseMapperPlus<TestTreeMapper, TestTree, TestTreeVo> {
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.ruoyi.demo.service;
|
||||
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
import com.ruoyi.common.core.mybatisplus.core.IServicePlus;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
import com.ruoyi.demo.domain.bo.TestDemoBo;
|
||||
@ -16,7 +15,7 @@ import java.util.List;
|
||||
* @author Lion Li
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
public interface ITestDemoService extends IServicePlus<TestDemo, TestDemoVo> {
|
||||
public interface ITestDemoService {
|
||||
|
||||
/**
|
||||
* 查询单个
|
||||
@ -64,4 +63,9 @@ public interface ITestDemoService extends IServicePlus<TestDemo, TestDemoVo> {
|
||||
* @return
|
||||
*/
|
||||
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
||||
|
||||
/**
|
||||
* 批量保存
|
||||
*/
|
||||
Boolean saveBatch(List<TestDemo> list);
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.ruoyi.demo.service;
|
||||
|
||||
import com.ruoyi.common.core.mybatisplus.core.IServicePlus;
|
||||
import com.ruoyi.demo.domain.TestTree;
|
||||
import com.ruoyi.demo.domain.bo.TestTreeBo;
|
||||
import com.ruoyi.demo.domain.vo.TestTreeVo;
|
||||
|
||||
@ -14,7 +12,7 @@ import java.util.List;
|
||||
* @author Lion Li
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
public interface ITestTreeService extends IServicePlus<TestTree, TestTreeVo> {
|
||||
public interface ITestTreeService {
|
||||
/**
|
||||
* 查询单个
|
||||
*
|
||||
|
@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
@ -13,6 +12,7 @@ import com.ruoyi.demo.domain.bo.TestDemoBo;
|
||||
import com.ruoyi.demo.domain.vo.TestDemoVo;
|
||||
import com.ruoyi.demo.mapper.TestDemoMapper;
|
||||
import com.ruoyi.demo.service.ITestDemoService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -25,18 +25,21 @@ import java.util.Map;
|
||||
* @author Lion Li
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDemo, TestDemoVo> implements ITestDemoService {
|
||||
public class TestDemoServiceImpl implements ITestDemoService {
|
||||
|
||||
private final TestDemoMapper baseMapper;
|
||||
|
||||
@Override
|
||||
public TestDemoVo queryById(Long id) {
|
||||
return getVoById(id);
|
||||
return baseMapper.selectVoById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDataInfo<TestDemoVo> queryPageList(TestDemoBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<TestDemo> lqw = buildQueryWrapper(bo);
|
||||
Page<TestDemoVo> result = pageVo(pageQuery.build(), lqw);
|
||||
Page<TestDemoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
return TableDataInfo.build(result);
|
||||
}
|
||||
|
||||
@ -52,7 +55,7 @@ public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDem
|
||||
|
||||
@Override
|
||||
public List<TestDemoVo> queryList(TestDemoBo bo) {
|
||||
return listVo(buildQueryWrapper(bo));
|
||||
return baseMapper.selectVoList(buildQueryWrapper(bo));
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<TestDemo> buildQueryWrapper(TestDemoBo bo) {
|
||||
@ -69,7 +72,7 @@ public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDem
|
||||
public Boolean insertByBo(TestDemoBo bo) {
|
||||
TestDemo add = BeanUtil.toBean(bo, TestDemo.class);
|
||||
validEntityBeforeSave(add);
|
||||
boolean flag = save(add);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (flag) {
|
||||
bo.setId(add.getId());
|
||||
}
|
||||
@ -80,7 +83,7 @@ public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDem
|
||||
public Boolean updateByBo(TestDemoBo bo) {
|
||||
TestDemo update = BeanUtil.toBean(bo, TestDemo.class);
|
||||
validEntityBeforeSave(update);
|
||||
return updateById(update);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,6 +100,11 @@ public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDem
|
||||
if (isValid) {
|
||||
//TODO 做一些业务上的校验,判断是否需要校验
|
||||
}
|
||||
return removeByIds(ids);
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean saveBatch(List<TestDemo> list) {
|
||||
return baseMapper.insertBatch(list);
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,13 @@ package com.ruoyi.demo.service.impl;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.demo.domain.TestTree;
|
||||
import com.ruoyi.demo.domain.bo.TestTreeBo;
|
||||
import com.ruoyi.demo.domain.vo.TestTreeVo;
|
||||
import com.ruoyi.demo.mapper.TestTreeMapper;
|
||||
import com.ruoyi.demo.service.ITestTreeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -23,19 +23,22 @@ import java.util.Map;
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
// @DS("slave") // 切换从库查询
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class TestTreeServiceImpl extends ServicePlusImpl<TestTreeMapper, TestTree, TestTreeVo> implements ITestTreeService {
|
||||
public class TestTreeServiceImpl implements ITestTreeService {
|
||||
|
||||
private final TestTreeMapper baseMapper;
|
||||
|
||||
@Override
|
||||
public TestTreeVo queryById(Long id) {
|
||||
return getVoById(id);
|
||||
return baseMapper.selectVoById(id);
|
||||
}
|
||||
|
||||
// @DS("slave") // 切换从库查询
|
||||
@Override
|
||||
public List<TestTreeVo> queryList(TestTreeBo bo) {
|
||||
LambdaQueryWrapper<TestTree> lqw = buildQueryWrapper(bo);
|
||||
return listVo(lqw);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<TestTree> buildQueryWrapper(TestTreeBo bo) {
|
||||
@ -51,7 +54,7 @@ public class TestTreeServiceImpl extends ServicePlusImpl<TestTreeMapper, TestTre
|
||||
public Boolean insertByBo(TestTreeBo bo) {
|
||||
TestTree add = BeanUtil.toBean(bo, TestTree.class);
|
||||
validEntityBeforeSave(add);
|
||||
boolean flag = save(add);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (flag) {
|
||||
bo.setId(add.getId());
|
||||
}
|
||||
@ -62,7 +65,7 @@ public class TestTreeServiceImpl extends ServicePlusImpl<TestTreeMapper, TestTre
|
||||
public Boolean updateByBo(TestTreeBo bo) {
|
||||
TestTree update = BeanUtil.toBean(bo, TestTree.class);
|
||||
validEntityBeforeSave(update);
|
||||
return updateById(update);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,6 +82,6 @@ public class TestTreeServiceImpl extends ServicePlusImpl<TestTreeMapper, TestTre
|
||||
if (isValid) {
|
||||
//TODO 做一些业务上的校验,判断是否需要校验
|
||||
}
|
||||
return removeByIds(ids);
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user