您好,登录后才能下订单哦!
这篇文章给大家分享的是有关如何使用EasyCode生成springboot+mybatis基础程序的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
此文将分享我个人使用的一个easycode生成方法,生成之后可以直接运行,这也就意味着,生成的代码会更加规范化。规范化就意味着会有更多的约束。
引入所需jar包
pom.xml加入一下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> <!--Swagger3--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency>
application.yml配置
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://ip:3306/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: xxx password: xxx mybatis: type-aliases-package: xxx.entity mapper-locations: classpath:mapper/*.xml # pagehelper pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql #showSql logging: level: top: yuechenc: yueduapi: mapper : debug
以下是我个人封装的pagehelper分页工具类和统一的返回以及swagger配置
pagehelper工具类
如上图所示,在启动类所在目录下新建目录common.page/common.util
在page目录新建类PageRequest.java
package xxx.common.page; /** * @author Zhiwei Wang * @version $1.0 * @description 分页请求 * @date 2022/1/19 9:25 * @history */ public class PageRequest { /** * 当前页码 */ private int pageNum; /** * 每页数量 */ private int pageSize; public PageRequest(int start, int limit) { pageNum=start; pageSize=limit; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
在page目录新建类PageResult.java
package xxx.common.page; import java.util.List; /** * @author Zhiwei Wang * @version $1.0 * @name PageResult * @description 分页返回结果 * @date 2022/1/19 9:25 * @history */ public class PageResult { /** * 当前页码 */ private int pageNum; /** * 每页数量 */ private int pageSize; /** * 记录总数 */ private long totalSize; /** * 页码总数 */ private int totalPages; /** * 数据模型 */ private List<?> content; public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public long getTotalSize() { return totalSize; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public List<?> getContent() { return content; } public void setContent(List<?> content) { this.content = content; } }
在util目录下新建工具类PageUtils.java
package xxx.common.util; import com.github.pagehelper.PageInfo; import xxx.common.page.PageRequest; import xxx.common.page.PageResult; /** * @author Zhiwei Wang * @version $ * @name PageUtil * @description * @date 2022/1/19 9:26 * @history */ public class PageUtils { /** * 将分页信息封装到统一的接口 * @param pageRequest * @param pageInfo * @return */ public static PageResult getPageResult(PageRequest pageRequest, PageInfo<?> pageInfo) { PageResult pageResult = new PageResult(); pageResult.setPageNum(pageInfo.getPageNum()); pageResult.setPageSize(pageInfo.getPageSize()); pageResult.setTotalSize(pageInfo.getTotal()); pageResult.setTotalPages(pageInfo.getPages()); pageResult.setContent(pageInfo.getList()); return pageResult; } }
统一返回体
在common目录下新建Status.java
package xxx.common; /** * @author Zhiwei Wang * @version $ * @name Status * @description 返回信息枚举 * @date 2022/1/18 21:01 * @history */ public enum Status { FAIL("101", "失败") ,GET_FAIL("111", "查询失败") ,ADD_FAIL("121", "添加失败") ,DELETE_FAIL("131", "删除失败") ,UPDATE_FAIL("141", "修改失败") ,SUCCESS("100", "成功") ,GET_SUCCESS("110", "查询成功") ,ADD_SUCCESS("120", "添加成功") ,DELETE_SUCCESS("130", "删除成功") ,UPDATE_SUCCESS("140", "修改成功") ,ERROR("201", "错误") ,USER_NOFOUND("211", "用户不存在") ,ERROR_ACCOUNT("212", "账号或密码错误") ,USER_EXIST("213", "用户已存在") ,USER_LOCK("214", "账号被锁定,请联系管理员") ,IP_LOCK("215", "IP 被锁定,请联系管理员") ,PARAM_ERROR("303", "参数错误") ,Token_Expired("1044", "token Invalid expired"); public String status; // 状态码 public String msg; // 提示语 Status(String status, String msg) { this.status = status; this.msg = msg; } }
在common目录下新建ReturnData.java
package xxx.common; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; /** * @author Zhiwei Wang * @version $ * @name ReturnData * @description 响应数据结构封装 * @date 2022/1/18 20:59 * @history */ public class ReturnData<T> { private String status; // 状态码 private String msg; // 提示语 private T data; // 数据集合 public static <T> ReturnData<T> SUCCESS(T data) { return new ReturnData<T>(Status.SUCCESS.status, Status.SUCCESS.msg, data); } public static <T> ReturnData<T> SUCCESS(String msg) { return new ReturnData<T>(Status.SUCCESS.status, msg); } public static <T> ReturnData<T> SUCCESS() { return new ReturnData<T>(Status.SUCCESS.status, Status.SUCCESS.msg); } public static <T> ReturnData<T> GET_SUCCESS(T data) { return new ReturnData<T>(Status.GET_SUCCESS.status, Status.GET_SUCCESS.msg, data); } public static <T> ReturnData<T> GET_SUCCESS(String msg) { return new ReturnData<T>(Status.GET_SUCCESS.status, msg); } public static <T> ReturnData<T> GET_SUCCESS() { return new ReturnData<T>(Status.GET_SUCCESS.status, Status.GET_SUCCESS.msg); } public static <T> ReturnData<T> ADD_SUCCESS() { return new ReturnData<T>(Status.ADD_SUCCESS.status, Status.ADD_SUCCESS.msg); } public static <T> ReturnData<T> ADD_SUCCESS(T data) { return new ReturnData<T>(Status.ADD_SUCCESS.status, Status.ADD_SUCCESS.msg,data); } public static <T> ReturnData<T> DELETE_SUCCESS() { return new ReturnData<T>(Status.DELETE_SUCCESS.status, Status.DELETE_SUCCESS.msg); } public static <T> ReturnData<T> UPDATE_SUCCESS() { return new ReturnData<T>(Status.UPDATE_SUCCESS.status, Status.UPDATE_SUCCESS.msg); } public static <T> ReturnData<T> UPDATE_SUCCESS(T data) { return new ReturnData<T>(Status.UPDATE_SUCCESS.status, Status.UPDATE_SUCCESS.msg,data); } public static <T> ReturnData<T> FAIL(String msg) { return new ReturnData<T>(Status.FAIL.status, msg); } public static <T> ReturnData<T> FAIL() { return new ReturnData<T>(Status.FAIL.status, Status.FAIL.msg); } public static <T> ReturnData<T> GET_FAIL(String msg) { return new ReturnData<T>(Status.GET_FAIL.status, msg); } public static <T> ReturnData<T> GET_FAIL() { return new ReturnData<T>(Status.GET_FAIL.status, Status.FAIL.msg); } public static <T> ReturnData<T> ADD_FAIL() { return new ReturnData<T>(Status.ADD_FAIL.status, Status.ADD_FAIL.msg); } public static <T> ReturnData<T> DELETE_FAIL() { return new ReturnData<T>(Status.DELETE_FAIL.status, Status.DELETE_FAIL.msg); } public static <T> ReturnData<T> UPDATE_FAIL() { return new ReturnData<T>(Status.UPDATE_FAIL.status, Status.UPDATE_FAIL.msg); } public static <T> ReturnData<T> ERROR(String msg) { return new ReturnData<T>(Status.ERROR.status, msg); } public static <T> ReturnData<T> ERROR() { return new ReturnData<T>(Status.ERROR.status, Status.ERROR.msg); } public ReturnData(String status, String msg, T data) { this.status = status; this.msg = msg; this.data = data; } public ReturnData(String status, String msg) { this.status = status; this.msg = msg; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } /** * 如果字段为null,该字段不显示 */ @Override public String toString() { return JSON.toJSONString(this); } /** * 返回全部字段,包括null * * @return */ public String toAllString() { return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue); } }
swagger3配置
在common目录下新建Swagger3Config.java
package xxx.common; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; /** * @author Zhiwei Wang * @version $ * @name Swagger3Config * @description * @date 2022/1/18 21:40 * @history */ @Configuration public class Swagger3Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("xxx.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger3接口文档") .description("测试API") .contact(new Contact("测试API", "http://localhost:8080/swagger-ui/index.html", "1757895437@qq.com")) .version("1.0") .build(); } }
如图所示,打开idea的设置界面,选择插件,搜索EasyCode,点击安装即可
如图:依次打开idea设置->其他设置->EasyCode-MyBatisCodeHelper->Template Setting
右边的就是生成代码的模板,一次将下列模板粘贴到里面即可:
entity
##引入宏定义 $!define ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity", ".java") ##使用宏定义设置包后缀 #setPackageSuffix("entity") ##使用全局变量实现默认包导入 $!autoImport import java.io.Serializable; ##使用宏定义实现类注释信息 #tableComment("实体类") public class $!{tableInfo.name} implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end #foreach($column in $tableInfo.fullColumn) ##使用宏定义实现get,set方法 #getSetMethod($column) #end @Override public String toString(){ return "$tableInfo.name {" + #foreach($column in $tableInfo.fullColumn) "$column.name : " + $column.name + ", " + #end '}'; } }
dao
##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "Dao")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/dao")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; /** * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层 * * @author $!author * @since $!time.currTime() */ public interface $!{tableName} { /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * 分页查询 * * @param start 查询起始位置 * @param limit 查询条数 * @return 对象列表 */ List<$!{tableInfo.name}> selectPage(@Param("start") int start, @Param("limit") int limit); /** * 查询全部 * * @return 对象列表 */ List<$!{tableInfo.name}> selectAll(); /** * 通过实体作为筛选条件查询 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 对象列表 */ List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 新增数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 影响行数 */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 批量新增 * * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合 * @return 影响行数 */ int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s); /** * 修改数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 影响行数 */ int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 影响行数 */ int deleteById($!pk.shortType $!pk.name); /** * 查询总数据数 * * @return 数据总数 */ int count(); }
service
##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "Service")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import java.util.List; import java.util.Map; import $!{tableInfo.savePackageName}.common.page.PageResult; /** * $!{tableInfo.comment}($!{tableInfo.name})表服务接口 * * @author $!author * @since $!time.currTime() */ public interface $!{tableName} { /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * 分页查询 * * @param start 查询起始位置 * @param limit 查询条数 * @return 对象列表 */ PageResult selectPage(int start, int limit); /** * 查询全部 * * @return 对象列表 */ List<$!{tableInfo.name}> selectAll(); /** * 通过实体作为筛选条件查询 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 对象列表 */ List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 新增数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 影响行数 */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 批量新增 * * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合 * @return 影响行数 */ int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s); /** * 修改数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 修改 */ $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 影响行数 */ int deleteById($!pk.shortType $!pk.name); /** * 查询总数据数 * * @return 数据总数 */ int count(); }
serviceimpl
##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "ServiceImpl")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service; import $!{tableInfo.savePackageName}.common.page.PageResult; import $!{tableInfo.savePackageName}.common.page.PageRequest; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import $!{tableInfo.savePackageName}.common.util.PageUtils; import javax.annotation.Resource; import java.util.List; import java.util.Map; /** * $!{tableInfo.comment}($!{tableInfo.name}表)服务实现类 * * @author $!author * @since $!time.currTime() */ @Service("$!tool.firstLowerCase($!{tableInfo.name})Service") public class $!{tableName} implements $!{tableInfo.name}Service { @Resource private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao; /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ @Override public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectById($!pk.name); } /** * 分页查询 * * @param start 查询起始位置 * @param limit 查询条数 * @return 对象列表 */ @Override public PageResult selectPage(int start, int limit) { PageRequest pageRequest=new PageRequest(start,limit); return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest)); } /** * 调用分页插件完成分页 * @param pageRequest * @return */ private PageInfo<$!{tableInfo.name}> getPageInfo(PageRequest pageRequest) { int pageNum = pageRequest.getPageNum(); int pageSize = pageRequest.getPageSize(); PageHelper.startPage(pageNum, pageSize); List<$!{tableInfo.name}> $!{tool.firstLowerCase($!{tableInfo.name})}s = this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll(); return new PageInfo<>($!{tool.firstLowerCase($!{tableInfo.name})}s); } /** * 查询所有 * * @return 实例对象的集合 */ @Override public List<$!{tableInfo.name}> selectAll() { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll(); } /** * 根据条件查询 * * @return 实例对象的集合 */ @Override public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectList($!{tool.firstLowerCase($!{tableInfo.name})}); } /** * 新增数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 实例对象 */ @Override public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name})); } /** * 批量新增 * * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合 * @return 生效的条数 */ @Override public int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.batchInsert($!tool.firstLowerCase($!{tableInfo.name})s); } /** * 修改数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 实例对象 */ @Override public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.update($!tool.firstLowerCase($!{tableInfo.name})); return this.selectById($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)()); } /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 是否成功 */ @Override public int deleteById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name); } /** * 查询总数据数 * * @return 数据总数 */ @Override public int count(){ return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.count(); } }
controller
##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "Controller")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/controller")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.web.bind.annotation.*; import $!{tableInfo.savePackageName}.common.ReturnData; import $!{tableInfo.savePackageName}.common.page.PageResult; import java.util.List; import javax.annotation.Resource; /** * $!{tableInfo.comment}($!{tableInfo.name})控制层 * * @author $!author * @since $!time.currTime() */ @RestController @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { /** * 服务对象 */ @Resource private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service; /** * 通过主键查询单条数据 * * @param $!tool.firstLowerCase($tableInfo.name) 参数对象 * @return 单条数据 */ @RequestMapping(value = "get", method = RequestMethod.GET) public ReturnData<$tableInfo.name> selectOne($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.selectById($!{tool.firstLowerCase($tableInfo.name)}.getId()); if(result != null){ return ReturnData.GET_SUCCESS(result); } return ReturnData.GET_FAIL(); } /** * 新增一条数据 * * @param $!tool.firstLowerCase($tableInfo.name) 实体类 * @return Response对象 */ @RequestMapping(value = "insert", method = RequestMethod.POST) public ReturnData<$tableInfo.name> insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { int result = $!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name)); if (result > 0) { return ReturnData.ADD_SUCCESS(); } return ReturnData.ADD_FAIL(); } /** * 修改一条数据 * * @param $!tool.firstLowerCase($tableInfo.name) 实体类 * @return Response对象 */ @RequestMapping(value = "update", method = RequestMethod.PUT) public ReturnData<$tableInfo.name> update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name)); if (result != null) { return ReturnData.UPDATE_SUCCESS(result); } return ReturnData.UPDATE_FAIL(); } /** * 删除一条数据 * * @param $!tool.firstLowerCase($tableInfo.name) 参数对象 * @return Response对象 */ @RequestMapping(value = "delete", method = RequestMethod.DELETE) public ReturnData<$tableInfo.name> delete($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { int result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!{tool.firstLowerCase($tableInfo.name)}.getId()); if (result > 0) { return ReturnData.DELETE_SUCCESS(); } return ReturnData.DELETE_FAIL(); } /** * 查询全部 * * @return Response对象 */ @RequestMapping(value = "selectAll", method = RequestMethod.GET) public ReturnData<List<$tableInfo.name>> selectAll() { List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}Service.selectAll(); if ($!tool.firstLowerCase($tableInfo.name)s != null) { return ReturnData.GET_SUCCESS($!tool.firstLowerCase($tableInfo.name)s); } return ReturnData.GET_FAIL(); } /** * 分页查询 * * @param start 偏移 * @param limit 条数 * @return Response对象 */ @RequestMapping(value = "selectPage", method = RequestMethod.GET) public ReturnData<PageResult> selectPage(Integer start, Integer limit) { PageResult pageResult = $!{tool.firstLowerCase($tableInfo.name)}Service.selectPage(start, limit); if (pageResult != null) { return ReturnData.GET_SUCCESS(pageResult); } return ReturnData.GET_FAIL(); } }
mapper.xml
##引入mybatis支持 $!mybatisSupport ##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.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}.dao.$!{tableInfo.name}Dao"> <!-- 结果集 --> <resultMap type="$!{tableInfo.savePackageName}.entity.$!{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> <!-- 基本字段 --> <sql id="Base_Column_List"> #allSqlColumn() </sql> <!-- 查询单个 --> <select id="selectById" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List" /> from $!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name} </select> <!-- 分页查询 --> <select id="selectPage" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List" /> from $!tableInfo.obj.name limit #{start},#{limit} </select> <!-- 查询全部 --> <select id="selectAll" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List" /> from $!tableInfo.obj.name </select> <!--通过实体作为筛选条件查询--> <select id="selectList" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List" /> 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> <!-- 新增所有列 --> <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end) values ( #foreach($column in $tableInfo.fullColumn)#{$!{column.name}}#if($velocityHasNext), #end#end) </insert> <!-- 批量新增 --> <insert id="batchInsert"> insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end) values <foreach collection="$!tool.firstLowerCase($!{tableInfo.name})s" item="item" index="index" separator=","> ( #foreach($column in $tableInfo.fullColumn) #{item.$!{column.name}}#if($velocityHasNext), #end #end ) </foreach> </insert> <!-- 通过主键修改数据 --> <update id="update"> update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} <set> #foreach($column in $tableInfo.otherColumn) <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end"> $!column.obj.name = #{$!column.name}, </if> #end </set> where $!pk.obj.name = #{$!pk.name} </update> <!--通过主键删除--> <delete id="deleteById"> delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name} </delete> <!-- 总数 --> <select id="count" resultType="int"> select count(*) from $!{tableInfo.obj.name} </select> </mapper>
首先使用idea连接数据库,此处省略,如果有不知道怎么连接的,请参考:mysql连接idea详细教程
如图选择需要生成代码的表->右键->EasyCodeMybatisCodeHelper->Generate_Code,弹出如下窗口
Module:选择要生成到那个模块
Package:填写自己的包名
Path:工程路径
选择要生成的类,点击ok即可看到工程里已经生成了相应的类
注意:使用mybatis需要在启动类中加入注释
//扫描dao的路径配置 @MapperScan(".dao")
感谢各位的阅读!关于“如何使用EasyCode生成springboot+mybatis基础程序”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。