EasyCode使用指南—MybatisPlus模板

2024-01-27 15:12

本文主要是介绍EasyCode使用指南—MybatisPlus模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 1、下载EasyCode插件
  • 2、配置EasyCode
    • 2.1、配置作者名称
    • 2.2、配置代码内容生成模板(模板内容见文末)
    • 2.3、连接数据库
    • 2.4、使用代码内容生成模板
  • 模板内容:
    • controller.java.vm
    • dto.java.vm
    • mapper.java.vm
    • mapper.xml.vm
    • pojo.java.vm
    • service.java.vm
    • serviceImpl.java.vm

1、下载EasyCode插件

在这里插入图片描述

2、配置EasyCode

2.1、配置作者名称

在这里插入图片描述

2.2、配置代码内容生成模板(模板内容见文末)

在这里插入图片描述

2.3、连接数据库

在这里插入图片描述

2.4、使用代码内容生成模板

在这里插入图片描述
在这里插入图片描述

模板内容:

controller.java.vm

##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("Controller")##保存文件(宏定义)
#save("/controller", "Controller.java")##包路径(宏定义)
#setPackageSuffix("controller")##定义服务名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))import $!{tableInfo.savePackageName}.domain.dto.$!{tableInfo.name}Dto;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;import java.util.List;
import lombok.RequiredArgsConstructor;##表注释(宏定义)
#tableComment("表控制层")
@RestController
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
@RequiredArgsConstructor
public class $!{tableName} extends BaseController{/*** 服务对象*/private final $!{tableInfo.name}Service $!{serviceName};/** * * @param $!{entityName}dto 请求参数封装* @author $!author * @description //TODO  分页查询所有数据* @date $!time.currTime()* @return 实例对象*/@PostMapping("/get$!{tableInfo.name}s")public TableDataInfo find$!{tableInfo.name}SelectList(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto ) {startPage();return getDataTable(this.$!{serviceName}.find$!{tableInfo.name}SelectList($!{entityName}dto)); }/** * * @param $!{entityName}dto 请求参数封装* @author $!author  * @description //TODO  通过主键查询单条数据* @date $!time.currTime()* @return 单条数据*/@PostMapping("/get$!{tableInfo.name}ById")public AjaxResult select$!{tableInfo.name}ById(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto) {return AjaxResult.success($!{serviceName}.select$!{tableInfo.name}ById($!{entityName}dto.getId())); }/** * * @param $!{entityName}dto 实体对象* @author $!author  * @description //TODO  新增数据* @date $!time.currTime()* @return 新增结果*/@PostMapping("/add$!{tableInfo.name}")public AjaxResult add$!{tableInfo.name}(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto) {return toAjax(this.$!{serviceName}.insert($!{entityName}dto)); }/** * * @param $!{entityName}dto 实体对象* @author $!author  * @description //TODO  修改数据* @date $!time.currTime()* @return 修改结果*/@PostMapping("/update$!{tableInfo.name}")public AjaxResult update(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto) {return toAjax(this.$!{serviceName}.update($!{entityName}dto)); }/** * * @param ids 主键集合* @author $!author  * @description //TODO  删除数据* @date $!time.currTime()* @return 删除结果*/@PostMapping("/delete$!{tableInfo.name}")public AjaxResult delete(@RequestParam("ids") List<Long> ids) {return toAjax(this.$!{serviceName}.deleteById(ids)); }
}

dto.java.vm

##引入宏定义
$!{define.vm}##使用宏定义设置回调(保存位置与文件后缀)
#save("/domain/dto", ".java")##使用宏定义设置包后缀
#setPackageSuffix("domain.dto")##使用全局变量实现默认包导入
$!{autoImport.vm}
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;import java.util.List;##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
public class $!{tableInfo.name}Dto {#foreach($column in $tableInfo.fullColumn)#if(${column.comment})/*** ${column.comment}*/#endprivate $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #endList<Long> ids;/*** 页*/private Integer pageNum;/*** 条*/private Integer pageSize;/*** 开始时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime startTime;/*** 结束时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime endTime;public Integer getPageNum() {return pageNum == null ? 1 : pageNum;}public Integer getPageSize() {return pageSize == null ? 10 : pageSize;}
}

mapper.java.vm

##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("Mapper")##保存文件(宏定义)
#save("/mapper", "Mapper.java")##包路径(宏定义)
#setPackageSuffix("mapper")import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.domain.pojo.$!tableInfo.name;
import org.apache.ibatis.annotations.Mapper;import java.util.List;##表注释(宏定义)
#tableComment("表数据库访问层")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {/** * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @author $!author  * @description  分页查询数据* @date $!time.currTime()* @return 实例对象*/List<$!{tableInfo.name}> select$!{tableInfo.name}List($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
}

mapper.xml.vm

##引入mybatis支持
$!{mybatisSupport.vm}##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())#set($pk = $tableInfo.pkColumn.get(0))
#end<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper"><resultMap type="$!{tableInfo.savePackageName}.domain.pojo.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
#foreach($column in $tableInfo.fullColumn)<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end</resultMap><!--查询指定行数据--><select id="select$!{tableInfo.name}List" resultMap="$!{tableInfo.name}Map">select#allSqlColumn()from $!tableInfo.obj.name<where>
#foreach($column in $tableInfo.fullColumn)<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name = #{$!column.name}</if>
#end</where></select>
</mapper>

pojo.java.vm

