代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code generator</title>
<link href="jquery.growl.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.12.4.min.js"></script>
<script src="xlsx.full.min.js"></script>
<script type="text/javascript" src="chosen.jquery.min.js"></script>
<script src="jquery.growl.js" type="text/javascript"></script>
<script src="common.js"></script>
<style>
.holder {
margin-bottom: 10px;
padding: 10px;
background: #fff;
border-radius: 5px;
}
.btn-holder {
margin-top: 10px;
}
label {
margin-right: 10px;
}
.script-container {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
justify-content: flex-start;
margin: 0 -5px;
}
.col-6 {
width: 25%;
min-width: 25%;
max-width: 25%;
}
.script-item {
display: none;
}
.script-holder {
margin: 0 5px 10px;
padding: 10px;
font-size: 14px;
background: #fff;
border-radius: 5px;
}
.script-holder-title {
margin-bottom: 5px;
}
.script-holder-title span {
cursor: pointer;
}
textarea {
width: calc(100% - 10px);
height: 400px;
}
</style>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="holder">
<div style='width:1px;height:1px;overflow: hidden'><label><input id='file-name' type='text' value='' /></label></div>
<input type="file" id="fileUploader" name="fileUploader" accept=".xls, .xlsx" />
<a href="tile.xlsx" target="_blank">示例</a>
</div>
<div class="holder">
JSON : <textarea id="jsonObject" style="height: 80px"> </textarea>
</div>
<div class="holder">
本代码生成器基于mysql数据库生成适用于mybatis+mysql组合的基础增删改查代码,包括bean mapper dao service controller <a
href='#front-end'>前端</a><br /><br />双击文本框、单击文件名可以复制
</div>
<div class="holder">
<div>
<label>数据库名:<input type="text" id="_database_name" /></label>
<label>数据库类型:
<select name="" id="_database_type">
<option value="MySQL">MySQL</option>
<option value="Oracle">Oracle</option>
<option value="PG">PG</option>
</select>
</label>
<label>表名:<input type="text" id="_table_name" /></label>
<label>表注释:<input type="text" id="_table_cn_name" /></label>
<label>表前缀:<input type="text" id="_table_prev" /></label>
<label>作者:<input type="text" id="_author" /></label>
<label>包名:<input type="text" id="_package_name" /></label>
</div>
<div class="btn-holder">
<label>
<button id="_generate_sql_btn">生成建表脚本</button>
</label>
<label>
<button id="_generate_be_btn">生成后端代码</button>
</label>
<label>
<button id="_generate_fe_btn">生成前端代码</button>
</label>
</div>
</div>
<div class="script-container">
<div class="col-6 script-item " id="_sql_script_holder">
<div class="script-holder">
<div class="script-holder-title">建表脚本</div>
<textarea name="" id="_sql_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_do_script_holder">
<div class="script-holder">
<div class="script-holder-title">DO: <span class="_file_name" id="_do_name"></span></div>
<textarea name="" id="_do_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_req_script_holder">
<div class="script-holder">
<div class="script-holder-title">REQ: <span class="_file_name" id="_req_name"></span></div>
<textarea name="" id="_req_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_dto_script_holder">
<div class="script-holder">
<div class="script-holder-title">DTO: <span class="_file_name" id="_dto_name"></span></div>
<textarea name="" id="_dto_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_xml_script_holder">
<div class="script-holder">
<div class="script-holder-title">XML: <span class="_file_name" id="_xml_name"></span></div>
<textarea name="" id="_xml_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_mapper_script_holder">
<div class="script-holder">
<div class="script-holder-title">Mapper: <span class="_file_name" id="_mapper_name"></span></div>
<textarea name="" id="_mapper_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_service_script_holder">
<div class="script-holder">
<div class="script-holder-title">Service: <span class="_file_name" id="_service_name"></span></div>
<textarea name="" id="_service_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_impl_script_holder">
<div class="script-holder">
<div class="script-holder-title">Impl: <span class="_file_name" id="_impl_name"></span></div>
<textarea name="" id="_impl_script" cols="30" rows="10"></textarea>
</div>
</div>
<div class="col-6 script-item" id="_controller_script_holder">
<div class="script-holder">
<div class="script-holder-title">Controller: <span class="_file_name" id="_controller_name"></span>,请修改1、8、9行
</div>
<textarea name="" id="_controller_script" cols="30" rows="10"></textarea>
</div>
</div>
</div>
</body>
</html>
<script>
let columnList = [
{
"field": "id",
"fieldName": "编号",
"fieldDesc": "编号",
"fieldType": "Long",
"isPrimaryKey": 1,
"isAutoIncrement": 1
}, { "field": "name", "fieldName": "名称", "fieldDesc": "磁贴名称", "fieldType": "String" }, {
"field": "data",
"fieldName": "磁贴配置",
"fieldDesc": "磁贴配置",
"fieldType": "String"
}, { "field": "seq_no", "fieldName": "序号", "fieldDesc": "序号", "fieldType": "Integer" }, {
"field": "create_time",
"fieldName": "创建时间",
"fieldDesc": "创建时间",
"fieldType": "Date"
}];
$(document).ready(function () {
addTest();
$("textarea").each(function () {
$(this).dblclick(function () {
var $ts = $(this);
$ts[0].select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
return $.growl.notice({
title: "复制成功",
message: "复制成功!"
});
});
});
$('._file_name').click(function () {
var $ts = $(this);
var text = $ts.text();
var $input = $("#file-name");
$input.val(text); // 修改文本框的内容
$input[0].select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
return $.growl.notice({
title: "复制成功",
message: "复制成功!"
});
});
$('#_generate_sql_btn').click(function () {
generateSqlScript();
});
$('#_generate_be_btn').click(function () {
generateBeScript();
});
$("#fileUploader").change(function (evt) {
const selectedFile = evt.target.files[0];
const reader = new FileReader();
reader.onload = function (event) {
const data = event.target.result;
const workbook = XLSX.read(data, {
type: 'binary'
});
workbook.SheetNames.forEach(function (sheetName) {
const XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if (XL_row_object.length > 0) {
document.getElementById("jsonObject").innerHTML = JSON.stringify(XL_row_object);
columnList = XL_row_object;
}
})
};
reader.onerror = function (event) {
console.error("File could not be read! Code " + event.target.error.code);
};
// 读取上传文件为二进制
reader.readAsBinaryString(selectedFile);
});
});
function addTest() {
$('#_database_name').val('easy_report');
$('#_table_name').val('tile_user');
$('#_table_cn_name').val('磁贴用户');
$('#_table_prev').val('t_');
$('#_author').val('hupeng');
$('#_package_name').val('com.iwhalecloud.er.portal');
}
function generateSqlScript() {
$('#_sql_script_holder').show();
const tableName = $('#_table_name').val();
const databaseType = $('#_database_type').val();
const dbType = databaseType || 'MySQL';// 默认mysql数据库
if (!tableName) {
alert('请输入表名')
}
if (dbType === 'MySQL') {
generateMySqlScript();
}
}
function generateMySqlScript() {
let databaseName = $('#_database_name').val();
const tableCnName = $('#_table_cn_name').val();
if (databaseName) {
databaseName = `\`${databaseName}\`.`;
}
const tableName = $('#_table_name').val() || 'tile';
const tablePrev = $('#_table_prev').val();
const fieldList = [];
let primaryKey = '';
let tableAutoIncrementFlag = '';
const str = $('#jsonObject').val();
let columnList = [];
if (str) {
columnList = JSON.parse(str);
}
columnList.forEach((item, index) => {
let nullFlag = 'DEFAULT NULL';
if (item.isPrimaryKey || item.isNotNull==='NO') {
nullFlag = 'NOT NULL';
}
let autoIncrement = '';
if (item.isAutoIncrement) {
autoIncrement = 'AUTO_INCREMENT'
primaryKey = `PRIMARY KEY (\`${item.field}\`)`
tableAutoIncrementFlag = 'AUTO_INCREMENT=0';
}
fieldList.push(`\`${item.field}\` ${Java2Mysql[item.fieldType]} ${nullFlag} ${autoIncrement} COMMENT '${item.fieldDesc}'`);
});
if (primaryKey) {
fieldList.push(primaryKey);
}
const script = `
CREATE TABLE ${databaseName}\`${tablePrev}${tableName}\` (
${fieldList.join(',\n')}
) ENGINE=InnoDB ${tableAutoIncrementFlag} DEFAULT CHARSET=utf8mb4 COMMENT='${tableCnName}';
`
$('#_sql_script').val(script);
}
function generateBeScript() {
$('#_do_script_holder').show();
$('#_req_script_holder').show();
$('#_dto_script_holder').show();
$('#_xml_script_holder').show();
$('#_mapper_script_holder').show();
$('#_service_script_holder').show();
$('#_impl_script_holder').show();
$('#_controller_script_holder').show();
const tableName = $('#_table_name').val();
const tableCnName = $('#_table_cn_name').val();
const tablePrev = $('#_table_prev').val();
const tableFullName = `${tablePrev}${tableName}`;
const author = $('#_author').val();
const packageName = $('#_package_name').val();
const entityName = toHump(tableName);
const className = firstToUpper(entityName);
const createTime = dateFormat(new Date());
const selectField = []; // 选择字段
const tableOtherName = getTableOtherName(tableFullName); // 表别名
console.log(tableOtherName);
const doField = []; // do 类属性
const voField = []; // vo dto req类属性
let importDate = '';
const str = $('#jsonObject').val();
let columnList = [];
if (str) {
columnList = JSON.parse(str);
}
columnList.forEach((item, index) => {
const fieldHumpName = toHump(item.field);
if (item.field === "id") {
doField.push(`\n\n\t/**\n\t* ${item.fieldDesc}\n\t*/\n\t@Id\n\t@Column(name = \"id\")\n\tprivate ${item.fieldType} ${fieldHumpName};`);
} else {
doField.push(`\n\n\t/**\n\t* ${item.fieldDesc}\n\t*/\n\t@Column(name = \"${item.field}\")\n\tprivate ${item.fieldType} ${fieldHumpName};`);
}
voField.push(`\n\n\t/**\n\t* ${item.fieldDesc}\n\t*/ \n\t private ${item.fieldType} ${fieldHumpName};`);
selectField.push(`${tableOtherName}.${item.field} AS ${fieldHumpName}`);
if (item.fieldType === 'Date') {
importDate = '\nimport java.util.Date;'
}
});
const doScript = `package ${packageName}.model.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;${importDate}
/**
* ${tableCnName}数据库实体类
*
* @author ${author}
* @datetime ${createTime}
*/
@Table(name = "${tableFullName}")
@Data
public class ${className}DO implements Serializable {
${doField.join('\n')}
}
`
const reqScript = `package ${packageName}.model.req;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;${importDate}
/**
* ${tableCnName}请求类
*
* @author ${author}
* @datetime ${createTime}
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class ${className}Req extends BaseReq implements Serializable {
${voField.join('\n')}
}
`;
const dtoScript = `package ${packageName}.model.dto;
import lombok.Data;
import java.io.Serializable;${importDate}
/**
* ${tableCnName}DTO
*
* @author ${author}
* @datetime ${createTime}
*/
@Data
public class ${className}DTO implements Serializable {
${voField.join('\n')}
}
`;
const xmlScript = `<?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="${packageName}.dao.${className}Mapper">
<!-- 为了返回list 类型而定义的returnMap -->
<resultMap type="${packageName}.model.dto.${className}DTO" id="lst${className}"/>
<select id="queryList" resultMap="lst${className}" parameterType="${packageName}.model.req.${className}Req">
SELECT
${selectField.join(',\n')}
FROM ${tableFullName} ${tableOtherName}
WHERE 1=1
<if test="id != null and id != '' ">
AND ${tableOtherName}.id = #{id}
</if>
<if test="orderBy!=null and orderBy!= '' ">
ORDER BY ${tableOtherName}.\$\{orderBy\} \$\{orderType\}
</if>
</select>
</mapper>`;
const mapperScript = `package ${packageName}.dao;
import ${packageName}.base.BaseMapper;
import ${packageName}.model.entity.${className}DO;
import ${packageName}.model.dto.${className}DTO;
import ${packageName}.model.req.${className}Req;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author ${author}
* @datetime ${createTime}
*/
@Repository
public interface ${className}Mapper extends BaseMapper<${className}DO> {
/**
* 数据列表
*
* @param ${entityName}Req 请求参数
* @return data list
*/
List<${className}DTO> queryList(${className}Req ${entityName}Req);
}
`;
const serviceScript = `package ${packageName}.service;
import ${packageName}.bean.result.PageRequest;
import ${packageName}.bean.result.PageResult;
import ${packageName}.model.dto.${className}DTO;
import ${packageName}.model.req.${className}Req;
import java.util.List;
/**
* ${tableCnName}service层接口
*
* @author hupeng
* @datetime 2021-03-14 22:05
*/
public interface ${className}Service {
/**
* 查询 ${tableCnName} 分页列表数据
*
* @param pageRequest {pageNum, pageSize, object}
* @return object list with page info
*/
PageResult<${className}DTO> query${className}PageList(PageRequest<${className}Req> pageRequest);
/**
* 获取 ${tableCnName} 列表
*
* @param ${entityName}Req {}
* @return object list without page info
*/
List<${className}DTO> get${className}List(${className}Req ${entityName}Req);
/**
* 查询 ${tableCnName} 单项信息
*
* @param ${entityName}Req {}
* @return object item
*/
${className}DTO query${className}Item(${className}Req ${entityName}Req);
/**
* 根据 ${tableCnName} 主键编号 获取 信息
*
* @param ${entityName}Id 编号
* @return object item
*/
${className}DTO query${className}ById(long ${entityName}Id);
/**
* 保存或者修改${tableCnName}
*
* @param ${entityName}DTO object dto
* @return insert or update row num
*/
Integer save${className}Item(${className}DTO ${entityName}DTO);
/**
* 根据主键删除 ${tableCnName} 数据
*
* @param ${entityName}Req {}
* @return delete row num
*/
Integer delete${className}Item(${className}Req ${entityName}Req);
}
`;
const implScript = `package ${packageName}.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import ${packageName}.bean.result.PageRequest;
import ${packageName}.bean.result.PageResult;
import ${packageName}.dao.${className}Mapper;
import ${packageName}.model.dto.${className}DTO;
import ${packageName}.model.entity.${className}DO;
import ${packageName}.model.req.${className}Req;
import ${packageName}.service.${className}Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author ${author}
* @datetime ${createTime}
*/
@Service
public class ${className}ServiceImpl implements ${className}Service {
private final Logger logger = LoggerFactory.getLogger(${className}ServiceImpl.class);
@Autowired
${className}Mapper ${entityName}Mapper;
@Override
public PageResult<${className}DTO> query${className}PageList(PageRequest<${className}Req> pageRequest) {
Page<${className}DTO> page = PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
List<${className}DTO> list = ${entityName}Mapper.queryList(pageRequest.getData());
PageResult<${className}DTO> pageResult = new PageResult();
pageResult.setPageNum(page.getPageNum());
pageResult.setPageSize(page.getPageSize());
pageResult.setTotal(page.getTotal());
pageResult.setPages(page.getPages());
pageResult.setData(list);
return pageResult;
}
@Override
public List<${className}DTO> get${className}List(${className}Req ${entityName}Req) {
return ${entityName}Mapper.queryList(${entityName}Req);
}
@Override
public ${className}DTO query${className}Item(${className}Req ${entityName}Req) {
${className}DO req = new ${className}DO();
${className}DO item = ${entityName}Mapper.selectOne(req);
${className}DTO dto = new ${className}DTO();
BeanUtils.copyProperties(item, dto);
return dto;
}
@Override
public ${className}DTO query${className}ById(long ${entityName}Id) {
${className}Req req = new ${className}Req();
req.setId(${entityName}Id);
${className}DO item = ${entityName}Mapper.selectByPrimaryKey(req);
${className}DTO dto = new ${className}DTO();
BeanUtils.copyProperties(item, dto);
return dto;
}
@Override
public Integer save${className}Item(${className}DTO ${entityName}DTO) {
${className}DO info = new ${className}DO();
BeanUtils.copyProperties(${entityName}DTO, info);
// 此处判断主键是否有值,存在则更新,否则新增
if (null != info.getId() && info.getId() > 0) {
return ${entityName}Mapper.updateByPrimaryKeySelective(info);
}
return ${entityName}Mapper.insert(info);
}
@Override
public Integer delete${className}Item(${className}Req ${entityName}Req) {
${className}DO item = new ${className}DO();
item.setId(${entityName}Req.getId());
return ${entityName}Mapper.delete(item);
}
}
`;
const controllerScript = `package ${packageName}.web.controller;
import ${packageName}.bean.result.PageRequest;
import ${packageName}.bean.result.PageResult;
import ${packageName}.model.dto.${className}DTO;
import ${packageName}.model.req.${className}Req;
import ${packageName}.service.${className}Service;
import ${packageName}.web.bean.PageResultVO;
import ${packageName}.web.bean.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* ${tableCnName}API接口
*
* @author ${author}
* @datetime ${createTime}
*/
@RestController
@RequestMapping("${entityName}")
public class ${className}Controller {
private Logger logger = LoggerFactory.getLogger(${className}Controller.class);
@Autowired
${className}Service ${entityName}Service;
/**
* 获取${tableCnName}列表
*
* @param req 查询参数 req 对象
* @param current 当前页码
* @param pageSize 每页条数
* @return page result vo with data list
*/
@RequestMapping(value = "", method = RequestMethod.GET)
public PageResultVO<${className}DTO> getPageList(${className}Req req, Integer current, Integer pageSize) {
PageRequest<${className}Req> pageRequest = new PageRequest<>();
pageRequest.setPageNum(current);
pageRequest.setPageSize(pageSize);
pageRequest.setData(req);
PageResultVO<${className}DTO> pageResultVo = new PageResultVO<>();
try {
PageResult<${className}DTO> pageResult = ${entityName}Service.query${className}PageList(pageRequest);
pageResultVo.setData(pageResult.getData());
pageResultVo.setPage(pageResult.getPageNum());
pageResultVo.setPageSize(pageResult.getPageSize());
pageResultVo.setTotalPage(pageResult.getPages());
pageResultVo.setTotal(pageResult.getTotal());
} catch (Exception ex) {
ex.printStackTrace();
pageResultVo.setSuccess(false);
pageResultVo.setMsg("获取 列表失败!");
}
return pageResultVo;
}
/**
* 保存${tableCnName}
*
* @param ${entityName} dto 对象
* @return result vo 包含影响行数
*/
@RequestMapping(value = "", method = RequestMethod.POST)
public ResultVO<Integer> post${className}(@RequestBody ${className}DTO ${entityName}) {
ResultVO<Integer> resultVO = new ResultVO<>();
resultVO.setSuccess(true, ${entityName}Service.save${className}Item(${entityName}));
return resultVO;
}
/**
* 获取单个${tableCnName}
*
* @param id id
* @return result vo 包含单个
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResultVO<${className}DTO> get${className}(@PathVariable Long id) {
ResultVO<${className}DTO> resultVO = new ResultVO<>();
resultVO.setSuccess(true, ${entityName}Service.query${className}ById(id));
return resultVO;
}
/**
* 更新${tableCnName}
*
* @param ${entityName} dto 对象
* @return result vo 包含影响行数
*/
@RequestMapping(value = "", method = RequestMethod.PUT)
public ResultVO<Integer> put${className}(@RequestBody ${className}DTO ${entityName}) {
ResultVO<Integer> resultVO = new ResultVO<>();
resultVO.setSuccess(true, ${entityName}Service.save${className}Item(${entityName}));
return resultVO;
}
/**
* 删除${tableCnName}
*
* @param ${entityName} req 对象
* @return result vo 包含影响行数
*/
@RequestMapping(value = "", method = RequestMethod.DELETE)
public ResultVO<Integer> delete${className}(@RequestBody ${className}Req ${entityName}) {
ResultVO<Integer> resultVO = new ResultVO<>();
resultVO.setSuccess(true, ${entityName}Service.delete${className}Item(${entityName}));
return resultVO;
}
}
`;
$('#_do_script').val(doScript);
$('#_do_name').text(`${className}DO`);
$('#_req_script').val(reqScript);
$('#_req_name').text(`${className}Req`);
$('#_dto_script').val(dtoScript);
$('#_dto_name').text(`${className}DTO`);
$('#_xml_script').val(xmlScript);
$('#_xml_name').text(`${className}Mapper.xml`)
$('#_mapper_script').val(mapperScript);
$('#_mapper_name').text(`${className}Mapper`);
$('#_service_script').val(serviceScript);
$('#_service_name').text(`${className}Service`);
$('#_impl_script').val(implScript);
$('#_impl_name').text(`${className}ServiceImpl`);
$('#_controller_script').val(controllerScript);
$('#_controller_name').text(`${className}Controller`);
}
</script>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。