
逆向工程插件指的是: mybatis code helper 等一类插件, 他的作用是, 可以根据写好的数据库表自动生成实体类, swagger以及mapper, service等文件, 适应快速迭代开发. 正向工程, 如: SpringData 等则是根据写好的实体类自动生成对应的数据库表. 同样也是适应快速迭代开发的需要. 二者各有千秋, 我们只需要根据自己的需求去合理的使用这些插件 / 工具, 利用其帮助我们快速达到快速开发的需要.



连接成功后, 如下图所示


b. 配置service接口

b. 配置自动生成的impl类

点击右下角ok, 自动生成即可
在实际应用中, 这些增删改查的逻辑是需要我们根据需求去编写的, 因此需要我们对自动生成的内容进行修改
/**
* 这里需要去除继承IService接口
* 在这里定义持久化接口
*/
public interface DictDataService{
/**
* 分页查询字典数据类型
*
* @param dictDataDto
* @return
*/
DataGridView listPage(DictDataDto dictDataDto);
/**
* 插入新的字典类型
*
* @param dictDataDto
* @return
*/
int insert(DictDataDto dictDataDto);
/**
* 修改的字典类型
*
* @param dictDataDto
* @return
*/
int update(DictDataDto dictDataDto);
/**
* 根据ID删除字典类型
*
* @param dictCodeIds
* @return
*/
int deleteDictDataByIds(Long[] dictCodeIds);
/**
* 根据字典类型查询字典数据
*
* @param dictType
* @return
*/
List<DictData> selectDictDataByDictType(String dictType);
/**
* 根据ID查询一个字典类型
*
* @param dictCode
* @return
*/
DictData selectDictDataById(Long dictCode);
}注意: 这里使用的mybatisplus, mapper.xml无需更改, 简单的增删改查已经为我们自动生成, 如下代码
public interface BaseMapper<T> extends Mapper<T> {
int insert(T entity);
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> wrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}QueryWrapper 对查询条件的封装, 相当于where子句.
update操作中, 利用hutool的BeanUtil, 将dto的属性复制到po上进行更新
deleteDictDataByIds操作中, 批量删除的写法package com.hrt.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrt.contants.Contants;
import com.hrt.domain.DictData;
import com.hrt.dto.DictDataDto;
import com.hrt.mapper.DictDataMapper;
import com.hrt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hrt.service.DictDataService;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
public class DictDataServiceImpl implements DictDataService{
@Autowired
private DictDataMapper dictDataMapper;
@Override
public DataGridView listPage(DictDataDto dictDataDto) {
//开启分页操作
Page<DictData> page = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
//封装查询条件, 根据前端页面的复杂查询条件实现 字典类型, 字典状态, 字典标识
QueryWrapper<DictData> qw = new QueryWrapper<>();
//这里使用的是带判断条件的封装 相当于mybatis 的<if test="taskName !=null and taskName !='' ">and...</if>
qw.eq(StringUtils.isNotBlank(dictDataDto.getDictType()),DictData.COL_DICT_TYPE, dictDataDto.getDictType());
qw.eq(StringUtils.isNoneBlank(dictDataDto.getStatus()),DictData.COL_STATUS, dictDataDto.getStatus());
qw.like(StringUtils.isNotBlank(dictDataDto.getDictLabel()),DictData.COL_DICT_LABEL, dictDataDto.getDictLabel());
//Inferred type 'E' for type parameter 'E' is not within its bound; should implement 'com.baomidou.mybatisplus.core.metadata.IPage<DictData>'
//原因: mapper继承了错误路径的BaseMapper<DictData>
this.dictDataMapper.selectPage(page,qw);
return new DataGridView(page.getTotal(),page.getRecords());
}
@Override
public int insert(DictDataDto dictDataDto) {
//这里采用的是复制, 因为dto的属性没有po全, 直接插入会带来空值问题
DictData dictData = new DictData();
BeanUtil.copyProperties(dictDataDto,dictData);
//设置创建时间
dictData.setCreateTime(new Date());
//设置创建者 dictDataDto继承了 BaseDto, 因此可以使用BaseDto的属性
dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
return this.dictDataMapper.insert(dictData);
}
@Override
public int update(DictDataDto dictDataDto) {
//复制更新同上
DictData dictData = new DictData();
BeanUtil.copyProperties(dictDataDto,dictData);
//设置修改人
dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
return this.dictDataMapper.updateById(dictData);
}
@Override
public int deleteDictDataByIds(Long[] dictCodeIds) {
//将数组转化成list
List<Long> longList = Arrays.asList(dictCodeIds);
if (CollectionUtil.isEmpty(longList)) {
return -1;
} else {
return this.dictDataMapper.deleteBatchIds(longList);
}
}
@Override
public List<DictData> selectDictDataByDictType(String dictType) {
QueryWrapper<DictData> qw = new QueryWrapper<>();
qw.eq(DictData.COL_DICT_TYPE, dictType);
qw.eq(DictData.COL_STATUS, Contants.STATUS_TRUE);//可用
return this.dictDataMapper.selectList(qw);
}
@Override
public DictData selectDictDataById(Long dictCode) {
return this.dictDataMapper.selectById(dictCode);
}
}package com.hrt.vo;
import com.hrt.contants.HttpStatus;
import java.util.HashMap;
/**
* 通用返回对象类
*
* @author caohaiyang
* @create 2021-07-09 下午 05:23
*/
public class AjaxResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
/**
* 状态码
*/
public static final String CODE_TAG = "code";
/**
* 返回内容
*/
public static final String MSG_TAG = "msg";
/**
* 数据对象
*/
public static final String DATA_TAG = "data";
/**
* 数据总条数
*/
public static final String DATA_TOTAL = "total";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult() {
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public AjaxResult(int code, String msg, Object data) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
super.put(DATA_TAG, data);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
* @param total 数据总条数
*/
public AjaxResult(int code, String msg, Object data, Long total) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
super.put(DATA_TAG, data);
super.put(DATA_TOTAL, total);
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success() {
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data) {
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg) {
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data) {
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data,Long total) {
return new AjaxResult(HttpStatus.SUCCESS, msg, data,total);
}
/**
* 返回失败消息
*/
public static AjaxResult fail() {
return AjaxResult.fail("操作失败");
}
/**
* 返回失败消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult fail(String msg) {
return AjaxResult.fail(msg, null);
}
/**
* 返回失败消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult fail(String msg, Object data) {
return new AjaxResult(HttpStatus.BAD_REQUEST, msg, data);
}
/**
* 返回错误消息
*/
public static AjaxResult error() {
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg) {
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data) {
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg) {
return new AjaxResult(code, msg, null);
}
/**
* 返回错误消息
*
* @param rows 状态码
* @return 添加修改删除转化信息
*/
public static AjaxResult toAjax(int rows) {
return rows > 0 ? AjaxResult.success() : AjaxResult.fail();
}
}@RestController
@RequestMapping("system/dict/data")
public class DictDataController {
@Autowired
private DictDataService dictDataService;
/**
* 分页查询
*
* @param dictDataDto 字典数据dto
* @return
*/
@GetMapping("listForPage")
public AjaxResult listForPage(DictDataDto dictDataDto) {
DataGridView gridView = this.dictDataService.listPage(dictDataDto);
return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
}
/**
* 新增字典数据
*
* @param dictDataDto 字典数据dto,新增时需要校验属性
* @return
*/
@PostMapping("addDictData")
public AjaxResult addDictData(@Validated DictDataDto dictDataDto) {
dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
int insert = this.dictDataService.insert(dictDataDto);
return AjaxResult.success(insert);
}
@PutMapping("updateDictData")
public AjaxResult updateDictData(@Validated DictDataDto dictDataDto) {
dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
return AjaxResult.success(this.dictDataService.update(dictDataDto));
}
/**
* 根据 ID 查询一个字典信息
*
* @param dictCode 字典id
* @return
*/
@GetMapping("getOne/{dictCode}")
public AjaxResult getDictData(@PathVariable @Validated @NotNull(message = "字典ID不能为空") Long dictCode) {
return AjaxResult.success(this.dictDataService.selectDictDataById(dictCode));
}
/**
* 批量删除
*
* @param dictCodeIds
* @return
*/
@DeleteMapping("deleteDictDataByIds/{dictCodeIds}")
public AjaxResult delectDictData(@PathVariable @Validated @NotEmpty(message = "要删除的 ID 不 能为空") Long[] dictCodeIds) {
return AjaxResult.toAjax(this.dictDataService.deleteDictDataByIds(dictCodeIds));
}
/**
* 查询所有可用的字典类型
* @param dictType 字典类型
* @return
*/
@GetMapping("getDataByType/{dictType}")
public AjaxResult getDataByType(@PathVariable @Validated @NotEmpty(message = "字典类型不能为空") String dictType) {
return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
}
}