此文将分享我个人使用的一个easycode生成方法,生成之后可以直接运行,这也就意味着,生成的代码会更加规范化。规范化就意味着会有更多的约束。
引入所需jar包
pom.xml加入一下依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </depende开婚纱店ncy> <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?uunicode=true&characterencoding=utf-8&ussl=fal&rvertimezone=utc urname: xxx password: xxxmybatis: type-alias-package: xxx.entity mapper-locations: classpath:mapper/*.xml# pagehelperpagehelper: helperdialect: mysql reasonable: true supportmethodsarguments: true params: count=countsql#showsqllogging: 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 tpagenum(int pagenum) { this.pagenum = pagenum; } public int getpagesize() { return pagesize; } public void tpagesize(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 tpagenum(int pagenum) { this.pagenum = pagenum; } public int getpagesize() { return pagesize; } public void tpagesize(int pagesize) { this.pagesize = pagesize; } public long gettotalsize() { return totalsize; } public void ttotalsize(long totalsize) { this.totalsize = totalsize; } public int gettotalpages() { return totalpages; } public void ttotalpages(int totalpages) { this.totalpages = totalpages; } public list<?> getcontent() { return content; } public void tcontent(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.tpagenum(pageinfo.getpagenum()); pageresult.tpagesize(pageinfo.getpagesize()); pageresult.ttotalsize(pageinfo.gettotal()); pageresult.ttotalpages(pageinfo.getpages()); pageresult.tcontent(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", "错误") ,ur_nofound("211", "用户不存在") ,error_account("212", "账号或密码错误") ,ur_exist("213", "用户已存在") ,ur_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.rializer.rializerfeature;/** * @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) { re中国有哪些传统节日turn 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 tstatus(string status) { this.status = status; } public string getmsg() { return msg; } public void tmsg(string msg) { this.msg = msg; } public t getdata() { return data; } public void tdata(t data) { this.data = data; } /** * 如果字段为null,该字段不显示 */ @override public string tostring() { return json.tojsonstring(this); } /** * 返回全部字段,包括null * * @return */ public string toallstring() { return json.tojsonstring(this, rializerfeature.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.pathlectors;import springfox.documentation.builders.requesthandlerlectors;import springfox.documentation.rvice.apiinfo;import springfox.documentation.rvice.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 */@configurationpublic class swagger3config { @bean public docket createrestapi() { return new docket(documentationtype.oas_30) .apiinfo(apiinfo()) .lect() .apis(requesthandlerlectors.bapackage("xxx.controller")) .paths(pathlectors.any()) .build(); } privat英语日记带翻译e 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 tting
右边的就是生成代码的模板,一次将下列模板粘贴到里面即可:
entity
##引入宏定义$!define##使用宏定义设置回调(保存位置与文件后缀)#save("/entity", ".java")##使用宏定义设置包后缀#tpackagesuffix("entity")##使用全局变量实现默认包导入$!autoimportimport java.io.rializable;##使用宏定义实现类注释信息#tablecomment("实体类")public class $!{tableinfo.name} implements rializable { private static final long rialversionuid = $!tool.rial();#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,t方法 #gettmethod($column)#end @override public string tostring(){ return "$tableinfo.name {" + #foreach($column in $tableinfo.fullcolumn) "$column.name : " + $column.name + ", " + #end '}'; }}
dao
##定义初始变量#t($tablename = $tool.append($tableinfo.name, "dao"))##设置回调$!callback.tfilename($tool.append($tablename, ".java"))$!callback.tsavepath($tool.append($tableinfo.savepath, "/dao"))##拿到主键#if(!$tableinfo.pkcolumn.impty()) #t($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} lectbyid($!pk.shorttype $!pk.name); /** * 分页查询 * * @param start 查询起始位置 * @param limit 查询条数 * @return 对象列表 */ list<$!{tableinfo.name}> lectpage(@param("start") int start, @param("limit") int limit); /** * 查询全部 * * @return 对象列表 */ list<$!{tableinfo.name}> lectall(); /** * 通过实体作为筛选条件查询 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 对象列表 */ list<$!{tableinfo.name}> lectlist($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name})); /** * 新增数据 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 影响行数 */ int inrt($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name}));/** * 批量新增 * * @param $!tool.firstlowerca($!{tableinfo.name})s 实例对象的集合 * @return 影响行数 */int batchinrt(list<$!{tableinfo.name}> $!tool.firstlowerca($!{tableinfo.name})s); /** * 修改数据 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 影响行数 */ int update($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name})); /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 影响行数 */ int deletebyid($!pk.shorttype $!pk.name); /** * 查询总数据数 * * @return 数据总数 */ int count();}
rvice
##定义初始变量#t($tablename = $tool.append($tableinfo.name, "rvice"))##设置回调$!callback.tfilename($tool.append($tablename, ".java"))$!callback.tsavepath($tool.append($tableinfo.savepath, "/rvice"))##拿到主键#if(!$tableinfo.pkcolumn.impty()) #t($pk = $tableinfo.pkcolumn.get(0))#end#if($tableinfo.savepackagename)package $!{tableinfo.savepackagename}.#{end}rvice;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} lectbyid($!pk.shorttype $!pk.name); /** * 分页查询 * * @param start 查询起始位置 * @param limit 查询条数 * @return 对象列表 */ pageresult lectpage(int start, int limit); /** * 查询全部 * * @return 对象列表 */ list<$!{tableinfo.name}> lectall(); /** * 通过实体作为筛选条件查询 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 对象列表 */ list<$!{tableinfo.name}> lectlist($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name})); /** * 新增数据 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 影响行数 */ int inrt($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name}));/** * 批量新增 * * @param $!tool.firstlowerca($!{tableinfo.name})s 实例对象的集合 * @return 影响行数 */int batchinrt(list<$!{tableinfo.name}> $!tool.firstlowerca($!{tableinfo.name})s); /** * 修改数据 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 修改 */ $!{tableinfo.name} update($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name})); /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 影响行数 */ int deletebyid($!pk.shorttype $!pk.name); /** * 查询总数据数 * * @return 数据总数 */ int count();}
rviceimpl
##定义初始变量#t($tablename = $tool.append($tableinfo.name, "rviceimpl"))##设置回调$!callback.tfilename($tool.append($tablename, ".java"))$!callback.tsavepath($tool.append($tableinfo.savepath, "/rvice/impl"))##拿到主键#if(!$tableinfo.pkcolumn.impty()) #t($pk = $tableinfo.pkcolumn.get(0))#end#if($tableinfo.savepackagename)package $!{tableinfo.savepackagename}.#{end}rvice.impl;import $!{tableinfo.savepackagename}.entity.$!{tableinfo.name};import $!{tableinfo.savepackagename}.dao.$!{tableinfo.name}dao;import $!{tableinfo.savepackagename}.rvice.$!{tableinfo.name}rvice;import org.springframework.stereotype.rvice;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() */@rvice("$!tool.firstlowerca($!{tableinfo.name})rvice")public class $!{tablename} implements $!{tableinfo.name}rvice { @resource private $!{tableinfo.name}dao $!tool.firstlowerca($!{tableinfo.name})dao; /** * 通过id查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ @override public $!{tableinfo.name} lectbyid($!pk.shorttype $!pk.name) { return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.lectbyid($!pk.name); } /** * 分页查询 * * @param start 查询起始位置 * @param limit 查询条数 * @return 对象列表 */ @override public pageresult lectpage(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.firstlowerca($!{tableinfo.name})}s = this.$!{tool.firstlowerca($!{tableinfo.name})}dao.lectall(); return new pageinfo<>($!{tool.firstlowerca($!{tableinfo.name})}s); } /** * 查询所有 * * @return 实例对象的集合 */ @override public list<$!{tableinfo.name}> lectall() { return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.lectall(); } /** * 根据条件查询 * * @return 实例对象的集合 */ @override public list<$!{tableinfo.name}> lectlist($!{tableinfo.name} $!{tool.firstlowerca($!{tableinfo.name})}) { return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.lectlist($!{tool.firstlowerca($!{tableinfo.name})}); } /** * 新增数据 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 实例对象 */ @override public int inrt($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name})) { return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.inrt($!tool.firstlowerca($!{tableinfo.name})); } /** * 批量新增 * * @param $!tool.firstlowerca($!{tableinfo.name})s 实例对象的集合 * @return 生效的条数 */ @override public int batchinrt(list<$!{tableinfo.name}> $!tool.firstlowerca($!{tableinfo.name})s) { return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.batchinrt($!tool.firstlowerca($!{tableinfo.name})s); } /** * 修改数据 * * @param $!tool.firstlowerca($!{tableinfo.name}) 实例对象 * @return 实例对象 */ @override public $!{tableinfo.name} update($!{tableinfo.name} $!tool.firstlowerca($!{tableinfo.name})) { this.$!{tool.firstlowerca($!{tableinfo.name})}dao.update($!tool.firstlowerca($!{tableinfo.name})); return this.lectbyid($!{tool.firstlowerca($!{tableinfo.name})}.get$!tool.firstupperca($pk.na马术装me)()); } /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 是否成功 */ @override public int deletebyid($!pk.shorttype $!pk.name) { return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.deletebyid($!pk.name); } /** * 查询总数据数 * * @return 数据总数 */ @override public int count(){ return this.$!{tool.firstlowerca($!{tableinfo.name})}dao.count(); }}
controller
##定义初始变量#t($tablename = $tool.append($tableinfo.name, "controller"))##设置回调$!callback.tfilename($tool.append($tablename, ".java"))$!callback.tsavepath($tool.append($tableinfo.savepath, "/controller"))##拿到主键#if(!$tableinfo.pkcolumn.impty()) #t($pk = $tableinfo.pkcolumn.get(0))#end#if($tableinfo.savepackagename)package $!{tableinfo.savepackagename}.#{end}controller;import $!{tableinfo.savepackagename}.entity.$!{tableinfo.name};import $!{tableinfo.savepackagename}.rvice.$!{tableinfo.name}rvice;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.firstlowerca($tableinfo.name)")public class $!{tablename} { /** * 服务对象 */ @resource private $!{tableinfo.name}rvice $!tool.firstlowerca($tableinfo.name)rvice; /** * 通过主键查询单条数据 * * @param $!tool.firstlowerca($tableinfo.name) 参数对象 * @return 单条数据 */ @requestmapping(value = "get", method = requestmethod.get) public returndata<$tableinfo.name> lectone($tableinfo.name $!tool.firstlowerca($tableinfo.name)) { $tableinfo.name result = $!{tool.firstlowerca($tableinfo.name)}rvice.lectbyid($!{tool.firstlowerca($tableinfo.name)}.getid()); if(result != null){ return returndata.get_success(result); } return returndata.get_fail(); } /** * 新增一条数据 * * @param $!tool.firstlowerca($tableinfo.name) 实体类 * @return respon对象 */ @requestmapping(value = "inrt", method = requestmethod.post) public returndata<$tableinfo.name> inrt(@requestbody $tableinfo.name $!tool.firstlowerca($tableinfo.name)) { int result = $!{tool.firstlowerca($tableinfo.name)}rvice.inrt($!tool.firstlowerca($tableinfo.name)); if (result > 0) { return returndata.add_success(); } return returndata.add_fail(); } /** * 修改一条数据 * * @param $!tool.firstlowerca($tableinfo.name) 实体类 * @return respon对象 */ @requestmapping(value = "update", method = requestmethod.put) public returndata<$tableinfo.name> update(@requestbody $tableinfo.name $!tool.firstlowerca($tableinfo.name)) { $tableinfo.name result = $!{tool.firstlowerca($tableinfo.name)}rvice.update($!tool.firstlowerca($tableinfo.name)); if (result != null) { return returndata.update_success(result); } return returndata.update_fail(); } /** * 删除一条数据 * * @param $!tool.firstlowerca($tableinfo.name) 参数对象 * @return respon对象 */ @requestmapping(value = "delete", method = requestmethod.delete) public returndata<$tableinfo.name> delete($tableinfo.name $!tool.firstlowerca($tableinfo.name)) { int result = $!{tool.firstlowerca($tableinfo.name)}rvice.deletebyid($!{tool.firstlowerca($tableinfo.name)}.getid()); if (result > 0) { return returndata.delete_success(); } return returndata.delete_fail(); } /** * 查询全部 * * @return respon对象 */ @requestmapping(value = "lectall", method = requestmethod.get) public returndata<list<$tableinfo.name>> lectall() { list<$tableinfo.name> $!tool.firstlowerca($tableinfo.name)s = $!{tool.firstlowerca($tableinfo.name)}rvice.lectall(); if ($!tool.firstlowerca($tableinfo.name)s != null) { return returndata.get_success($!tool.firstlowerca($tableinfo.name)s); } return returndata.get_fail(); } /** * 分页查询 * * @param start 偏移 * @param limit 条数 * @return respon对象 */ @requestmapping(value = "lectpage", method = requestmethod.get) public returndata<pageresult> lectpage(integer start, integer limit) { pageresult pageresult = $!{tool.firstlowerca($tableinfo.name)}rvice.lectpage(start, limit); if (pageresult != null) { return returndata.get_success(pageresult); } return returndata.get_fail(); } }
mapper.xml
##引入mybatis支持$!mybatissupport##设置保存名称与保存位置$!callback.tfilename($tool.append($!{tableinfo.name}, "dao.xml"))$!callback.tsavepath($tool.append($modulepath, "/src/main/resources/mapper"))##拿到主键#if(!$tableinfo.pkcolumn.impty()) #t($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="ba_column_list"> #allsqlcolumn() </sql> <!-- 查询单个 --> <lect id="lectbyid" resultmap="$!{tableinfo.name}map"> lect <include refid="ba_column_list" /> from $!tableinfo.obj.name where $!pk.obj.name = #{$!pk.name} </lect> <!-- 分页查询 --> <lect id="lectpage" resultmap="$!{tableinfo.name}map"> lect <include refid="ba_column_list" /> from $!tableinfo.obj.name limit #{start},#{limit} </lect> <!-- 查询全部 --> <lect id="lectall" resultmap="$!{tableinfo.name}map"> lect <include refid="ba_column_list" /> from $!tableinfo.obj.name </lect> <!--通过实体作为筛选条件查询--> <lect id="lectlist" resultmap="$!{tableinfo.name}map"> lect <include refid="ba_column_list" /> here反义词 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> </lect> <!-- 新增所有列 --> <inrt id="inrt" keyproperty="$!pk.name" ugeneratedkeys="true"> inrt 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) </inrt> <!-- 批量新增 --> <inrt id="batchinrt"> inrt into $!{tableinfo.obj.name}(#foreach($column in $tableinfo.fullcolumn)$!column.obj.name#if($velocityhasnext), #end#end) values <foreach collection="$!tool.firstlowerca($!{tableinfo.name})s" item="item" index="index" parator=","> ( #foreach($column in $tableinfo.fullcolumn) #{item.$!{column.name}}#if($velocityhasnext), #end#end ) </foreach> </inrt> <!-- 通过主键修改数据 --> <update id="update"> update $!{tableinfo.obj.parent.name}.$!{tableinfo.obj.name} <t> #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 </t> where $!pk.obj.name = #{$!pk.name} </update> <!--通过主键删除--> <delete id="deletebyid"> delete from $!{tableinfo.obj.name} where $!pk.obj.name = #{$!pk.name} </delete> <!-- 总数 --> <lect id="count" resulttype="int"> lect count(*) from $!{tableinfo.obj.name} </lect></mapper>
首先使用idea连接数据库,此处省略,如果有不知道怎么连接的,请参考:
如图选择需要生成代码的表->右键->easycodemybatiscodehelper->generate_code,弹出如下窗口
module:选择要生成到那个模块
package:填写自己的包名
path:工程路径
选择要生成的类,点击ok即可看到工程里已经生成了相应的类
注意:使用mybatis需要在启动类中加入注释
//扫描dao的路径配置@mapperscan(".dao")
到此这篇关于使用 easycode生成springboot+mybatis基础程序的实现示例的文章就介绍到这了,更多相关easycode生成springboot+mybatis基础程序内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 16:57:43,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/be5e72a3968e31984e079b59901a1224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:使用 EasyCode生成springboot+mybatis基础程序的实现示例.doc
本文 PDF 下载地址:使用 EasyCode生成springboot+mybatis基础程序的实现示例.pdf
留言与评论(共有 0 条评论) |