Commit 4bdfca59 by WOSHINPC

修复BigDecimal未导包问题

添加moduleName配置,用于分模块创建代码,使原本固定生成在generator下改为可配置
添加mainPath配置,当使用者将人人项目的包名io.renren改为其他时,需配置此项
parent f1bbec38
......@@ -46,16 +46,16 @@ public class GenUtils {
* 生成代码
*/
public static void generatorCode(Map<String, String> table,
List<Map<String, String>> columns, ZipOutputStream zip){
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"));
tableEntity.setTableName(table.get("tableName" ));
tableEntity.setComments(table.get("tableComment" ));
//表名转换成Java类名
String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix"));
String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix" ));
tableEntity.setClassName(className);
tableEntity.setClassname(StringUtils.uncapitalize(className));
......@@ -63,10 +63,10 @@ public class GenUtils {
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"));
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());
......@@ -74,11 +74,13 @@ public class GenUtils {
columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
//列的数据类型,转换成Java类型
String attrType = config.getString(columnEntity.getDataType(), "unknowType");
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){
if ("PRI".equalsIgnoreCase(column.get("columnKey" )) && tableEntity.getPk() == null) {
tableEntity.setPk(columnEntity);
}
......@@ -87,15 +89,16 @@ public class GenUtils {
tableEntity.setColumns(columsList);
//没主键,则第一个字段为主键
if(tableEntity.getPk() == null){
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");
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());
......@@ -105,24 +108,27 @@ public class GenUtils {
map.put("classname", tableEntity.getClassname());
map.put("pathName", tableEntity.getClassname().toLowerCase());
map.put("columns", tableEntity.getColumns());
map.put("package", config.getString("package"));
map.put("author", config.getString("author"));
map.put("email", config.getString("email"));
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){
for (String template : templates) {
//渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
Template tpl = Velocity.getTemplate(template, "UTF-8" );
tpl.merge(context, sw);
try {
//添加到zip
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package"))));
IOUtils.write(sw.toString(), zip, "UTF-8");
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package" ), config.getString("moduleName" ))));
IOUtils.write(sw.toString(), zip, "UTF-8" );
IOUtils.closeQuietly(sw);
zip.closeEntry();
} catch (IOException e) {
......@@ -136,15 +142,15 @@ public class GenUtils {
* 列名转换成Java属性名
*/
public static String columnToJava(String columnName) {
return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "" );
}
/**
* 表名转换成Java类名
*/
public static String tableToJava(String tableName, String tablePrefix) {
if(StringUtils.isNotBlank(tablePrefix)){
tableName = tableName.replace(tablePrefix, "");
if (StringUtils.isNotBlank(tablePrefix)) {
tableName = tableName.replace(tablePrefix, "" );
}
return columnToJava(tableName);
}
......@@ -152,9 +158,9 @@ public class GenUtils {
/**
* 获取配置信息
*/
public static Configuration getConfig(){
public static Configuration getConfig() {
try {
return new PropertiesConfiguration("generator.properties");
return new PropertiesConfiguration("generator.properties" );
} catch (ConfigurationException e) {
throw new RRException("获取配置文件失败,", e);
}
......@@ -163,47 +169,47 @@ public class GenUtils {
/**
* 获取文件名
*/
public static String getFileName(String template, String className, String packageName){
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;
if (StringUtils.isNotBlank(packageName)) {
packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
}
if(template.contains("Entity.java.vm")){
if (template.contains("Entity.java.vm" )) {
return packagePath + "entity" + File.separator + className + "Entity.java";
}
if(template.contains("Dao.java.vm")){
if (template.contains("Dao.java.vm" )) {
return packagePath + "dao" + File.separator + className + "Dao.java";
}
if(template.contains("Service.java.vm")){
if (template.contains("Service.java.vm" )) {
return packagePath + "service" + File.separator + className + "Service.java";
}
if(template.contains("ServiceImpl.java.vm")){
if (template.contains("ServiceImpl.java.vm" )) {
return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
}
if(template.contains("Controller.java.vm")){
if (template.contains("Controller.java.vm" )) {
return packagePath + "controller" + File.separator + className + "Controller.java";
}
if(template.contains("Dao.xml.vm")){
return "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + "generator" + File.separator + className + "Dao.xml";
if (template.contains("Dao.xml.vm" )) {
return "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + moduleName + File.separator + className + "Dao.xml";
}
if(template.contains("list.html.vm")){
if (template.contains("list.html.vm" )) {
return "main" + File.separator + "resources" + File.separator + "views" + File.separator
+ "modules" + File.separator + "generator" + File.separator + className.toLowerCase() + ".html";
+ "modules" + File.separator + moduleName + File.separator + className.toLowerCase() + ".html";
}
if(template.contains("list.js.vm")){
if (template.contains("list.js.vm" )) {
return "main" + File.separator + "resources" + File.separator + "static" + File.separator + "js" + File.separator
+ "modules" + File.separator + "generator" + File.separator + className.toLowerCase() + ".js";
+ "modules" + File.separator + moduleName + File.separator + className.toLowerCase() + ".js";
}
if(template.contains("menu.sql.vm")){
if (template.contains("menu.sql.vm" )) {
return className.toLowerCase() + "_menu.sql";
}
......
#\u4EE3\u7801\u751F\u6210\u5668\uFF0C\u914D\u7F6E\u4FE1\u606F
mainPath=io.renren
#\u5305\u540D
package=io.renren.modules.generator
package=io.renren.modules
moduleName=generator
#\u4F5C\u8005
author=chenshun
#Email
......
package ${package}.controller;
package ${package}.${moduleName}.controller;
import java.util.List;
import java.util.Map;
......@@ -11,11 +11,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ${package}.entity.${className}Entity;
import ${package}.service.${className}Service;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import ${package}.${moduleName}.entity.${className}Entity;
import ${package}.${moduleName}.service.${className}Service;
import ${mainPath}.common.utils.PageUtils;
import ${mainPath}.common.utils.Query;
import ${mainPath}.common.utils.R;
......@@ -28,7 +28,7 @@ import io.renren.common.utils.R;
* @date ${datetime}
*/
@RestController
@RequestMapping("${pathName}")
@RequestMapping("/${moduleName}/${pathName}")
public class ${className}Controller {
@Autowired
private ${className}Service ${classname}Service;
......@@ -37,7 +37,7 @@ public class ${className}Controller {
* 列表
*/
@RequestMapping("/list")
@RequiresPermissions("${pathName}:list")
@RequiresPermissions("${moduleName}:${pathName}:list")
public R list(@RequestParam Map<String, Object> params){
//查询列表数据
Query query = new Query(params);
......@@ -55,7 +55,7 @@ public class ${className}Controller {
* 信息
*/
@RequestMapping("/info/{${pk.attrname}}")
@RequiresPermissions("${pathName}:info")
@RequiresPermissions("${moduleName}:${pathName}:info")
public R info(@PathVariable("${pk.attrname}") ${pk.attrType} ${pk.attrname}){
${className}Entity ${classname} = ${classname}Service.queryObject(${pk.attrname});
......@@ -66,7 +66,7 @@ public class ${className}Controller {
* 保存
*/
@RequestMapping("/save")
@RequiresPermissions("${pathName}:save")
@RequiresPermissions("${moduleName}:${pathName}:save")
public R save(@RequestBody ${className}Entity ${classname}){
${classname}Service.save(${classname});
......@@ -77,7 +77,7 @@ public class ${className}Controller {
* 修改
*/
@RequestMapping("/update")
@RequiresPermissions("${pathName}:update")
@RequiresPermissions("${moduleName}:${pathName}:update")
public R update(@RequestBody ${className}Entity ${classname}){
${classname}Service.update(${classname});
......@@ -88,7 +88,7 @@ public class ${className}Controller {
* 删除
*/
@RequestMapping("/delete")
@RequiresPermissions("${pathName}:delete")
@RequiresPermissions("${moduleName}:${pathName}:delete")
public R delete(@RequestBody ${pk.attrType}[] ${pk.attrname}s){
${classname}Service.deleteBatch(${pk.attrname}s);
......
package ${package}.dao;
package ${package}.${moduleName}.dao;
import ${package}.entity.${className}Entity;
import io.renren.modules.sys.dao.BaseDao;
import ${package}.${moduleName}.entity.${className}Entity;
import ${mainPath}.modules.sys.dao.BaseDao;
import org.apache.ibatis.annotations.Mapper;
/**
......
<?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}.dao.${className}Dao">
<mapper namespace="${package}.${moduleName}.dao.${className}Dao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="${package}.entity.${className}Entity" id="${classname}Map">
<resultMap type="${package}.${moduleName}.entity.${className}Entity" id="${classname}Map">
#foreach($column in $columns)
<result property="${column.attrname}" column="${column.columnName}"/>
#end
</resultMap>
<select id="queryObject" resultType="${package}.entity.${className}Entity">
<select id="queryObject" resultType="${package}.${moduleName}.entity.${className}Entity">
select * from ${tableName} where ${pk.columnName} = #{value}
</select>
<select id="queryList" resultType="${package}.entity.${className}Entity">
<select id="queryList" resultType="${package}.${moduleName}.entity.${className}Entity">
select * from ${tableName}
<choose>
<when test="sidx != null and sidx.trim() != ''">
......@@ -33,7 +33,7 @@
select count(*) from ${tableName}
</select>
<insert id="save" parameterType="${package}.entity.${className}Entity"#if($pk.extra == 'auto_increment') useGeneratedKeys="true" keyProperty="$pk.attrname"#end>
<insert id="save" parameterType="${package}.${moduleName}.entity.${className}Entity"#if($pk.extra == 'auto_increment') useGeneratedKeys="true" keyProperty="$pk.attrname"#end>
insert into ${tableName}
(
#foreach($column in $columns)
......@@ -54,7 +54,7 @@
)
</insert>
<update id="update" parameterType="${package}.entity.${className}Entity">
<update id="update" parameterType="${package}.${moduleName}.entity.${className}Entity">
update ${tableName}
<set>
#foreach($column in $columns)
......
package ${package}.entity;
package ${package}.${moduleName}.entity;
import java.io.Serializable;
import java.util.Date;
#if(${hasBigDecimal})
import java.math.BigDecimal;
#end
......
package ${package}.service;
package ${package}.${moduleName}.service;
import ${package}.entity.${className}Entity;
import ${package}.${moduleName}.entity.${className}Entity;
import java.util.List;
import java.util.Map;
......
package ${package}.service.impl;
package ${package}.${moduleName}.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -6,9 +6,9 @@ import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import ${package}.dao.${className}Dao;
import ${package}.entity.${className}Entity;
import ${package}.service.${className}Service;
import ${package}.${moduleName}.dao.${className}Dao;
import ${package}.${moduleName}.entity.${className}Entity;
import ${package}.${moduleName}.service.${className}Service;
......
......@@ -23,9 +23,9 @@
<div id="rrapp" v-cloak>
<div v-show="showList">
<div class="grid-btn">
<a v-if="hasPermission('${pathName}:save')" class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
<a v-if="hasPermission('${pathName}:update')" class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
<a v-if="hasPermission('${pathName}:delete')" class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
<a v-if="hasPermission('${moduleName}:${pathName}:save')" class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
<a v-if="hasPermission('${moduleName}:${pathName}:update')" class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
<a v-if="hasPermission('${moduleName}:${pathName}:delete')" class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
</div>
<table id="jqGrid"></table>
<div id="jqGridPager"></div>
......@@ -53,6 +53,6 @@
</div>
</div>
<script src="../../js/modules/generator/${pathName}.js"></script>
<script src="../../js/modules/${moduleName}/${pathName}.js"></script>
</body>
</html>
\ No newline at end of file
$(function () {
$("#jqGrid").jqGrid({
url: baseURL + '${pathName}/list',
url: baseURL + '${moduleName}/${pathName}/list',
datatype: "json",
colModel: [
#foreach($column in $columns)
......@@ -66,7 +66,7 @@ var vm = new Vue({
vm.getInfo(${pk.attrname})
},
saveOrUpdate: function (event) {
var url = vm.${classname}.${pk.attrname} == null ? "${pathName}/save" : "${pathName}/update";
var url = vm.${classname}.${pk.attrname} == null ? "${moduleName}/${pathName}/save" : "${moduleName}/${pathName}/update";
$.ajax({
type: "POST",
url: baseURL + url,
......@@ -92,7 +92,7 @@ var vm = new Vue({
confirm('确定要删除选中的记录?', function(){
$.ajax({
type: "POST",
url: baseURL + "${pathName}/delete",
url: baseURL + "${moduleName}/${pathName}/delete",
contentType: "application/json",
data: JSON.stringify(${pk.attrname}s),
success: function(r){
......@@ -108,7 +108,7 @@ var vm = new Vue({
});
},
getInfo: function(${pk.attrname}){
$.get(baseURL + "${pathName}/info/"+${pk.attrname}, function(r){
$.get(baseURL + "${moduleName}/${pathName}/info/"+${pk.attrname}, function(r){
vm.${classname} = r.${classname};
});
},
......
-- 菜单SQL
INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `perms`, `type`, `icon`, `order_num`)
VALUES ('1', '${comments}', 'modules/generator/${pathName}.html', NULL, '1', 'fa fa-file-code-o', '6');
VALUES ('1', '${comments}', 'modules/${moduleName}/${pathName}.html', NULL, '1', 'fa fa-file-code-o', '6');
-- 按钮父菜单ID
set @parentId = @@identity;
-- 菜单对应按钮SQL
INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `perms`, `type`, `icon`, `order_num`)
SELECT @parentId, '查看', null, '${pathName}:list,${pathName}:info', '2', null, '6';
SELECT @parentId, '查看', null, '${moduleName}:${pathName}:list,${pathName}:info', '2', null, '6';
INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `perms`, `type`, `icon`, `order_num`)
SELECT @parentId, '新增', null, '${pathName}:save', '2', null, '6';
SELECT @parentId, '新增', null, '${moduleName}:${pathName}:save', '2', null, '6';
INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `perms`, `type`, `icon`, `order_num`)
SELECT @parentId, '修改', null, '${pathName}:update', '2', null, '6';
SELECT @parentId, '修改', null, '${moduleName}:${pathName}:update', '2', null, '6';
INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `perms`, `type`, `icon`, `order_num`)
SELECT @parentId, '删除', null, '${pathName}:delete', '2', null, '6';
SELECT @parentId, '删除', null, '${moduleName}:${pathName}:delete', '2', null, '6';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment