现在除了idea自带生成实体类,搭配lombook可免生成get.set,Mybatis逆向工程生成crud外,还可指定读取表名生成表格管理页面。
启动类
代码语言:javascript复制package com.atkk.autogenera;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@Configuration
@MapperScan("com.atkk.autogenera.mapper")
public class AutogeneraApplication {
public static void main(String[] args) {
SpringApplication.run(AutogeneraApplication.class, args);
}
}
Controller
代码语言:javascript复制package com.atkk.autogenera.Controller;
import com.atkk.autogenera.Service.IGenService;
import com.atkk.autogenera.domain.TableDataInfo;
import com.atkk.autogenera.domain.TableInfo;
import com.atkk.autogenera.framework.aspectj.lang.annotation.Log;
import com.atkk.autogenera.framework.aspectj.lang.constant.BusinessType;
import com.atkk.autogenera.support.Convert;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @author zhaokk
* @create 2019-12-23-20:45
*/
@Controller
@RequestMapping("/tool/Auto")
public class GenController extends BaseController
{
private String prefix = "tool/gen";
@Autowired
private IGenService genService;
//shiro
@RequiresPermissions("tool:gen:view")
@GetMapping()
public String gen()
{
return prefix "/gen";
}
@RequiresPermissions("tool:gen:list")
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(TableInfo tableInfo)
{
startPage();
List<TableInfo> list = genService.selectTableList(tableInfo);
return getDataTable(list);
}
/**
* 生成代码
*/
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", action = BusinessType.GENCODE)
@GetMapping("/genCode/{tableName}")
public void genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
{
byte[] data = genService.generatorCode(tableName);
response.reset();
response.setHeader("Content-Disposition", "common; filename="table.zip"");
response.addHeader("Content-Length", "" data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
/**
* 批量生成代码
*/
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", action = BusinessType.GENCODE)
@GetMapping("/batchGenCode")
@ResponseBody
public void batchGenCode(HttpServletResponse response, String tables) throws IOException
{
String[] tableNames = Convert.toStrArray(tables);
byte[] data = genService.generatorCode(tableNames);
response.reset();
response.setHeader("Content-Disposition", "common; filename="table.zip"");
response.addHeader("Content-Length", "" data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
}
Service
代码语言:javascript复制package com.atkk.autogenera.Service;
import com.atkk.autogenera.domain.TableInfo;
import java.util.List;
/**
* 代码生成 服务层
*
*/
public interface IGenService
{
/**
* 查询ry数据库表信息
*
* @param tableInfo 表信息
* @return 数据库表列表
*/
public List<TableInfo> selectTableList(TableInfo tableInfo);
/**
* 生成代码
*
* @param tableName 表名称
* @return 数据
*/
public byte[] generatorCode(String tableName);
/**
* 批量生成代码
*
* @param tableNames 表数组
* @return 数据
*/
public byte[] generatorCode(String[] tableNames);
}
mapper
代码语言:javascript复制<?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.atkk.autogenera.mapper.GenMapper">
<resultMap type="TableInfo" id="TableInfoResult">
<id property="tableName" column="table_name" />
<result property="tableComment" column="table_comment" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
</resultMap>
<resultMap type="ColumnInfo" id="ColumnInfoResult">
<id property="columnName" column="column_name" />
<result property="dataType" column="data_type" />
<result property="columnComment" column="column_comment" />
</resultMap>
<sql id="selectGenVo">
select table_name, table_comment, create_time, update_time from information_schema.tables
</sql>
<select id="selectTableList" parameterType="TableInfo" resultMap="TableInfoResult">
<include refid="selectGenVo"/>
where table_comment <![CDATA[ <> ]]> '' and table_schema = (select database())
<if test="tableName != null and tableName != ''">
AND table_name like concat('%', #{tableName}, '%')
</if>
<if test="tableComment != null and tableComment != ''">
AND table_comment like concat('%', #{tableComment}, '%')
</if>
<if test="params != null and params.beginTime != ''"><!-- 开始时间检索 -->
and date_format(create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params != null and params.endTime != ''"><!-- 结束时间检索 -->
and date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
</select>
<select id="selectTableByName" parameterType="String" resultMap="TableInfoResult">
<include refid="selectGenVo"/>
where table_comment <![CDATA[ <> ]]> '' and table_schema = (select database())
and table_name = #{tableName}
</select>
<select id="selectTableColumnsByName" parameterType="String" resultMap="ColumnInfoResult">
select column_name, data_type, column_comment from information_schema.columns
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position
</select>
</mapper>
页面Html
代码语言:javascript复制<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="container-div">
<div class="row">
<div class="col-sm-12 select-info">
<form id="gen-form">
<div class="select-list gd">
<ul>
<li>
表名称:<input type="text" name="tableName"/>
</li>
<li>
表描述:<input type="text" name="tableComment"/>
</li>
<li class="time">
<label>表时间: </label>
<input type="text" class="layui-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
<span>-</span>
<input type="text" class="layui-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
</li>
<li>
<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i> 搜索</a>
</li>
</ul>
</div>
</form>
</div>
<div class="btn-group hidden-xs" id="toolbar" role="group">
<a class="btn btn-outline btn-success btn-rounded" onclick="javascript:batchGenCode()" shiro:hasPermission="tool:gen:code">
<i class="fa fa-download"></i> 批量生成
</a>
</div>
<div class="col-sm-12 select-info table-striped">
<table id="bootstrap-table" data-mobile-responsive="true"></table>
</div>
</div>
</div>
<div th:include="include :: footer"></div>
<script type="text/javascript">
var prefix = ctx "tool/gen"
$(function() {
var options = {
url: prefix "/list",
sortName: "createTime",
sortOrder: "desc",
search: false,
columns: [{
checkbox: true
},
{
field: 'tableName',
title: '表名称',
width: '20%',
sortable: true
},
{
field: 'tableComment',
title: '表描述',
width: '20%',
sortable: true
},
{
field: 'createTime',
title: '创建时间',
width: '20%',
sortable: true
},
{
field: 'updateTime',
title: '更新时间',
width: '20%',
sortable: true
},
{
title: '操作',
width: '20%',
align: 'center',
formatter: function(value, row, index) {
var msg = '<a class="btn btn-primary btn-xs" href="#" onclick="genCode('' row.tableName '')"><i class="fa fa-bug"></i>生成代码</a> ';
return msg;
}
}]
};
$.table.init(options);
});
// 生成代码
function genCode(tableName) {
$.modal.confirm("确定要生成" tableName "表代码吗?", function() {
location.href = prefix "/genCode/" tableName;
layer.msg('执行成功,正在生成代码请稍后…', { icon: 1 });
})
}
//批量生成代码
function batchGenCode() {
var rows = $.table.selectColumns("tableName");
if (rows.length == 0) {
$.modal.alertWarning("请选择要生成的数据");
return;
}
$.modal.confirm("确认要生成选中的" rows.length "条数据吗?", function() {
location.href = prefix "/batchGenCode?tables=" rows;
layer.msg('执行成功,正在生成代码请稍后…', { icon: 1 });
});
}
</script>
</body>
</html>
即可自动生成带有前台管理页面的全部文件,shiro作为权限管理,可精准的控制住每个按钮的权限分配,另外自动生成可减少工作量,逻辑复杂的sql还是要自己书写