用到的框架:springboot+mybatis+lombok+mysql5.7
使用mybatis开发的一大痛点就是搭建一些重复性的代码很多,想必起来的话用JPA就很容易了!所以我们需要简化mybatis的各种操作,其实就是自己生成一些代码;生成模板代码的方式有很多,如果是公司内部的话肯定也有自己的一套代码生成工具,但是我们平常写自己的项目的话,还是用自己喜欢的模板来写比较好!
第一种:最原始的mybatis逆向工程的方式,这个应该很熟悉,就是可以自动生成表对应的po,mapper和xml
第二种:就是用easycode插件直接生成表对应的controller,service,mapper和xml的常用的crud,减少代码量,用于我们平常自己写项目的时候比较实用;这也是下面会仔细说的
第三种:有开源的项目可以直接一键生成vue和后端spingboot的所有代码,推荐人人开源的一个代码生成器,点击这里,有兴趣的可以用一下,贼好用,而且还有可视化页面!
对于我们自己使用的话就没有必要那么华丽呼哨的了,这里就用第二种吧!,首先用spring Initializer随便创建一个springboot项目,务必使用阿里云的这个地址,spring的创建项目的东东贼鸡儿慢!!!
· https://start.aliyun.com/
使用idea连接mysql数据库,这个很容易,不在本篇教程之内,参考这个老哥的教程,连接成功之后是这个样子的(记得idea下载easycode插件然后重启):
然后我们随便新建一张t_user表:
create table t_user ( id int auto_increment primary key, username char(255) null, password varchar(255) null )charset = utf8;
然后根据:file->settings->Other Settings->Template Setting,新建分组,之后就是把我图中那些.java文件给创建出来(根据自己的需要进行修改生成规则,我也是参考别人的代码改了好多)
entity.java
##引入宏定义 $!define $!init ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity", ".java") ##使用宏定义设置包后缀 #setPackageSuffix("entity") ##使用全局变量实现默认包导入 $!autoImport import java.io.Serializable; import lombok.Data; ##使用宏定义实现类注释信息 #tableComment("实体类") @Data 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 }View Code
controller.java
##定义初始变量 #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 lombok.extern.slf4j.Slf4j; import com.github.pagehelper.PageInfo; import $!{tableInfo.savePackageName}.response.PageResult; import $!{tableInfo.savePackageName}.response.Result; import $!{tableInfo.savePackageName}.response.StatusCode; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.web.bind.annotation.*; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.util.List; import java.util.Objects; /** * $!{tableInfo.comment}($!{tableInfo.name})控制层 * * @author protagonist * @since $!time.currTime() */ @RestController @Slf4j @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { /** * 服务对象 */ @Resource private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)ServiceImpl; /** * 通过主键查询单条数据 * * @param $!pk.name 主键 * @return 单条数据 */ @GetMapping(value = "/get/{$!pk.name}") public Result selectOne(@PathVariable("$!pk.name") $!pk.shortType $!pk.name) { $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectById(id); if(Objects.nonNull(result)){ return new Result<>(true,StatusCode.OK,"查询成功",result); } return new Result<>(true,StatusCode.ERROR,"查询失败"); } /** * 新增一条数据 * * @param $!tool.firstLowerCase($tableInfo.name) 实体类 * @return Result对象 */ @PostMapping(value = "/insert") public Result insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { int result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.insert($!tool.firstLowerCase($tableInfo.name)); if (result > 0) { return new Result<>(true,StatusCode.OK,"新增成功",result); } return new Result<>(true,StatusCode.ERROR,"新增失败"); } /** * 修改一条数据 * * @param $!tool.firstLowerCase($tableInfo.name) 实体类 * @return Result对象 */ @PutMapping(value = "/update") public Result update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.update($!tool.firstLowerCase($tableInfo.name)); if (Objects.nonNull(result)) { return new Result<>(true,StatusCode.OK,"修改成功",result); } return new Result<>(true,StatusCode.ERROR,"修改失败"); } /** * 删除一条数据 * * @param $!pk.name 主键 * @return Result对象 */ @DeleteMapping(value = "/delete/{$!pk.name}") public Result delete(@PathVariable("$!pk.name") $!pk.shortType $!pk.name) { int result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.deleteById($!pk.name); if (result > 0) { return new Result<>(true,StatusCode.OK,"删除成功",result); } return new Result<>(true,StatusCode.ERROR,"删除失败"); } /** * 查询全部 * * @return Result对象 */ @GetMapping(value = "/selectAll") public Result<List<$tableInfo.name>> selectAll() { List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectAll(); if (CollectionUtils.isEmpty($!tool.firstLowerCase($tableInfo.name)s)) { return new Result<>(true,StatusCode.ERROR,"查询全部数据失败"); } return new Result<>(true,StatusCode.OK,"查询全部数据成功",$!tool.firstLowerCase($tableInfo.name)s); } /** * 分页查询 * * @param current 当前页 第零页和第一页的数据是一样 * @param size 每一页的数据条数 * @return Result对象 */ @GetMapping(value = "/selectPage/{current}/{size}") public Result selectPage(@PathVariable("current") Integer current,@PathVariable("size") Integer size) { PageInfo<$tableInfo.name> page = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectPage(current, size); if (Objects.nonNull(page)) { return new Result<>(true,StatusCode.OK,"分页条件查询成功",new PageResult<>(page.getTotal(),page.getList())); } return new Result<>(true,StatusCode.ERROR,"分页查询数据失败"); } }View Code
service.java
##定义初始变量 #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 com.github.pagehelper.PageInfo; /** * $!{tableInfo.comment}($!{tableInfo.name})表服务接口 * * @author protagonist * @since $!time.currTime() */ public interface $!{tableName} { /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * 分页查询 * * @param current 当前页 * @param size 每一页数据的条数 * @return 对象列表 */ PageInfo<$!{tableInfo.name}> selectPage(int current, int size); /** * 查询全部 * * @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(); }View Code
serviceImpl.java
##定义初始变量 #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.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import javax.annotation.Resource; import java.util.List; /** * $!{tableInfo.comment}($!{tableInfo.name}表)服务实现类 * * @author protagonist * @since $!time.currTime() */ @Service("$!tool.firstLowerCase($!{tableInfo.name})ServiceImpl") 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 current 当前页 * @param size 每一页的条数 * @return 对象列表 */ @Override public PageInfo<$!{tableInfo.name}> selectPage(int current, int size) { PageHelper.startPage(current,size); List<$!{tableInfo.name}> dataList = $!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll(); return new PageInfo<>(dataList); } /** * 查询所有 * * @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 @Transactional 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 @Transactional 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 @Transactional 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 @Transactional 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(); } }View Code
dao.java
##定义初始变量 #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.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层 * * @author protagonist * @since $!time.currTime() */ @Mapper public interface $!{tableName} { /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * 查询全部 * * @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(); }View Code
mapper.xml
##引入mybatis支持 $!mybatisSupport ##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml")) $&
参与评论
手机查看
返回顶部