init project v1.0.0
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.central.generator;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class CodeGeneratorApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CodeGeneratorApp.class, args);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.central.generator.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.generator.service.SysGeneratorService;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
|
||||
/**
|
||||
* @Author: zlt
|
||||
*/
|
||||
@RestController
|
||||
@Api(tags = "代码生成器")
|
||||
@RequestMapping("/generator")
|
||||
public class SysGeneratorController {
|
||||
@Autowired
|
||||
private SysGeneratorService sysGeneratorService;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
@ResponseBody
|
||||
@RequestMapping("/list")
|
||||
public PageResult list(@RequestParam Map<String, Object> params) {
|
||||
return sysGeneratorService.queryList(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*/
|
||||
@RequestMapping("/code")
|
||||
public void code(String tables, HttpServletResponse response) throws IOException {
|
||||
byte[] data = sysGeneratorService.generatorCode(tables.split(","));
|
||||
response.reset();
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"generator.zip\"");
|
||||
response.addHeader("Content-Length", "" + data.length);
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IOUtils.write(data, response.getOutputStream());
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.central.generator.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author zlt
|
||||
*/
|
||||
@Component
|
||||
@Mapper
|
||||
public interface SysGeneratorMapper extends SuperMapper {
|
||||
List<Map<String, Object>> queryList(Page<Map<String, Object>> page, @Param("p") Map<String, Object> map);
|
||||
|
||||
int queryTotal(Map<String, Object> map);
|
||||
|
||||
Map<String, String> queryTable(String tableName);
|
||||
|
||||
List<Map<String, String>> queryColumns(String tableName);
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package com.central.generator.model;
|
||||
|
||||
/**
|
||||
* @Author: zlt
|
||||
*/
|
||||
public class ColumnEntity {
|
||||
/**
|
||||
* 列名
|
||||
*/
|
||||
private String columnName;
|
||||
/**
|
||||
* 列名类型
|
||||
*/
|
||||
private String dataType;
|
||||
/**
|
||||
* 列名备注
|
||||
*/
|
||||
private String comments;
|
||||
/**
|
||||
* 属性名称(第一个字母大写),如:user_name => UserName
|
||||
*/
|
||||
private String attrName;
|
||||
/**
|
||||
* 属性名称(第一个字母小写),如:user_name => userName
|
||||
*/
|
||||
private String attrname;
|
||||
/**
|
||||
* 属性类型
|
||||
*/
|
||||
private String attrType;
|
||||
/**
|
||||
* auto_increment
|
||||
*/
|
||||
private String extra;
|
||||
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
|
||||
public void setColumnName(String columnName) {
|
||||
this.columnName = columnName;
|
||||
}
|
||||
|
||||
public String getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public void setDataType(String dataType) {
|
||||
this.dataType = dataType;
|
||||
}
|
||||
|
||||
public String getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void setComments(String comments) {
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public String getAttrName() {
|
||||
return attrName;
|
||||
}
|
||||
|
||||
public void setAttrName(String attrName) {
|
||||
this.attrName = attrName;
|
||||
}
|
||||
|
||||
public String getAttrname() {
|
||||
return attrname;
|
||||
}
|
||||
|
||||
public void setAttrname(String attrname) {
|
||||
this.attrname = attrname;
|
||||
}
|
||||
|
||||
public String getAttrType() {
|
||||
return attrType;
|
||||
}
|
||||
|
||||
public void setAttrType(String attrType) {
|
||||
this.attrType = attrType;
|
||||
}
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.central.generator.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: zlt
|
||||
*/
|
||||
public class TableEntity {
|
||||
/**
|
||||
* 表的名称
|
||||
*/
|
||||
private String tableName;
|
||||
/**
|
||||
* 表的备注
|
||||
*/
|
||||
private String comments;
|
||||
/**
|
||||
* 表的主键
|
||||
*/
|
||||
private ColumnEntity pk;
|
||||
/**
|
||||
* 表的列名(不包含主键)
|
||||
*/
|
||||
private List<ColumnEntity> columns;
|
||||
/**
|
||||
* 类名(第一个字母大写),如:sys_user => SysUser
|
||||
*/
|
||||
private String className;
|
||||
/**
|
||||
* 类名(第一个字母小写),如:sys_user => sysUser
|
||||
*/
|
||||
private String classname;
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void setComments(String comments) {
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public ColumnEntity getPk() {
|
||||
return pk;
|
||||
}
|
||||
|
||||
public void setPk(ColumnEntity pk) {
|
||||
this.pk = pk;
|
||||
}
|
||||
|
||||
public List<ColumnEntity> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(List<ColumnEntity> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
public String getClassname() {
|
||||
return classname;
|
||||
}
|
||||
|
||||
public void setClassname(String classname) {
|
||||
this.classname = classname;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.central.generator.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.central.common.model.PageResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @Author zlt
|
||||
*/
|
||||
@Service
|
||||
public interface SysGeneratorService extends IService {
|
||||
PageResult queryList(Map<String, Object> map);
|
||||
|
||||
Map<String, String> queryTable(String tableName);
|
||||
|
||||
List<Map<String, String>> queryColumns(String tableName);
|
||||
|
||||
byte[] generatorCode(String[] tableNames);
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.central.generator.service.impl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.generator.mapper.SysGeneratorMapper;
|
||||
import com.central.generator.service.SysGeneratorService;
|
||||
import com.central.generator.utils.GenUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @Author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysGeneratorServiceImpl extends ServiceImpl implements SysGeneratorService {
|
||||
@Autowired
|
||||
private SysGeneratorMapper sysGeneratorMapper;
|
||||
|
||||
@Override
|
||||
public PageResult<Map<String, Object>> queryList(Map<String, Object> map) {
|
||||
Page<Map<String, Object>> page = new Page<>(MapUtils.getInteger(map, "page"), MapUtils.getInteger(map, "limit"));
|
||||
|
||||
List<Map<String, Object>> list = sysGeneratorMapper.queryList(page, map);
|
||||
return PageResult.<Map<String, Object>>builder().data(list).code(0).count(page.getTotal()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> queryTable(String tableName) {
|
||||
return sysGeneratorMapper.queryTable(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, String>> queryColumns(String tableName) {
|
||||
return sysGeneratorMapper.queryColumns(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generatorCode(String[] tableNames) {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try (
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream)
|
||||
) {
|
||||
for (String tableName : tableNames) {
|
||||
//查询表信息
|
||||
Map<String, String> table = queryTable(tableName);
|
||||
//查询列信息
|
||||
List<Map<String, String>> columns = queryColumns(tableName);
|
||||
//生成代码
|
||||
GenUtils.generatorCode(table, columns, zip);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("generatorCode-error: ", e);
|
||||
}
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package com.central.generator.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import com.central.common.utils.DateUtils;
|
||||
import com.central.generator.model.ColumnEntity;
|
||||
import com.central.generator.model.TableEntity;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.configuration.Configuration;
|
||||
import org.apache.commons.configuration.ConfigurationException;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
|
||||
/**
|
||||
* 代码生成器工具类
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
public class GenUtils {
|
||||
private final static String FILE_NAME_MODEL = "Model.java.vm";
|
||||
private final static String FILE_NAME_MAPPER = "Mapper.java.vm";
|
||||
private final static String FILE_NAME_MAPPERXML = "Mapper.xml.vm";
|
||||
private final static String FILE_NAME_SERVICE = "Service.java.vm";
|
||||
private final static String FILE_NAME_SERVICEIMPL = "ServiceImpl.java.vm";
|
||||
private final static String FILE_NAME_CONTROLLER = "Controller.java.vm";
|
||||
private final static String FILE_NAME_PAGE = "index.html.vm";
|
||||
|
||||
public static List<String> getTemplates() {
|
||||
List<String> templates = new ArrayList<String>();
|
||||
templates.add("template/"+FILE_NAME_MODEL);
|
||||
templates.add("template/"+FILE_NAME_MAPPER);
|
||||
templates.add("template/"+FILE_NAME_MAPPERXML);
|
||||
templates.add("template/"+FILE_NAME_SERVICE);
|
||||
templates.add("template/"+FILE_NAME_SERVICEIMPL);
|
||||
templates.add("template/"+FILE_NAME_CONTROLLER);
|
||||
|
||||
templates.add("template/"+FILE_NAME_PAGE);
|
||||
|
||||
return templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*/
|
||||
public static void generatorCode(Map<String, String> table,
|
||||
List<Map<String, String>> columns, ZipOutputStream zip) {
|
||||
//配置信息
|
||||
Configuration config = getConfig();
|
||||
boolean hasBigDecimal = false;
|
||||
//表信息
|
||||
TableEntity tableEntity = new TableEntity();
|
||||
tableEntity.setTableName(table.get("tableName"));
|
||||
tableEntity.setComments(table.get("tableComment"));
|
||||
//表名转换成Java类名
|
||||
String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix"));
|
||||
tableEntity.setClassName(className);
|
||||
tableEntity.setClassname(StringUtils.uncapitalize(className));
|
||||
|
||||
//列信息
|
||||
List<ColumnEntity> columsList = new ArrayList<>();
|
||||
for (Map<String, String> column : columns) {
|
||||
ColumnEntity columnEntity = new ColumnEntity();
|
||||
columnEntity.setColumnName(column.get("columnName"));
|
||||
columnEntity.setDataType(column.get("dataType"));
|
||||
columnEntity.setComments(column.get("columnComment"));
|
||||
columnEntity.setExtra(column.get("extra"));
|
||||
|
||||
//列名转换成Java属性名
|
||||
String attrName = columnToJava(columnEntity.getColumnName());
|
||||
columnEntity.setAttrName(attrName);
|
||||
columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
|
||||
|
||||
//列的数据类型,转换成Java类型
|
||||
String attrType = config.getString(columnEntity.getDataType(), "unknowType");
|
||||
columnEntity.setAttrType(attrType);
|
||||
if (!hasBigDecimal && attrType.equals("BigDecimal")) {
|
||||
hasBigDecimal = true;
|
||||
}
|
||||
//是否主键
|
||||
if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
|
||||
tableEntity.setPk(columnEntity);
|
||||
}
|
||||
|
||||
columsList.add(columnEntity);
|
||||
}
|
||||
tableEntity.setColumns(columsList);
|
||||
|
||||
//没主键,则第一个字段为主键
|
||||
if (tableEntity.getPk() == null) {
|
||||
tableEntity.setPk(tableEntity.getColumns().get(0));
|
||||
}
|
||||
|
||||
//设置velocity资源加载器
|
||||
Properties prop = new Properties();
|
||||
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
||||
Velocity.init(prop);
|
||||
String mainPath = config.getString("mainPath");
|
||||
mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
|
||||
//封装模板数据
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("tableName", tableEntity.getTableName());
|
||||
map.put("comments", tableEntity.getComments());
|
||||
map.put("pk", tableEntity.getPk());
|
||||
map.put("className", tableEntity.getClassName());
|
||||
map.put("classname", tableEntity.getClassname());
|
||||
map.put("pathName", tableEntity.getClassname().toLowerCase());
|
||||
map.put("columns", tableEntity.getColumns());
|
||||
map.put("hasBigDecimal", hasBigDecimal);
|
||||
map.put("mainPath", mainPath);
|
||||
map.put("package", config.getString("package"));
|
||||
map.put("moduleName", config.getString("moduleName"));
|
||||
map.put("author", config.getString("author"));
|
||||
map.put("email", config.getString("email"));
|
||||
map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
|
||||
VelocityContext context = new VelocityContext(map);
|
||||
|
||||
//获取模板列表
|
||||
List<String> templates = getTemplates();
|
||||
for (String template : templates) {
|
||||
//渲染模板
|
||||
try (
|
||||
StringWriter sw = new StringWriter()
|
||||
) {
|
||||
Template tpl = Velocity.getTemplate(template, "UTF-8");
|
||||
tpl.merge(context, sw);
|
||||
|
||||
//添加到zip
|
||||
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package"), config.getString("moduleName"))));
|
||||
IOUtils.write(sw.toString(), zip, "UTF-8");
|
||||
zip.closeEntry();
|
||||
} catch (IOException e) {
|
||||
log.error("generatorCode-error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 列名转换成Java属性名
|
||||
*/
|
||||
public static String columnToJava(String columnName) {
|
||||
return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 表名转换成Java类名
|
||||
*/
|
||||
public static String tableToJava(String tableName, String tablePrefix) {
|
||||
if (StringUtils.isNotBlank(tablePrefix)) {
|
||||
tableName = tableName.replace(tablePrefix, "");
|
||||
}
|
||||
return columnToJava(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*/
|
||||
public static Configuration getConfig() {
|
||||
try {
|
||||
return new PropertiesConfiguration("generator.properties");
|
||||
} catch (ConfigurationException e) {
|
||||
throw new RuntimeException("获取配置文件失败,", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件名
|
||||
*/
|
||||
public static String getFileName(String template, String className, String packageName, String moduleName) {
|
||||
String packagePath = "main" + File.separator + "java" + File.separator;
|
||||
if (StringUtils.isNotBlank(packageName)) {
|
||||
packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_MODEL)) {
|
||||
return packagePath + "model" + File.separator + className + ".java";
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_MAPPER)) {
|
||||
return packagePath + "mapper" + File.separator + className + "Mapper.java";
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_SERVICE)) {
|
||||
return packagePath + "service" + File.separator + "I" + className + "Service.java";
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_SERVICEIMPL)) {
|
||||
return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_CONTROLLER)) {
|
||||
return packagePath + "controller" + File.separator + className + "Controller.java";
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_MAPPERXML)) {
|
||||
return "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + className + "Mapper.xml";
|
||||
}
|
||||
|
||||
if (template.contains(FILE_NAME_PAGE)) {
|
||||
return "main" + File.separator + "view" + File.separator + "pages" +
|
||||
File.separator + moduleName + File.separator + "index.html";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/user-center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
|
||||
mybatis-plus:
|
||||
mapper-locations: classpath:/mapper/*Mapper.xml
|
||||
#实体扫描,多个package用逗号或者分号分隔
|
||||
typeAliasesPackage: com.central.generator.model
|
||||
@@ -0,0 +1,14 @@
|
||||
server:
|
||||
port: 7300
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: code-generator
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: ${zlt.nacos.server-addr}
|
||||
file-extension: yml
|
||||
shared-dataids: common.yml
|
||||
discovery:
|
||||
server-addr: ${zlt.nacos.server-addr}
|
||||
@@ -0,0 +1,33 @@
|
||||
#包前缀
|
||||
package=com.central
|
||||
#模块名,生成的类的package为:com.central.generator
|
||||
moduleName=generator
|
||||
#作者名
|
||||
author=zlt
|
||||
#Email
|
||||
email=
|
||||
#表前缀,如表tb_sys_user 生成类 sys_user
|
||||
tablePrefix=tb_
|
||||
|
||||
#数据转换类型
|
||||
tinyint=Integer
|
||||
smallint=Integer
|
||||
mediumint=Integer
|
||||
int=Integer
|
||||
integer=Integer
|
||||
bigint=Long
|
||||
float=Float
|
||||
double=Double
|
||||
decimal=BigDecimal
|
||||
bit=Boolean
|
||||
|
||||
char=String
|
||||
varchar=String
|
||||
tinytext=String
|
||||
text=String
|
||||
mediumtext=String
|
||||
longtext=String
|
||||
|
||||
date=Date
|
||||
datetime=Date
|
||||
timestamp=Date
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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="com.central.generator.mapper.SysGeneratorMapper">
|
||||
<select id="queryList" resultType="map">
|
||||
select table_name tableName, engine, table_comment tableComment, create_time createTime from
|
||||
information_schema.tables
|
||||
where table_schema = (select database())
|
||||
<if test="p.tableName != null and p.tableName.trim() != ''">
|
||||
and table_name like concat('%', #{p.tableName}, '%')
|
||||
</if>
|
||||
order by create_time desc
|
||||
</select>
|
||||
|
||||
<select id="queryTotal" resultType="int">
|
||||
select count(*) from information_schema.tables where table_schema = (select database())
|
||||
<if test="tableName != null and tableName.trim() != ''">
|
||||
and table_name like concat('%', #{tableName}, '%')
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="queryTable" resultType="map">
|
||||
select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
|
||||
where table_schema = (select database()) and table_name = #{tableName}
|
||||
</select>
|
||||
|
||||
<select id="queryColumns" resultType="map">
|
||||
select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns
|
||||
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,74 @@
|
||||
package ${package}.${moduleName}.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import ${package}.${moduleName}.model.${className};
|
||||
import ${package}.${moduleName}.service.${className}Service;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
|
||||
/**
|
||||
* ${comments}
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${datetime}
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/${pathName}")
|
||||
@Api(tags = "${comments}")
|
||||
public class ${className}Controller {
|
||||
@Autowired
|
||||
private I${className}Service ${classname}Service;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
@ApiOperation(value = "查询列表")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
|
||||
@ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
|
||||
})
|
||||
@GetMapping("/${className}")
|
||||
public PageResult list(@RequestParam Map<String, Object> params) {
|
||||
return ${classname}Service.findList(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
*/
|
||||
@ApiOperation(value = "查询")
|
||||
@GetMapping("/${className}/{id}")
|
||||
public Result findUserById(@PathVariable Long ${pk.attrname}) {
|
||||
${className} model = ${classname}Service.getById(id);
|
||||
return Result.succeed(model, "查询成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增or更新
|
||||
*/
|
||||
@ApiOperation(value = "保存")
|
||||
@PostMapping("/${className}")
|
||||
public Result save(@RequestBody ${className} ${classname}) {
|
||||
${classname}Service.saveOrUpdateUser(${classname});
|
||||
return Result.succeed("保存成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
@ApiOperation(value = "删除")
|
||||
@DeleteMapping("/${className}/{id}")
|
||||
public Result delete(@PathVariable Long ${pk.attrname}) {
|
||||
${classname}Service.removeById(${pk.attrname});
|
||||
return Result.succeed("删除成功");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package ${package}.${moduleName}.dao;
|
||||
|
||||
import ${package}.${moduleName}.entity.${className};
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ${comments}
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${datetime}
|
||||
*/
|
||||
@Mapper
|
||||
public interface ${className}Mapper extends SuperMapper<${className}> {
|
||||
/**
|
||||
* 分页查询用户列表
|
||||
* @param page
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
List<${className}> findList(Page<${className}> page, @Param("p") Map<String, Object> params);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?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="${package}.${moduleName}.mapper.${className}Mapper">
|
||||
<select id="findList" resultType="${package}.${moduleName}.model.${className}">
|
||||
select * from ${tableName} t
|
||||
order by t.id desc
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,31 @@
|
||||
package ${package}.${moduleName}.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotations.TableName;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.EqualsAndHashCode;
|
||||
#if(${hasBigDecimal})
|
||||
import java.math.BigDecimal;
|
||||
#end
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* ${comments}
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${datetime}
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("${tableName}")
|
||||
public class ${className} extends SuperEntity {
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
#foreach ($column in $columns)
|
||||
#if($column.columnName != $pk.columnName && $column.columnName != "createTime" && $column.columnName != "updateTime")
|
||||
private $column.attrType $column.attrname;
|
||||
#end
|
||||
#end
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ${package}.${moduleName}.service;
|
||||
|
||||
import ${package}.${moduleName}.entity.${className};
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ${comments}
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${datetime}
|
||||
*/
|
||||
public interface I${className}Service extends IService<${className}> {
|
||||
/**
|
||||
* 列表
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
PageResult<${className}> findList(Map<String, Object> params);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package ${package}.${moduleName}.service.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import ${package}.${moduleName}.entity.${className};
|
||||
import ${package}.${moduleName}.dao.${className}Dao;
|
||||
import ${package}.${moduleName}.service.${className}Service;
|
||||
|
||||
/**
|
||||
* ${comments}
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${datetime}
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements I${className}Service {
|
||||
/**
|
||||
* 列表
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public PageResult<${className}> findList(Map<String, Object> params){
|
||||
Integer page = MapUtils.getInteger(params, "page");
|
||||
Integer limit = MapUtils.getInteger(params, "limit");
|
||||
if (page == null) {
|
||||
page = 1;
|
||||
}
|
||||
if (limit == null) {
|
||||
limit = -1;
|
||||
}
|
||||
Page<${className}> page = new Page<>(page, limit);
|
||||
List<${className}> list = baseMapper.findList(page, params);
|
||||
return PageResult.<${className}>builder().data(list).code(0).count(page.getTotal()).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-header">
|
||||
<h2 class="header-title">${comments}</h2>
|
||||
<span class="layui-breadcrumb pull-right">
|
||||
<a href="#!console">首页</a>
|
||||
<a><cite>${comments}</cite></a>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="layui-card-body">
|
||||
<div class="layui-form toolbar">
|
||||
搜索:
|
||||
<input id="${classname}-edit-value" class="layui-input search-input" type="text" placeholder="输入关键字"/> 
|
||||
<button id="${classname}-btn-search" class="layui-btn icon-btn"><i class="layui-icon"></i>搜索</button>
|
||||
<button id="${classname}-btn-add" class="layui-btn icon-btn"><i class="layui-icon"></i>添加</button>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<table class="layui-table" id="${classname}-table" lay-filter="${classname}-table"></table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<script>
|
||||
layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
|
||||
var form = layui.form;
|
||||
var table = layui.table;
|
||||
var config = layui.config;
|
||||
var layer = layui.layer;
|
||||
var util = layui.util;
|
||||
var admin = layui.admin;
|
||||
|
||||
//渲染表格
|
||||
table.render({
|
||||
elem: '#${classname}-table',
|
||||
url: config.base_server + 'api-${moduleName}/${pathName}/list',
|
||||
where: {
|
||||
access_token: config.getToken().access_token
|
||||
},
|
||||
page: true,
|
||||
cols: [[
|
||||
{type: 'numbers'},
|
||||
{field: 'id', sort: true, title: 'ID',width: 80},
|
||||
{field: 'name', sort: true, title: 'name'},
|
||||
{field: 'code', sort: true, title: 'code'},
|
||||
{align: 'center', toolbar: '#${classname}-table-bar', title: '操作',width: 250}
|
||||
]]
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user