Easycode
Entity
##导入宏定义
$!{define.vm}##保存文件(宏定义)
#save("/entity", ".java")##包路径(宏定义)
#setPackageSuffix("entity")##自动导入包(全局变量)
$!{autoImport.vm}import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
##表注释(宏定义)
#tableComment("表实体类")
@SuppressWarnings("serial")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("$!{tableInfo.obj.name}")
public class $!{tableInfo.name} {
#foreach($column in $tableInfo.pkColumn)#if(${column.comment})//${column.comment}#end
@TableIdprivate $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end#foreach($column in $tableInfo.otherColumn)#if(${column.comment})//${column.comment}#endprivate $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end}
Mapper
##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("Mapper")##保存文件(宏定义)
#save("/mapper", "Mapper.java")##包路径(宏定义)
#setPackageSuffix("mapper")import com.baomidou.mybatisplus.core.mapper.BaseMapper;##表注释(宏定义)
#tableComment("表数据库访问层")
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {}
Service
##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("Service")##保存文件(宏定义)
#save("/service", "Service.java")##包路径(宏定义)
#setPackageSuffix("service")import com.baomidou.mybatisplus.extension.service.IService;##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {}
ServiceImpl
##导入宏定义
$!{define.vm}##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")##包路径(宏定义)
#setPackageSuffix("service.impl")import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;##表注释(宏定义)
#tableComment("表服务实现类")
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {}
controller
##导入宏定义
$!{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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;##表注释(宏定义)
#tableComment("表控制层")
@RestController
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
public class $!{tableName} extends ApiController {/*** 服务对象*/@Resourceprivate $!{tableInfo.name}Service $!{serviceName};/*** 分页查询所有数据** @param page 分页对象* @param $!entityName 查询实体* @return 所有数据*/@GetMappingpublic R selectAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) {return success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName)));}/*** 通过主键查询单条数据** @param id 主键* @return 单条数据*/@GetMapping("{id}")public R selectOne(@PathVariable Serializable id) {return success(this.$!{serviceName}.getById(id));}/*** 新增数据** @param $!entityName 实体对象* @return 新增结果*/@PostMappingpublic R insert(@RequestBody $!tableInfo.name $!entityName) {return success(this.$!{serviceName}.save($!entityName));}/*** 修改数据** @param $!entityName 实体对象* @return 修改结果*/@PutMappingpublic R update(@RequestBody $!tableInfo.name $!entityName) {return success(this.$!{serviceName}.updateById($!entityName));}/*** 删除数据** @param idList 主键结合* @return 删除结果*/@DeleteMappingpublic R delete(@RequestParam("idList") List<Long> idList) {return success(this.$!{serviceName}.removeByIds(idList));}
}
mybatis-plus
generator 代码生成器
在MybatisPlus 官网出
1 安装依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version>
</dependency><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.3</version>
</dependency>
2.使用
FastAutoGenerator.create("url", "username", "password").globalConfig(builder -> {builder.author("baomidou") // 设置作者.enableSwagger() // 开启 swagger 模式.fileOverride() // 覆盖已生成文件.outputDir("D://"); // 指定输出目录}).packageConfig(builder -> {builder.parent("com.baomidou.mybatisplus.samples.generator") // 设置父包名.moduleName("system") // 设置父包模块名.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://")); // 设置mapperXml生成路径}).strategyConfig(builder -> {builder.entityBuilder().enableLombok();builder.mapperBuilder().enableMapperAnnotation().build();builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符.enableRestStyle(); // 开启生成@RestController 控制器builder.addInclude("t_simple") // 设置需要生成的表名.addTablePrefix("t_", "c_"); // 设置过滤表前缀})// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}
找模板