GenServiceImpl.java
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.lhcredit.project.tool.gen.service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lhcredit.common.constant.Constants;
import com.lhcredit.common.exception.base.BaseException;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.framework.config.GenConfig;
import com.lhcredit.project.tool.gen.domain.ColumnInfo;
import com.lhcredit.project.tool.gen.domain.TableInfo;
import com.lhcredit.project.tool.gen.mapper.GenMapper;
import com.lhcredit.project.tool.gen.util.GenUtils;
import com.lhcredit.project.tool.gen.util.VelocityInitializer;
/**
* 代码生成 服务层处理
*/
@Service
public class GenServiceImpl implements IGenService {
@Autowired
private GenMapper genMapper;
/**
* 查询ry数据库表信息
*
* @param tableInfo 表信息
* @return 数据库表列表
*/
@Override
public List<TableInfo> selectTableList(TableInfo tableInfo) {
return genMapper.selectTableList(tableInfo);
}
/**
* 生成代码
*
* @param tableName 表名称
* @return 数据
*/
@Override
public byte[] generatorCode(String tableName) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
generatorCode(tableName, zip);
IOUtils.closeQuietly(zip);
return outputStream.toByteArray();
}
/**
* 批量生成代码
*
* @param tableNames 表数组
* @return 数据
*/
@Override
public byte[] generatorCode(String[] tableNames) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for (String tableName : tableNames) {
generatorCode(tableName, zip);
}
IOUtils.closeQuietly(zip);
return outputStream.toByteArray();
}
/**
* 查询表信息并生成代码
*/
private void generatorCode(String tableName, ZipOutputStream zip) {
// 查询表信息
TableInfo table = genMapper.selectTableByName(tableName);
// 查询列信息
List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
// 表名转换成Java属性名
String className = GenUtils.tableToJava(table.getTableName());
table.setClassName(className);
table.setClassname(StringUtils.uncapitalize(className));
// 列信息
table.setColumns(GenUtils.transColums(columns));
// 设置主键
table.setPrimaryKey(table.getColumnsLast());
VelocityInitializer.initVelocity();
String packageName = GenConfig.getPackageName();
String moduleName = GenUtils.getModuleName(packageName);
VelocityContext context = GenUtils.getVelocityContext(table);
// 获取模板列表
List<String> templates = GenUtils.getTemplates();
for (String template : templates) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try {
String s = GenUtils.getFileName(template, table, moduleName);
// 添加到zip
zip.putNextEntry(new ZipEntry(s));
IOUtils.write(sw.toString(), zip, Constants.UTF8);
IOUtils.closeQuietly(sw);
zip.closeEntry();
} catch (IOException e) {
throw new BaseException("渲染模板失败,表名:" + table.getTableName(), e.getMessage());
}
}
}
}