Easy Code
目录
作为开发人员,逆向工程能很大程度上的给我们带来便利,通过建立好的数据库进行逆向工程,能极大程度上的减少后端人员的开发时间,极大的提升了开发效率
一 配置编码以及作者名称
二 将Date类型转换为sql.Date
三 配置模板
3.1 pojo.java
##引入宏定义
$!define
##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("pojo")
##使用全局变量实现默认包导入
$!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
}
3.2 mapper.java
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}mapper;
import $!{tableInfo.savePackageName}.pojo.$!{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} chaXunById($!pk.shortType $!pk.name);
/**
* 通过ID删除单条数据
*
* @param $!pk.name 主键
* @return void
*/
void shanChuById($!pk.shortType $!pk.name);
}
3.3 mapper.xml
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!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}.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="chaXunById" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--通过id单个对象-->
<delete id="shanChuById">
delete from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</delete>
</mapper>
3.4 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}.pojo.$!{tableInfo.name};
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} chaXunById($!pk.shortType $!pk.name);
/**
* 通过ID删除 $!{tableName} 对象
*
* @param $!pk.name 主键
* @return Map
*/
Map<String,Object> shanChuById($!pk.shortType $!pk.name);
}
3.5 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}.pojo.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import $!{tableInfo.savePackageName}.util.ReturnCode;
import $!{tableInfo.savePackageName}.util.ReturnMap;
import $!{tableInfo.savePackageName}.util.ReturnMsg;
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}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
@Override
public $!{tableInfo.name} chaXunById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.chaXunById($!pk.name);
}
/**
* 通过ID删除对象
*
* @param $!pk.name 主键
* @return Map
*/
@Override
public Map<String,Object> shanChuById($!pk.shortType $!pk.name){
this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.shanChuById($!pk.name);
Map<String,Object> map = ReturnMap.getMap(ReturnCode.CHENG_GONG,ReturnMsg.SHAN_CHU_CHENG_GONG);
return map;
}
}
3.6 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 $!{tableInfo.savePackageName}.pojo.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import java.util.Map;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
@Api(value = "$!{tableName}管理",tags = "$!{tableName}管理接口API")
public class $!{tableName} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("/chaXunById")
@ApiOperation(value = "通过id查询对象接口",notes = "通过id查询对象接口",httpMethod = "get")
public $!{tableInfo.name} chaXunById($!pk.shortType id) {
return this.$!{tool.firstLowerCase($tableInfo.name)}Service.chaXunById(id);
}
/**
* 通过主键删除单条数据
*
* @param $!pk.name 主键
* @return Map
*/
@RequestMapping(value = "/shanChuById",method = RequestMethod.POST)
@ApiOperation(value = "通过id删除对象接口",notes = "通过id删除对象接口",httpMethod = "post")
public Map<String,Object> shanChuById(@Param("$!pk.name")$!pk.shortType $!pk.name){
return this.$!{tool.firstLowerCase($tableInfo.name)}Service.shanChuById($!pk.name);
}
}
四 测试Easy Code
idea里面的database中,右键点击数据表,
选择需要生成的文件【debug.json目前暂时用不到】
ok,在确定创建几个文件的弹出框后,可以发现项目目录中多了这些东西
此时直接运行服务器,再地址栏输入:localhost/user/chaXunById?id=1
就可以看到数据库和中id为1的数据了