##引入宏定义
$!{define.vm}##使用宏定义设置回调(保存位置与文件后缀)
#save("/domain/pojo", ".java")##使用宏定义设置包后缀
#setPackageSuffix("domain.pojo")##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
import lombok.Data;##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
public class $!{tableInfo.name} {private static final long serialVersionUID = $!tool.serial();#foreach($column in $tableInfo.fullColumn)#if(${column.comment})/*** ${column.comment}*/#endprivate $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end}

service.java.vm

##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("Service")##保存文件(宏定义)
#save("/service", "Service.java")##包路径(宏定义)
#setPackageSuffix("service")import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.domain.dto.$!tableInfo.nameDto;
import $!{tableInfo.savePackageName}.domain.pojo.$!tableInfo.name;
import java.util.List;##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {/** * * @param id 主键* @author $!author  * @description  通过ID查询单条数据* @date $!time.currTime()* @return 实例对象*/public $!{tableInfo.name} select$!{tableInfo.name}ById (Long id);/** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象* @author $!author  * @description 分页查询数据* @date $!time.currTime()* @return 实例对象*/public List<$!{tableInfo.name}> find$!{tableInfo.name}SelectList ($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto);/** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象* @author $!author  * @description  新增数据* @date $!time.currTime()* @return 实例对象*/public int insert($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto);/** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象* @author $!author  * @description 修改数据并返回* @date $!time.currTime()* @return 实例对象*/public int update($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto);/**** @param ids 主键* @author $!author * @description  通过主键删除数据* @date $!time.currTime()* @return*/public int deleteById(List<Long> ids);
}

serviceImpl.java.vm

##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")##包路径(宏定义)
#setPackageSuffix("service.impl")import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.domain.pojo.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.domain.dto.$!{tableInfo.name}Dto;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;import java.util.List;
import lombok.RequiredArgsConstructor;import com.ruoyi.common.utils.bean.BeanUtils;##表注释(宏定义)
#tableComment("表服务实现类")
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
@RequiredArgsConstructor
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {private final $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;/** * * @param  id 主键* @author $!author  * @description //TODO  通过ID查询单条数据* @date $!time.currTime()* @return 实例对象*/@Overridepublic $!{tableInfo.name} select$!{tableInfo.name}ById (Long id) {LambdaQueryWrapper<$!{tableInfo.name}> $!{tool.firstLowerCase($!{tableInfo.name})}lam =  new LambdaQueryWrapper<$!{tableInfo.name}>();$!{tool.firstLowerCase($!{tableInfo.name})}lam.eq($!{tableInfo.name} ::getId ,id);return baseMapper.selectOne($!{tool.firstLowerCase($!{tableInfo.name})}lam);}/** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象* @author $!author  * @description //TODO  分页查询数据* @date $!time.currTime()* @return 实例对象*/@Overridepublic List<$!{tableInfo.name}> find$!{tableInfo.name}SelectList ($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto) {$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}) = new $!{tableInfo.name}();BeanUtils.copyBeanProp($!tool.firstLowerCase($!{tableInfo.name}),$!tool.firstLowerCase($!{tableInfo.name})dto);return this.$!{tool.firstLowerCase($tableInfo.name)}Mapper.select$!{tableInfo.name}List($!{tool.firstLowerCase($tableInfo.name)});     }/** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象* @author $!author  * @description //TODO  新增数据* @date $!time.currTime()* @return 实例对象*/@Overridepublic int insert($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto) {$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}) = new $!{tableInfo.name}();BeanUtils.copyBeanProp($!tool.firstLowerCase($!{tableInfo.name}),$!tool.firstLowerCase($!{tableInfo.name})dto);return baseMapper.insert($!tool.firstLowerCase($!{tableInfo.name}));}/** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象* @author $!author  * @description //TODO  修改数据并返回* @date $!time.currTime()* @return 实例对象*/@Overridepublic int update($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto) {$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}) = new $!{tableInfo.name}();BeanUtils.copyBeanProp($!tool.firstLowerCase($!{tableInfo.name}),$!tool.firstLowerCase($!{tableInfo.name})dto);return  baseMapper.updateById($!{tool.firstLowerCase($!{tableInfo.name})});}/**** @param idList 主键* @author $!author * @description //TODO    通过主键删除数据* @date $!time.currTime()* @return*/@Overridepublic void deleteById(List<Long> idList) {return baseMapper.deleteBatchIds(idList);}
}

这篇关于EasyCode使用指南—MybatisPlus模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/650625

相关文章

Linux grep 命令的使用指南

《Linuxgrep命令的使用指南》本文给大家介绍Linuxgrep命令的使用指南,包括基础搜索语法、实践指南,感兴趣的朋友跟随小编一起看看吧... 目录linux grep 命令全面使用指南一、基础搜索语法1. 基本文本搜索2. 多文件搜索二、常用选项详解1. 输出控制选项2. 上下文控制选项三、正则表达

mybatisplus的逻辑删除过程

《mybatisplus的逻辑删除过程》:本文主要介绍mybatisplus的逻辑删除过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录myBATisplus的逻辑删除1、在配置文件中添加逻辑删除的字段2、在实体类上加上@TableLogic3、业务层正常删除即

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

MybatisPlus中removeById删除数据库未变解决方案

《MybatisPlus中removeById删除数据库未变解决方案》MyBatisPlus中,removeById需实体类标注@TableId注解以识别数据库主键,若字段名不一致,应通过value属... 目录MyBATisPlus中removeBypythonId删除数据库未变removeById(Se

Spring Security6.3.x的使用指南与注意事项

《SpringSecurity6.3.x的使用指南与注意事项》SpringSecurity6.3.1基于现代化架构,提供简洁配置、增强默认安全性和OAuth2.1/OIDC支持,采用Lambda... 目录介绍基础配置 (Servlet 应用 - 使用 Lambda DSL)关键配置详解(Lambda DS

Python操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功