parent
3ed179c49c
commit
ac3f17b734
@ -0,0 +1,723 @@
|
|||||||
|
package xyz.wbsite.dbtool.javafx.manger.callable;
|
||||||
|
|
||||||
|
import xyz.wbsite.dbtool.javafx.manger.FreeMarkerManager;
|
||||||
|
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
|
||||||
|
import xyz.wbsite.dbtool.javafx.manger.XmlManager;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.AbstractDBmapper;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.DataBase;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.Module;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.MySQLDBmapper;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.OracleDBmapper;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.Project;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.SQLiteDBmapper;
|
||||||
|
import xyz.wbsite.dbtool.javafx.po.Table;
|
||||||
|
import xyz.wbsite.dbtool.javafx.tool.Tool;
|
||||||
|
import xyz.wbsite.dbtool.web.frame.utils.FileUtil;
|
||||||
|
import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
public class SpringBootServiceCallable implements Callable {
|
||||||
|
|
||||||
|
private File root;
|
||||||
|
private Project project;
|
||||||
|
private AbstractDBmapper dBmapper;
|
||||||
|
private FreeMarkerManager freeMarkerManager;
|
||||||
|
|
||||||
|
public SpringBootServiceCallable(Project project, File root) {
|
||||||
|
this.root = root;
|
||||||
|
this.project = project;
|
||||||
|
this.freeMarkerManager = ManagerFactory.getFreeMarkerManager();
|
||||||
|
// 根据配置获取数据库处理器
|
||||||
|
if (project.getDatabase().name().equals(DataBase.MySQL.name())) {
|
||||||
|
this.dBmapper = new MySQLDBmapper();
|
||||||
|
} else if (project.getDatabase().name().equals(DataBase.Oracle.name())) {
|
||||||
|
this.dBmapper = new OracleDBmapper();
|
||||||
|
} else if (project.getDatabase().name().equals(DataBase.SQLite.name())) {
|
||||||
|
this.dBmapper = new SQLiteDBmapper();
|
||||||
|
} else {
|
||||||
|
this.dBmapper = new MySQLDBmapper();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean call() throws Exception {
|
||||||
|
// 创建项目文件夹
|
||||||
|
File projectDir = Tool.createPath(root, project.getProjectName());
|
||||||
|
// 清空内容
|
||||||
|
Tool.clear(projectDir);
|
||||||
|
// 生成java目录
|
||||||
|
File javaDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "main", "java");
|
||||||
|
// 生成资源目录
|
||||||
|
File resourcesDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "main", "resources");
|
||||||
|
// 生成域名目录
|
||||||
|
File domainDir = javaDir;
|
||||||
|
for (String s : project.getProjectBasePackage().split("\\.")) {
|
||||||
|
domainDir = Tool.createPath(domainDir.getAbsolutePath(), s);
|
||||||
|
}
|
||||||
|
// 生成单元测试java目录
|
||||||
|
File testJavaDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "test", "java");
|
||||||
|
File testDomainDir = testJavaDir;
|
||||||
|
for (String s : project.getProjectBasePackage().split("\\.")) {
|
||||||
|
testDomainDir = Tool.createPath(testDomainDir.getAbsolutePath(), s);
|
||||||
|
}
|
||||||
|
// 生成单元测试resources目录
|
||||||
|
File testResourcesDir = Tool.createPath(projectDir.getAbsolutePath(), "src", "test", "resources");
|
||||||
|
|
||||||
|
// 生成POM
|
||||||
|
generatePom(projectDir, project);
|
||||||
|
|
||||||
|
{//生成java文件
|
||||||
|
//生成 Controller
|
||||||
|
System.out.println("生成模块:action");
|
||||||
|
generateController(Tool.createPath(domainDir.getAbsolutePath(), "action"), project);
|
||||||
|
|
||||||
|
// 生成 Config
|
||||||
|
System.out.println("生成模块:config");
|
||||||
|
generateConfig(Tool.createPath(domainDir.getAbsolutePath(), "config"), project);
|
||||||
|
|
||||||
|
|
||||||
|
{// 模块
|
||||||
|
for (Module module : project.getModules()) {// 业务模块
|
||||||
|
module.setProjectAuthor(project.getProjectAuthor());
|
||||||
|
module.setProjectBasePackage(project.getProjectBasePackage());
|
||||||
|
module.setProjectName(project.getProjectName());
|
||||||
|
|
||||||
|
File moduleDir = Tool.createPath(domainDir.getAbsolutePath(), "module", module.getModuleName());
|
||||||
|
|
||||||
|
System.out.println("生成模块:Entity");
|
||||||
|
generateEntity(Tool.createPath(moduleDir.getAbsolutePath(), "ent"), module);
|
||||||
|
System.out.println("生成模块:Mapper");
|
||||||
|
generateMapper(Tool.createPath(moduleDir.getAbsolutePath(), "mpr"), module);
|
||||||
|
System.out.println("生成模块:Manager");
|
||||||
|
generateManager(Tool.createPath(moduleDir.getAbsolutePath(), "mgr"), module);
|
||||||
|
System.out.println("生成模块:Request");
|
||||||
|
generateRequest(Tool.createPath(moduleDir.getAbsolutePath(), "req"), module);
|
||||||
|
System.out.println("生成模块:Response");
|
||||||
|
generateResponse(Tool.createPath(moduleDir.getAbsolutePath(), "rsp"), module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// frame等内容
|
||||||
|
System.out.println("生成模块:frame");
|
||||||
|
generateFrame(Tool.createPath(domainDir.getAbsolutePath(), "frame"), project);
|
||||||
|
|
||||||
|
// Task
|
||||||
|
System.out.println("生成模块:task");
|
||||||
|
generateTask(Tool.createPath(domainDir.getAbsolutePath(), "task"), project);
|
||||||
|
|
||||||
|
// Application
|
||||||
|
System.out.println("生成模块:Application");
|
||||||
|
generateApplication(Tool.createPath(domainDir.getAbsolutePath()), project);
|
||||||
|
}
|
||||||
|
|
||||||
|
{//生成resources文件
|
||||||
|
System.out.println("生成模块:Resources");
|
||||||
|
generateResources(resourcesDir, project);
|
||||||
|
System.out.println("生成模块:Static");
|
||||||
|
System.out.println("生成模块:Templates");
|
||||||
|
}
|
||||||
|
|
||||||
|
{//生成Test
|
||||||
|
System.out.println("生成模块:Test");
|
||||||
|
generateTest(testDomainDir, project);
|
||||||
|
}
|
||||||
|
|
||||||
|
{//生成Test Resources
|
||||||
|
System.out.println("生成模块:Test Resources");
|
||||||
|
generateTestResources(testResourcesDir, project);
|
||||||
|
}
|
||||||
|
System.out.println("finish");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成POM.xml
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param project
|
||||||
|
*/
|
||||||
|
public void generatePom(File root, Project project) {
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("projectName", project.getProjectName());
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("dataBase", project.getDatabase().toString());
|
||||||
|
ctx.put("project", project);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "pom.xml"), "SpringBootService/pom.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成Controller类
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param project
|
||||||
|
*/
|
||||||
|
public void generateController(File root, Project project) {
|
||||||
|
Tool.clear(root);
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
ctx.put("modules", project.getModules());
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
File ajax = Tool.createPath(root.getAbsolutePath(), "ajax");
|
||||||
|
|
||||||
|
if (project.isNeedSys()) {//生成系统模块
|
||||||
|
File system = Tool.createPath(ajax.getAbsolutePath(), "system");
|
||||||
|
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/action/ajax/system/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(system.getAbsolutePath(), name), "SpringBootService/java/action/ajax/system/" + name, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
File api = Tool.createPath(root.getAbsolutePath(), "api");
|
||||||
|
|
||||||
|
for (Module module : project.getModules()) {
|
||||||
|
File m = Tool.createPath(api.getAbsolutePath(), module.getModuleName());
|
||||||
|
|
||||||
|
for (Table table : module.getTables()) {
|
||||||
|
HashMap<String, Object> ctxss = new HashMap<String, Object>();
|
||||||
|
ctxss.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctxss.put("module", module.getModuleName());
|
||||||
|
ctxss.put("author", project.getProjectAuthor());
|
||||||
|
ctxss.put("date", new Date());
|
||||||
|
ctxss.put("table", table);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(m.getAbsolutePath(), table.getCName() + "Api.java"), "SpringBootService/java/action/api/Api.java", ctxss);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.isNeedSys()) {//生成系统模块
|
||||||
|
File system = Tool.createPath(api.getAbsolutePath(), "system");
|
||||||
|
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/action/api/system/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(system.getAbsolutePath(), name), "SpringBootService/java/action/api/system/" + name, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "GlobalController.java"), "SpringBootService/java/action/GlobalController.java", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateConfig(File root, Project project) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("projectName", project.getProjectName());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
//config
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ActionConfig.java"), "SpringBootService/java/config/ActionConfig.java", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "CacheConfig.java"), "SpringBootService/java/config/CacheConfig.java", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "SecurityConfig.java"), "SpringBootService/java/config/SecurityConfig.java", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ScheduleConfig.java"), "SpringBootService/java/config/ScheduleConfig.java", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ThreadPoolConfig.java"), "SpringBootService/java/config/ThreadPoolConfig.java", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "WebMvcConfig.java"), "SpringBootService/java/config/WebMvcConfig.java", ctx);
|
||||||
|
if (project.getDatabase().name().equals(DataBase.SQLite.name())) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "SQLiteConfig.java"), "SpringBootService/java/config/SQLiteConfig.java", ctx);
|
||||||
|
}
|
||||||
|
if (project.isNeedMoreDB()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "MapperMainConfig.java"), "SpringBootService/java/config/MapperMainConfig.java", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "MapperTwoConfig.java"), "SpringBootService/java/config/MapperTwoConfig.java", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成entity
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param md
|
||||||
|
*/
|
||||||
|
public void generateEntity(File root, Module md) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Table table : md.getTables()) {
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("basePackage", md.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", md.getModuleName());
|
||||||
|
ctx.put("table", table);
|
||||||
|
ctx.put("author", md.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
File file = Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + ".java");
|
||||||
|
freeMarkerManager.outputTemp(file, "SpringBootService/java/module/ent/entity.ftl", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成Mapper
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param md
|
||||||
|
*/
|
||||||
|
public void generateMapper(File root, Module md) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("module", md);
|
||||||
|
ctx.put("dataBase", project.getDatabase().toString());
|
||||||
|
ctx.put("basePackage", md.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", md.getModuleName());
|
||||||
|
ctx.put("author", md.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
for (Table table : md.getTables()) {
|
||||||
|
ctx.put("table", table);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "Mapper" + ".java"), "SpringBootService/java/module/mpr/mapper.java", ctx);
|
||||||
|
|
||||||
|
if (table.getSys()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "Mapper" + ".xml"), "SpringBootService/java/module/mpr/" + project.getDatabase().name() + "_mapper.xml", ctx);
|
||||||
|
} else {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "Mapper" + ".xml"), "SpringBootService/java/module/mpr/" + project.getDatabase().name() + "_NSYS_mapper.xml", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateManager(File root, Module md) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("module", md);
|
||||||
|
ctx.put("basePackage", md.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", md.getModuleName());
|
||||||
|
ctx.put("author", md.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
for (Table table : md.getTables()) {
|
||||||
|
ctx.put("table", table);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "Manager" + ".java"), "SpringBootService/java/module/mgr/manager.ftl", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "ManagerImpl" + ".java"), "SpringBootService/java/module/mgr/managerImpl.ftl", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成Request
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param md
|
||||||
|
*/
|
||||||
|
public void generateRequest(File root, Module md) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("module", md);
|
||||||
|
ctx.put("basePackage", md.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", md.getModuleName());
|
||||||
|
ctx.put("author", md.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
if (md.getModuleName().equals("api")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ApiExampleRequest" + ".java"), "SpringBootService/java/module/req/ApiExampleRequest.java", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Table table : md.getTables()) {
|
||||||
|
ctx.put("table", table);
|
||||||
|
|
||||||
|
if (table.getCreate()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "CreateRequest" + ".java"), "SpringBootService/java/module/req/createRequestClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getDelete()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "DeleteRequest" + ".java"), "SpringBootService/java/module/req/deleteRequestClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getUpdate()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "UpdateRequest" + ".java"), "SpringBootService/java/module/req/updateRequestClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getFind()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "FindRequest" + ".java"), "SpringBootService/java/module/req/findRequestClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getGet()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "GetRequest" + ".java"), "SpringBootService/java/module/req/getRequestClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getSearch()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "SearchRequest" + ".java"), "SpringBootService/java/module/req/searchRequestClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成一般文件
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param md
|
||||||
|
*/
|
||||||
|
public void generateResponse(File root, Module md) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("module", md);
|
||||||
|
ctx.put("basePackage", md.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", md.getModuleName());
|
||||||
|
ctx.put("author", md.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
if (md.getModuleName().equals("api")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ApiExampleResponse" + ".java"), "SpringBootService/java/module/rsp/ApiExampleResponse.java", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Table table : md.getTables()) {
|
||||||
|
ctx.put("table", table);
|
||||||
|
|
||||||
|
if (table.getCreate()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "CreateResponse" + ".java"), "SpringBootService/java/module/rsp/createResponseClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getDelete()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "DeleteResponse" + ".java"), "SpringBootService/java/module/rsp/deleteResponseClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getUpdate()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "UpdateResponse" + ".java"), "SpringBootService/java/module/rsp/updateResponseClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getFind()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "FindResponse" + ".java"), "SpringBootService/java/module/rsp/findResponseClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getGet()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "GetResponse" + ".java"), "SpringBootService/java/module/rsp/getResponseClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (table.getSearch()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "SearchResponse" + ".java"), "SpringBootService/java/module/rsp/searchResponseClass.ftl", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成frame类
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param project
|
||||||
|
*/
|
||||||
|
public void generateFrame(File root, Project project) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("timestamp", new Date().getTime());
|
||||||
|
|
||||||
|
File auth = Tool.createPath(root.getAbsolutePath(), "auth");
|
||||||
|
File base = Tool.createPath(root.getAbsolutePath(), "base");
|
||||||
|
File excel = Tool.createPath(root.getAbsolutePath(), "excel");
|
||||||
|
File excelannotation = Tool.createPath(excel.getAbsolutePath(), "annotation");
|
||||||
|
File excelconverter = Tool.createPath(excel.getAbsolutePath(), "converter");
|
||||||
|
File exception = Tool.createPath(excel.getAbsolutePath(), "exception");
|
||||||
|
File excelstyle = Tool.createPath(excel.getAbsolutePath(), "style");
|
||||||
|
File schedule = Tool.createPath(root.getAbsolutePath(), "schedule");
|
||||||
|
File utils = Tool.createPath(root.getAbsolutePath(), "utils");
|
||||||
|
File validation = Tool.createPath(root.getAbsolutePath(), "validation");
|
||||||
|
|
||||||
|
//auth
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/auth/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(auth.getAbsolutePath(), name), "SpringBootService/java/frame/auth/" + name, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
//base
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/base/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(base.getAbsolutePath(), name), "SpringBootService/java/frame/base/" + name, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
//excel
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(excel.getAbsolutePath(), name), "SpringBootService/java/frame/excel/" + name, ctx);
|
||||||
|
}
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/annotation/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(excelannotation.getAbsolutePath(), name), "SpringBootService/java/frame/excel/annotation/" + name, ctx);
|
||||||
|
}
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/converter/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(excelconverter.getAbsolutePath(), name), "SpringBootService/java/frame/excel/converter/" + name, ctx);
|
||||||
|
}
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/exception/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(exception.getAbsolutePath(), name), "SpringBootService/java/frame/excel/exception/" + name, ctx);
|
||||||
|
}
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/excel/style/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(excelstyle.getAbsolutePath(), name), "SpringBootService/java/frame/excel/style/" + name, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
//schedule
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/schedule/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(schedule.getAbsolutePath(), name), "SpringBootService/java/frame/schedule/" + name, ctx);
|
||||||
|
}
|
||||||
|
//utils
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/utils/")) {
|
||||||
|
ArrayList<String> filters = new ArrayList<>();
|
||||||
|
filters.add("MailUtil.java");
|
||||||
|
|
||||||
|
if (filters.contains(name)) {
|
||||||
|
if (project.isNeedEMail()) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(utils.getAbsolutePath(), name), "SpringBootService/java/frame/utils/" + name, ctx);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(utils.getAbsolutePath(), name), "SpringBootService/java/frame/utils/" + name, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//validation
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/frame/validation/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(validation.getAbsolutePath(), name), "SpringBootService/java/frame/validation/" + name, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成frame类
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param project
|
||||||
|
*/
|
||||||
|
public void generateTask(File root, Project project) {
|
||||||
|
if (!root.exists()) {
|
||||||
|
root.mkdirs();
|
||||||
|
} else {
|
||||||
|
Tool.clear(root);
|
||||||
|
}
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("timestamp", new Date().getTime());
|
||||||
|
|
||||||
|
//task
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/java/task/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), name), "SpringBootService/java/task/" + name, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成base类
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param project
|
||||||
|
*/
|
||||||
|
public void generateApplication(File root, Project project) {
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("project", project);
|
||||||
|
ctx.put("timestamp", new Date().getTime());
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "Application.java"), "SpringBootService/java/Application.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成properties
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @param project
|
||||||
|
*/
|
||||||
|
public void generateResources(File root, Project project) {
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("dataBase", project.getDatabase().toString());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("dBmapper", dBmapper);
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("projectName", project.getProjectName());
|
||||||
|
ctx.put("project", project);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "start-dev.bat"), "SpringBootService/resources/start-dev.bat", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "start-prod.bat"), "SpringBootService/resources/start-prod.bat", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "application-dev.properties"), "SpringBootService/resources/application-dev.ftl", ctx);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "application-prod.properties"), "SpringBootService/resources/application-prod.ftl", ctx);
|
||||||
|
Tool.outputResource("SpringBootService/resources/logback-spring.xml", Tool.createFile(root.getAbsolutePath(), "logback-spring.xml"));
|
||||||
|
|
||||||
|
File dbtool = Tool.createPath(root.getAbsolutePath(), "dbtool");
|
||||||
|
{
|
||||||
|
XmlManager xmlManager = ManagerFactory.getXmlManager();
|
||||||
|
xmlManager.saveAs(dbtool, project);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Module module : project.getModules()) {
|
||||||
|
ctx.put("moduleName", module.getModuleName());
|
||||||
|
ctx.put("module", module);
|
||||||
|
File tableDir = Tool.createPath(dbtool.getAbsolutePath(), module.getModuleName() + "_table");
|
||||||
|
for (Table table : module.getTables()) {
|
||||||
|
ctx.put("table", table);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(tableDir.getAbsolutePath(), project.getDatabase().name() + "_" + table.getTableName() + ".sql"), "SpringBootService/resources/dbtool/table.ftl", ctx);
|
||||||
|
}
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(tableDir.getAbsolutePath(), project.getDatabase().name() + "_ALL_TABLE" + ".sql"), "SpringBootService/resources/dbtool/tableAll.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.isNeedSys()) {//生成系统模块
|
||||||
|
File system = Tool.createPath(dbtool.getAbsolutePath(), "system_table");
|
||||||
|
|
||||||
|
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBootService/resources/dbtool/system/")) {
|
||||||
|
if (name.contains(project.getDatabase().name())) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(system.getAbsolutePath(), name), "SpringBootService/resources/dbtool/system/" + name, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DataBase.SQLite.name().equals(project.getDatabase().name())) {
|
||||||
|
File dbFile = Tool.createFile(root.getAbsolutePath(), project.getProjectName() + ".db");
|
||||||
|
Connection connection = null;
|
||||||
|
Statement statement = null;
|
||||||
|
try {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());
|
||||||
|
|
||||||
|
for (File fileModule : dbtool.listFiles()) {
|
||||||
|
if (!fileModule.isDirectory()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (File file : fileModule.listFiles()) {
|
||||||
|
if (!file.getName().startsWith("SQLite_ALL_") && file.getName().endsWith(".sql")) {
|
||||||
|
statement = connection.createStatement();
|
||||||
|
String sql = FileUtil.readFileToString(file);
|
||||||
|
statement.execute(sql);
|
||||||
|
statement.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ClassNotFoundException | SQLException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (statement != null) statement.close();
|
||||||
|
if (connection != null) connection.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateTestResources(File root, Project project) {
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("projectName", project.getProjectName());
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("dataBase", project.getDatabase().toString());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("dBmapper", dBmapper);
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
ctx.put("project", project);
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "application.properties"), "SpringBootService/test/application.ftl", ctx);
|
||||||
|
Tool.outputResource("SpringBootService/test/logback-spring.xml", Tool.createFile(root.getAbsolutePath(), "logback-spring.xml"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateTest(File root, Project project) {
|
||||||
|
|
||||||
|
{
|
||||||
|
File config = Tool.createPath(root.getAbsolutePath(), "config");
|
||||||
|
config.mkdirs();
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
File file = Tool.createFile(config.getAbsolutePath(), "TestConfig" + ".java");
|
||||||
|
freeMarkerManager.outputTemp(file, "SpringBootService/test/TestConfig.ftl", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
File file = Tool.createFile(root.getAbsolutePath(), "UtilTest" + ".java");
|
||||||
|
freeMarkerManager.outputTemp(file, "SpringBootService/test/UtilTest.java", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Module md : project.getModules()) {
|
||||||
|
File module = Tool.createPath(root.getAbsolutePath(), md.getModuleName());
|
||||||
|
|
||||||
|
for (Table table : md.getTables()) {
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("module", md);
|
||||||
|
ctx.put("basePackage", md.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", md.getModuleName());
|
||||||
|
ctx.put("table", table);
|
||||||
|
ctx.put("author", md.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
|
||||||
|
File file = Tool.createFile(module.getAbsolutePath(), Tool.lineToClassName(table.getTableName()) + "Test" + ".java");
|
||||||
|
freeMarkerManager.outputTemp(file, "SpringBootService/test/test.ftl", ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.isNeedSys()) {//生成系统模块
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
File system = Tool.createPath(root.getAbsolutePath(), "system");
|
||||||
|
|
||||||
|
for (String apiFile : ResourceUtil.getResourceFiles("/modules/SpringBootService/test/system/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(system.getAbsolutePath(), apiFile), "SpringBootService/test/system/" + apiFile, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{//数据初始化
|
||||||
|
HashMap<String, Object> ctx = new HashMap<String, Object>();
|
||||||
|
ctx.put("tool", Tool.class);
|
||||||
|
ctx.put("basePackage", project.getProjectBasePackage());
|
||||||
|
ctx.put("moduleName", project.getProjectName());
|
||||||
|
ctx.put("author", project.getProjectAuthor());
|
||||||
|
ctx.put("date", new Date());
|
||||||
|
File system = Tool.createPath(root.getAbsolutePath(), "datainit");
|
||||||
|
|
||||||
|
for (String apiFile : ResourceUtil.getResourceFiles("/modules/SpringBootService/test/datainit/")) {
|
||||||
|
freeMarkerManager.outputTemp(Tool.createFile(system.getAbsolutePath(), apiFile), "SpringBootService/test/datainit/" + apiFile, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,191 @@
|
|||||||
|
<?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="${basePackage}.module.${moduleName}.mpr.${table.getCName()}Mapper">
|
||||||
|
|
||||||
|
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<sql id="table">"${module.modulePrefix?default("")}${table.tableName}"</sql>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<sql id="table">`${module.modulePrefix?default("")}${table.tableName}`</sql>
|
||||||
|
</#if>
|
||||||
|
|
||||||
|
<sql id="entityColumnList">
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#list table.fields as f>"${f.fieldName}"<#if f_has_next>,</#if></#list>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#list table.fields as f>`${f.fieldName}`<#if f_has_next>,</#if></#list>
|
||||||
|
</#if>
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<resultMap id="${table.getFName()}" type="${basePackage}.module.${moduleName}.ent.${table.getCName()}">
|
||||||
|
<#list table.fields as f>
|
||||||
|
<result column="${f.fieldName}" jdbcType="${f.fieldType.jdbcType()}" property="${f.getFName()}"/>
|
||||||
|
</#list>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<select id="find" resultMap="${table.getFName()}">
|
||||||
|
SELECT
|
||||||
|
<include refid="entityColumnList"/>
|
||||||
|
FROM
|
||||||
|
<include refid="table"/>
|
||||||
|
WHERE
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isQuery>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="search" resultMap="${table.getFName()}">
|
||||||
|
SELECT
|
||||||
|
<include refid="entityColumnList"/>
|
||||||
|
FROM
|
||||||
|
<include refid="table"/>
|
||||||
|
WHERE
|
||||||
|
1 = 1
|
||||||
|
<#if table.hasSeachKey()>
|
||||||
|
<if test="request.keyword != null and request.keyword != ''">
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isQuery>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
OR "${f.fieldName}" LIKE '%'||${r"#{"}request.keyword}||'%'
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
OR `${f.fieldName}` LIKE CONCAT(CONCAT('%',${r"#{"}request.keyword}),'%')
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
1 = 2
|
||||||
|
</#if>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<insert id="insert">
|
||||||
|
INSERT INTO
|
||||||
|
<include refid="table"/>
|
||||||
|
(
|
||||||
|
<include refid="entityColumnList"/>
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
<#list table.fields as f>
|
||||||
|
${r"#{"}request.${f.getFName()},jdbcType=${f.fieldType.jdbcType()}}<#if f_has_next>,</#if>
|
||||||
|
</#list>
|
||||||
|
)
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<delete id="delete">
|
||||||
|
DELETE FROM
|
||||||
|
<include refid="table"/>
|
||||||
|
WHERE
|
||||||
|
<#if table.hasPrimaryKey()>
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isPrimaryKey>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
1 = 2
|
||||||
|
</#if>
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<update id="update">
|
||||||
|
UPDATE
|
||||||
|
<include refid="table"/>
|
||||||
|
SET
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#list table.fields as f>
|
||||||
|
"${f.fieldName}" = ${r"#{"}request.${f.getFName()},jdbcType=${f.fieldType.jdbcType()}}<#if f_has_next>,</#if>
|
||||||
|
</#list>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#list table.fields as f>
|
||||||
|
`${f.fieldName}` = ${r"#{"}request.${f.getFName()},jdbcType=${f.fieldType.jdbcType()}}<#if f_has_next>,</#if>
|
||||||
|
</#list>
|
||||||
|
</#if>
|
||||||
|
WHERE
|
||||||
|
<#if table.hasPrimaryKey()>
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isPrimaryKey>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
1 = 2
|
||||||
|
</#if>
|
||||||
|
</update>
|
||||||
|
</mapper>
|
@ -0,0 +1,191 @@
|
|||||||
|
<?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="${basePackage}.module.${moduleName}.mpr.${table.getCName()}Mapper">
|
||||||
|
|
||||||
|
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<sql id="table">"${module.modulePrefix?default("")}${table.tableName}"</sql>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<sql id="table">`${module.modulePrefix?default("")}${table.tableName}`</sql>
|
||||||
|
</#if>
|
||||||
|
|
||||||
|
<sql id="entityColumnList">
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#list table.fields as f>"${f.fieldName}"<#if f_has_next>,</#if></#list>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#list table.fields as f>`${f.fieldName}`<#if f_has_next>,</#if></#list>
|
||||||
|
</#if>
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<resultMap id="${table.getFName()}" type="${basePackage}.module.${moduleName}.ent.${table.getCName()}">
|
||||||
|
<#list table.fields as f>
|
||||||
|
<result column="${f.fieldName}" jdbcType="${f.fieldType.jdbcType()}" property="${f.getFName()}"/>
|
||||||
|
</#list>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<select id="find" resultMap="${table.getFName()}">
|
||||||
|
SELECT
|
||||||
|
<include refid="entityColumnList"/>
|
||||||
|
FROM
|
||||||
|
<include refid="table"/>
|
||||||
|
WHERE
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isQuery>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="search" resultMap="${table.getFName()}">
|
||||||
|
SELECT
|
||||||
|
<include refid="entityColumnList"/>
|
||||||
|
FROM
|
||||||
|
<include refid="table"/>
|
||||||
|
WHERE
|
||||||
|
1 = 1
|
||||||
|
<#if table.hasSeachKey()>
|
||||||
|
<if test="request.keyword != null and request.keyword != ''">
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isQuery>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
OR "${f.fieldName}" LIKE '%'||${r"#{"}request.keyword}||'%'
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
OR `${f.fieldName}` LIKE CONCAT(CONCAT('%',${r"#{"}request.keyword}),'%')
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
1 = 2
|
||||||
|
</#if>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<insert id="insert">
|
||||||
|
INSERT INTO
|
||||||
|
<include refid="table"/>
|
||||||
|
(
|
||||||
|
<include refid="entityColumnList"/>
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
<#list table.fields as f>
|
||||||
|
${r"#{"}request.${f.getFName()},jdbcType=${f.fieldType.jdbcType()}}<#if f_has_next>,</#if>
|
||||||
|
</#list>
|
||||||
|
)
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<delete id="delete">
|
||||||
|
DELETE FROM
|
||||||
|
<include refid="table"/>
|
||||||
|
WHERE
|
||||||
|
<#if table.hasPrimaryKey()>
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isPrimaryKey>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
1 = 2
|
||||||
|
</#if>
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<update id="update">
|
||||||
|
UPDATE
|
||||||
|
<include refid="table"/>
|
||||||
|
SET
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#list table.fields as f>
|
||||||
|
"${f.fieldName}" = ${r"#{"}request.${f.getFName()},jdbcType=${f.fieldType.jdbcType()}}<#if f_has_next>,</#if>
|
||||||
|
</#list>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#list table.fields as f>
|
||||||
|
`${f.fieldName}` = ${r"#{"}request.${f.getFName()},jdbcType=${f.fieldType.jdbcType()}}<#if f_has_next>,</#if>
|
||||||
|
</#list>
|
||||||
|
</#if>
|
||||||
|
WHERE
|
||||||
|
<#if table.hasPrimaryKey()>
|
||||||
|
1 = 1
|
||||||
|
<#list table.fields as f>
|
||||||
|
<#if f.isPrimaryKey>
|
||||||
|
<#if dataBase == 'Oracle'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND "${f.fieldName}" = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#elseif dataBase='MySQL'>
|
||||||
|
<#if f.fieldType.javaType() == "String">
|
||||||
|
<if test="request.${f.getFName()} != null and request.${f.getFName()} != ''">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
<#if f.fieldType.javaType() == "Boolean">
|
||||||
|
<if test="request.${f.getFName()} != null">
|
||||||
|
AND `${f.fieldName}` = ${r"#{"}request.${f.getFName()}}
|
||||||
|
</if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
1 = 2
|
||||||
|
</#if>
|
||||||
|
</update>
|
||||||
|
</mapper>
|
@ -0,0 +1,29 @@
|
|||||||
|
package ${basePackage};
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
<#if project.needMoreDB>
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
</#if>
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||||
|
import ${basePackage}.frame.auth.LocalData;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
<#if project.needMoreDB>
|
||||||
|
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
|
||||||
|
<#else>
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
</#if>
|
||||||
|
public class Application extends SpringBootServletInitializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||||
|
return application.sources(Application.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LocalData.setApplicationContext(SpringApplication.run(Application.class, args));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package ${basePackage}.action.api.${module};
|
||||||
|
|
||||||
|
import ${basePackage}.frame.auth.LocalData;
|
||||||
|
import ${basePackage}.module.${module}.mgr.${table.getCName()}Manager;
|
||||||
|
import ${basePackage}.module.${module}.req.*;
|
||||||
|
import ${basePackage}.module.${module}.rsp.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
public class ${table.getCName()}Api{
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ${table.getCName()}Manager ${table.getFName()}Manager;
|
||||||
|
|
||||||
|
<#if table.getCreate()>
|
||||||
|
public ${table.getCName()}CreateResponse create(${table.getCName()}CreateRequest request) {
|
||||||
|
return ${table.getFName()}Manager.create(request, LocalData.getToken());
|
||||||
|
}
|
||||||
|
</#if>
|
||||||
|
<#if table.getDelete()>
|
||||||
|
|
||||||
|
public ${table.getCName()}DeleteResponse delete(${table.getCName()}DeleteRequest request) {
|
||||||
|
return ${table.getFName()}Manager.delete(request, LocalData.getToken());
|
||||||
|
}
|
||||||
|
</#if>
|
||||||
|
<#if table.getUpdate()>
|
||||||
|
|
||||||
|
public ${table.getCName()}UpdateResponse update(${table.getCName()}UpdateRequest request) {
|
||||||
|
return ${table.getFName()}Manager.update(request, LocalData.getToken());
|
||||||
|
}
|
||||||
|
</#if>
|
||||||
|
<#if table.getFind()>
|
||||||
|
|
||||||
|
public ${table.getCName()}FindResponse find(${table.getCName()}FindRequest request) {
|
||||||
|
return ${table.getFName()}Manager.find(request, LocalData.getToken());
|
||||||
|
}
|
||||||
|
</#if>
|
||||||
|
<#if table.getGet()>
|
||||||
|
|
||||||
|
public ${table.getCName()}GetResponse get(${table.getCName()}GetRequest request) {
|
||||||
|
return ${table.getFName()}Manager.get(request, LocalData.getToken());
|
||||||
|
}
|
||||||
|
</#if>
|
||||||
|
<#if table.getSearch()>
|
||||||
|
|
||||||
|
public ${table.getCName()}SearchResponse search(${table.getCName()}SearchRequest request) {
|
||||||
|
return ${table.getFName()}Manager.search(request, LocalData.getToken());
|
||||||
|
}
|
||||||
|
</#if>
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package ${basePackage}.config;
|
||||||
|
|
||||||
|
import net.sf.ehcache.Cache;
|
||||||
|
import net.sf.ehcache.CacheManager;
|
||||||
|
import net.sf.ehcache.config.CacheConfiguration;
|
||||||
|
import net.sf.ehcache.config.DiskStoreConfiguration;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableCaching
|
||||||
|
public class CacheConfig {
|
||||||
|
|
||||||
|
public static final String TOKEN_CACHE = "tokenCache";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public EhCacheCacheManager getCacheManager() {
|
||||||
|
net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
|
||||||
|
// todo 需根据服务器物理内存配置
|
||||||
|
configuration.setMaxBytesLocalHeap("128M");
|
||||||
|
configuration.updateCheck(false);
|
||||||
|
configuration.addDiskStore(new DiskStoreConfiguration().path("java.io.tmpdir"));
|
||||||
|
CacheManager cacheManager = CacheManager.create(configuration);
|
||||||
|
|
||||||
|
// 添加token缓存
|
||||||
|
cacheManager.addCache(buildTokenCache());
|
||||||
|
return new EhCacheCacheManager(cacheManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建TokenCache
|
||||||
|
*
|
||||||
|
* @return 缓存
|
||||||
|
*/
|
||||||
|
private Cache buildTokenCache() {
|
||||||
|
CacheConfiguration config = new CacheConfiguration();
|
||||||
|
config.setMemoryStoreEvictionPolicy("LFU");//最少使用
|
||||||
|
config.setTimeToLiveSeconds(60 * 60);//最长有效时间
|
||||||
|
config.setTimeToIdleSeconds(60 * 60);//无访问最长有效时间
|
||||||
|
config.setName(TOKEN_CACHE);
|
||||||
|
return new Cache(config);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package ${basePackage}.config;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@MapperScan(basePackages = {"${basePackage}.*.mainMpr"}, sqlSessionFactoryRef = "mainSqlSessionFactory")
|
||||||
|
public class MapperMainConfig {
|
||||||
|
@Bean(name = "mainDataSource")
|
||||||
|
@Primary
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.main")
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return DataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "mainTransactionManager")
|
||||||
|
public DataSourceTransactionManager transactionManager(@Qualifier("mainDataSource") DataSource dataSource) {
|
||||||
|
return new DataSourceTransactionManager(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "mainSqlSessionFactory")
|
||||||
|
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("mainDataSource") DataSource basicDataSource) throws Exception {
|
||||||
|
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
||||||
|
factoryBean.setDataSource(basicDataSource);
|
||||||
|
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:**/mainMpr/*.xml"));
|
||||||
|
return factoryBean.getObject();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package ${basePackage}.config;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@MapperScan(basePackages = {"${basePackage}.*.twoMpr"}, sqlSessionFactoryRef = "twoSqlSessionFactory")
|
||||||
|
public class MapperTwoConfig {
|
||||||
|
@Bean(name = "TwoDataSource")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.two")
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return DataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "twoTransactionManager")
|
||||||
|
public DataSourceTransactionManager transactionManager(@Qualifier("twoDataSource") DataSource dataSource) {
|
||||||
|
return new DataSourceTransactionManager(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "twoSqlSessionFactory")
|
||||||
|
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("twoDataSource") DataSource basicDataSource) throws Exception {
|
||||||
|
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
||||||
|
factoryBean.setDataSource(basicDataSource);
|
||||||
|
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:**/twoMpr/*.xml"));
|
||||||
|
return factoryBean.getObject();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package ${basePackage}.config;
|
||||||
|
|
||||||
|
import ${basePackage}.frame.utils.ResourceUtil;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SQLiteConfig {
|
||||||
|
|
||||||
|
@Value("${r'${spring.datasource.url}'}")
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void generateDB() {
|
||||||
|
Pattern compile = Pattern.compile("jdbc:sqlite:(.*.db).*");
|
||||||
|
Matcher matcher = compile.matcher(url);
|
||||||
|
if (matcher.find()) {
|
||||||
|
String group = matcher.group(1);
|
||||||
|
File file = new File(group);
|
||||||
|
if (!file.exists()) {
|
||||||
|
File path = file.getAbsoluteFile().getParentFile();
|
||||||
|
if (!path.exists()) path.mkdirs();
|
||||||
|
ResourceUtil.copyResource2File("${projectName}.db", file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,137 @@
|
|||||||
|
package ${basePackage}.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
|
||||||
|
import ${basePackage}.frame.auth.LocalData;
|
||||||
|
import ${basePackage}.frame.auth.Token;
|
||||||
|
import ${basePackage}.frame.utils.CookieUtil;
|
||||||
|
import ${basePackage}.module.system.mgr.TokensManager;
|
||||||
|
import ${basePackage}.module.system.req.TokensBuildRequest;
|
||||||
|
import ${basePackage}.module.system.rsp.TokensBuildResponse;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableGlobalMethodSecurity(securedEnabled = true)
|
||||||
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Value("${r'${web.url.auth.included}'}")
|
||||||
|
private String[] included;
|
||||||
|
@Value("${r'${web.url.auth.excluded}'}")
|
||||||
|
private String[] excluded;
|
||||||
|
@Value("${r'${spring.mvc.static-path-pattern}'}")
|
||||||
|
private String[] staticPath;
|
||||||
|
@Value("${r'${web.url.login}'}")
|
||||||
|
private String loginPage;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(WebSecurity web) throws Exception {
|
||||||
|
web.ignoring().mvcMatchers(staticPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||||
|
.and()
|
||||||
|
.addFilterBefore(new TokenFilter(), FilterSecurityInterceptor.class)// 过滤器用于处理Token
|
||||||
|
.authorizeRequests()
|
||||||
|
.antMatchers(excluded).permitAll()// 放行排除的URL
|
||||||
|
.antMatchers(included).access("@Authorization.hasPermission(request,authentication)")// 需要权限的URL
|
||||||
|
.and().cors()
|
||||||
|
.and().headers().frameOptions().disable()
|
||||||
|
.and().csrf().disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 此方法不要删除 用于屏蔽默认用户密码生成
|
||||||
|
* <p>
|
||||||
|
* 例如 Using generated security password: f6b42a66-71b1-4c31-b6a8-942838c81408
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
||||||
|
return super.authenticationManagerBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TokenFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||||
|
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||||
|
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
||||||
|
String token = request.getParameter("token");
|
||||||
|
if (token == null || token.isEmpty()) {
|
||||||
|
token = CookieUtil.getCookieValue(request.getCookies(), "token");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组装Token ~ 这边根据实际的业务组装Token
|
||||||
|
if (token != null) {
|
||||||
|
TokensManager tokensManager = LocalData.getBean(TokensManager.class);
|
||||||
|
TokensBuildRequest tokensBuildRequest = new TokensBuildRequest();
|
||||||
|
tokensBuildRequest.setToken(token);
|
||||||
|
TokensBuildResponse tokensBuildResponse = tokensManager.build(tokensBuildRequest, LocalData.getSysToken());
|
||||||
|
LocalData.setToken(tokensBuildResponse.getToken());
|
||||||
|
} else {
|
||||||
|
LocalData.setToken(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Action
|
||||||
|
String servletPath = request.getServletPath().toLowerCase();
|
||||||
|
Pattern compile = Pattern.compile("^/(.+)\\.htm");
|
||||||
|
Matcher matcher = compile.matcher(servletPath);
|
||||||
|
if (matcher.find()) {
|
||||||
|
LocalData.setAction(matcher.group(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
|
} catch (AccessDeniedException e) {
|
||||||
|
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||||
|
} catch (Exception e) {
|
||||||
|
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("Authorization")
|
||||||
|
public Object getAuthorization() {
|
||||||
|
return new Object() {
|
||||||
|
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
|
||||||
|
|
||||||
|
Token token_ = LocalData.getToken();
|
||||||
|
if (token_ == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String path = request.getServletPath();
|
||||||
|
if (token_.hasRes(path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
package ${basePackage}.frame.auth;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
import ${basePackage}.frame.auth.Token;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LocalData - 本地数据存放类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class LocalData {
|
||||||
|
|
||||||
|
private static ApplicationContext applicationContext = null;
|
||||||
|
|
||||||
|
private static Token system = null;
|
||||||
|
|
||||||
|
static {
|
||||||
|
// 组装系统Token
|
||||||
|
system = new Token();
|
||||||
|
system.setId(0);
|
||||||
|
system.setUserId(0);
|
||||||
|
system.setUserName("system");
|
||||||
|
system.putRes(".*");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Token getSysToken() {
|
||||||
|
return system;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当请求目标 target = '/aa/bb'
|
||||||
|
*/
|
||||||
|
private static final ThreadLocal<String> actionHolder = new ThreadLocal();
|
||||||
|
|
||||||
|
public static String getAction() {
|
||||||
|
return actionHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setAction(String action) {
|
||||||
|
actionHolder.set(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前用户的通行证
|
||||||
|
*/
|
||||||
|
private static final ThreadLocal<Token> tokenHolder = new ThreadLocal();
|
||||||
|
|
||||||
|
public static Token getToken() {
|
||||||
|
return tokenHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setToken(Token token) {
|
||||||
|
tokenHolder.set(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HttpServletRequest getRequest() {
|
||||||
|
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HttpServletResponse getResponse() {
|
||||||
|
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApplicationContext getApplicationContext() {
|
||||||
|
return LocalData.applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setApplicationContext(ApplicationContext applicationContext) {
|
||||||
|
LocalData.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getBean(Class<T> t) {
|
||||||
|
if (getApplicationContext() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return getApplicationContext().getBean(t);
|
||||||
|
} catch (BeansException ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Environment getEnvironment() {
|
||||||
|
return getBean(Environment.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
package ${basePackage}.frame.auth;
|
||||||
|
|
||||||
|
import ${basePackage}.frame.utils.IDgenerator;
|
||||||
|
import ${basePackage}.module.system.ent.Res;
|
||||||
|
import ${basePackage}.module.system.mgr.ResManager;
|
||||||
|
import ${basePackage}.module.system.req.ResCreateRequest;
|
||||||
|
import ${basePackage}.module.system.req.ResFindRequest;
|
||||||
|
import ${basePackage}.module.system.rsp.ResFindResponse;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token - 通行证类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class Token implements Serializable {
|
||||||
|
private static final Long serialVersionUID = 1L;
|
||||||
|
/**
|
||||||
|
* ID
|
||||||
|
*/
|
||||||
|
private long id;
|
||||||
|
/**
|
||||||
|
* TOKEN
|
||||||
|
*/
|
||||||
|
private String token;
|
||||||
|
/**
|
||||||
|
* 用户ID
|
||||||
|
*/
|
||||||
|
private long userId;
|
||||||
|
/**
|
||||||
|
* 用户名称
|
||||||
|
*/
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
private Set<String> resSet = new HashSet<>();
|
||||||
|
|
||||||
|
public boolean hasRes(String res) {
|
||||||
|
|
||||||
|
{// todo 开发初期收集资源,后期删除
|
||||||
|
String[] profiles = LocalData.getEnvironment().getActiveProfiles();
|
||||||
|
for (String profile : profiles) {
|
||||||
|
if (profile.contains("dev")) {//测试环境捕获资源
|
||||||
|
ResFindRequest resFindRequest = new ResFindRequest();
|
||||||
|
resFindRequest.setPageSize(0);
|
||||||
|
resFindRequest.setResValue(res);
|
||||||
|
ResManager resManager = LocalData.getBean(ResManager.class);
|
||||||
|
ResFindResponse resFindResponse = resManager.find(resFindRequest, LocalData.getSysToken());
|
||||||
|
|
||||||
|
if (resFindResponse.getResult().size() == 0) {//资源不存在,主动收集资源
|
||||||
|
ResCreateRequest resCreateRequest = new ResCreateRequest();
|
||||||
|
resCreateRequest.setResCode(IDgenerator.nextUUID());
|
||||||
|
resCreateRequest.setResName("默认");
|
||||||
|
resCreateRequest.setResValue(res);
|
||||||
|
resCreateRequest.setValid(true);
|
||||||
|
resCreateRequest.setResType("");
|
||||||
|
resManager.create(resCreateRequest, LocalData.getSysToken());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String s : resSet) {
|
||||||
|
if (res.matches(s)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putRes(String resource) {
|
||||||
|
resSet.add(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putRes(Set<String> resourceSet) {
|
||||||
|
this.resSet.addAll(resourceSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putRes(List<Res> resList) {
|
||||||
|
if (resList == null) return;
|
||||||
|
for (Res res : resList) {
|
||||||
|
this.resSet.add(res.getResValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getResSet() {
|
||||||
|
return resSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getToken() {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setToken(String token) {
|
||||||
|
this.token = token;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package ${basePackage}.frame.auth;
|
||||||
|
|
||||||
|
import ${basePackage}.frame.validation.DictValidator;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Constraint(validatedBy = DictValidator.class)
|
||||||
|
public @interface Verification {
|
||||||
|
String name() default "";
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base - 基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class BaseEntity implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*/
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 行版本
|
||||||
|
*/
|
||||||
|
private long rowVersion;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建用户
|
||||||
|
*/
|
||||||
|
@JsonIgnore
|
||||||
|
private long createBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最后更新用户
|
||||||
|
*/
|
||||||
|
@JsonIgnore
|
||||||
|
private long lastUpdateBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最后更新时间
|
||||||
|
*/
|
||||||
|
@JsonIgnore
|
||||||
|
private Date lastUpdateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否删除
|
||||||
|
*/
|
||||||
|
@JsonIgnore
|
||||||
|
private boolean isDeleted;
|
||||||
|
|
||||||
|
public long getRowVersion() {
|
||||||
|
return rowVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRowVersion(long rowVersion) {
|
||||||
|
this.rowVersion = rowVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreateBy() {
|
||||||
|
return createBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateBy(long createBy) {
|
||||||
|
this.createBy = createBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(Date createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLastUpdateBy() {
|
||||||
|
return lastUpdateBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastUpdateBy(long lastUpdateBy) {
|
||||||
|
this.lastUpdateBy = lastUpdateBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getLastUpdateTime() {
|
||||||
|
return lastUpdateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastUpdateTime(Date lastUpdateTime) {
|
||||||
|
this.lastUpdateTime = lastUpdateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getIsDeleted() {
|
||||||
|
return isDeleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsDeleted(boolean deleted) {
|
||||||
|
isDeleted = deleted;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BaseFindRequest - 基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class BaseFindRequest extends BaseRequest {
|
||||||
|
|
||||||
|
private int pageNumber = 1;
|
||||||
|
|
||||||
|
private int pageSize = 10;
|
||||||
|
|
||||||
|
private String sortKey;
|
||||||
|
|
||||||
|
private SortType sortType;
|
||||||
|
|
||||||
|
public int getPageNumber() {
|
||||||
|
return pageNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPageNumber(int pageNumber) {
|
||||||
|
this.pageNumber = pageNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPageSize() {
|
||||||
|
return pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPageSize(int pageSize) {
|
||||||
|
this.pageSize = pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSortKey() {
|
||||||
|
return sortKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSortKey(String sortKey) {
|
||||||
|
this.sortKey = sortKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SortType getSortType() {
|
||||||
|
return sortType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSortType(SortType sortType) {
|
||||||
|
this.sortType = sortType;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BaseFindResponse - 基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class BaseFindResponse<T> extends BaseResponse{
|
||||||
|
|
||||||
|
private List<T> result;
|
||||||
|
|
||||||
|
private Long totalCount;
|
||||||
|
|
||||||
|
public List<T> getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResult(List<T> result) {
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getTotalCount() {
|
||||||
|
return totalCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalCount(Long totalCount) {
|
||||||
|
this.totalCount = totalCount;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BaseRequest - 基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class BaseRequest {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BaseSearchRequest - 基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class BaseSearchRequest extends BaseFindRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模糊查询的关键字。
|
||||||
|
*/
|
||||||
|
private String keyword;
|
||||||
|
|
||||||
|
public String getKeyword() {
|
||||||
|
return keyword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKeyword(String keyword) {
|
||||||
|
this.keyword = keyword;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BaseUpdateRequest - 基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class BaseUpdateRequest extends BaseRequest{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 版本戳
|
||||||
|
*/
|
||||||
|
private long rowVersion;
|
||||||
|
|
||||||
|
public long getRowVersion() {
|
||||||
|
return rowVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRowVersion(long rowVersion) {
|
||||||
|
this.rowVersion = rowVersion;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public abstract class Control {
|
||||||
|
public abstract void exec(Model model, HttpServletRequest request, HttpServletResponse response);
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error - 错误基类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class Error {
|
||||||
|
|
||||||
|
/*错误类型*/
|
||||||
|
private ErrorType type;
|
||||||
|
|
||||||
|
/*错误内容*/
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public Error() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Error(ErrorType type, String message) {
|
||||||
|
this.type = type;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(ErrorType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ErrorType - 错误类型
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public enum ErrorType {
|
||||||
|
BUSINESS_ERROR,
|
||||||
|
SYSTEM_ERROR,
|
||||||
|
UNKNOWN_ERROR,
|
||||||
|
UNIQUENESS_ERROR,
|
||||||
|
EXPECTATION_NULL,
|
||||||
|
INVALID_PARAMETER
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public abstract class Screen {
|
||||||
|
public abstract void exec(Model model, HttpServletRequest request, HttpServletResponse response);
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package ${basePackage}.frame.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SortTypeEnum - 排序方式
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public enum SortType {
|
||||||
|
|
||||||
|
//升序
|
||||||
|
ASC,
|
||||||
|
|
||||||
|
//降序
|
||||||
|
DESC
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package ${basePackage}.frame.excel.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ColumnDescription - Excel列描述注解
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface ColumnDescription {
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package ${basePackage}.frame.excel.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ColumnList - 单元格下拉选项
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
@Target({ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface ColumnList {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下拉列表
|
||||||
|
*/
|
||||||
|
String[] value();
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package ${basePackage}.frame.excel.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converter - Excel列转化器注解
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface Convert {
|
||||||
|
Class target();
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package ${basePackage}.frame.excel.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excel文件读取失败异常
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ReadErrorException extends Exception {
|
||||||
|
public ReadErrorException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadErrorException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package ${basePackage}.frame.excel.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excel文件与模板不匹配时异常
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class TemplateNotMatchException extends Exception {
|
||||||
|
public TemplateNotMatchException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public TemplateNotMatchException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package ${basePackage}.frame.excel.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 值转换异常
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ValueConverterException extends Exception {
|
||||||
|
|
||||||
|
public ValueConverterException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created on 2015/1/29.
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class BaseCellStyle {
|
||||||
|
/**
|
||||||
|
* 样式
|
||||||
|
*/
|
||||||
|
protected CellStyle style;
|
||||||
|
|
||||||
|
public BaseCellStyle(Workbook workbook) {
|
||||||
|
style = workbook.createCellStyle();
|
||||||
|
style.setFillPattern(CellStyle.NO_FILL);
|
||||||
|
//下边框
|
||||||
|
style.setBorderBottom(CellStyle.SOLID_FOREGROUND);
|
||||||
|
//下边框颜色
|
||||||
|
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||||
|
//左边框
|
||||||
|
style.setBorderLeft(CellStyle.SOLID_FOREGROUND);
|
||||||
|
//左边框颜色
|
||||||
|
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
||||||
|
//右边框
|
||||||
|
style.setBorderRight(CellStyle.SOLID_FOREGROUND);
|
||||||
|
//右边框颜色
|
||||||
|
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
||||||
|
//上边框
|
||||||
|
style.setBorderTop(CellStyle.SOLID_FOREGROUND);
|
||||||
|
//上边框颜色
|
||||||
|
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
||||||
|
|
||||||
|
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //上下居中
|
||||||
|
style.setBorderBottom(CellStyle.SOLID_FOREGROUND); //下边框
|
||||||
|
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色
|
||||||
|
style.setBorderLeft(CellStyle.SOLID_FOREGROUND); //左边框
|
||||||
|
style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); //左边框颜色
|
||||||
|
style.setBorderRight(CellStyle.SOLID_FOREGROUND); //右边框
|
||||||
|
style.setRightBorderColor(IndexedColors.BLACK.getIndex()); //右边框颜色
|
||||||
|
style.setBorderTop(CellStyle.SOLID_FOREGROUND); //上边框
|
||||||
|
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色
|
||||||
|
}
|
||||||
|
|
||||||
|
public CellStyle getStyle() {
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStyle(CellStyle style) {
|
||||||
|
this.style = style;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.Font;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created on 2015/1/29.
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class BaseFont {
|
||||||
|
/**
|
||||||
|
* 字体
|
||||||
|
*/
|
||||||
|
protected Font font;
|
||||||
|
|
||||||
|
public BaseFont(Workbook workbook) {
|
||||||
|
font = workbook.createFont();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Font getFont() {
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFont(Font font) {
|
||||||
|
this.font = font;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
public class DataCellStyle extends BaseCellStyle {
|
||||||
|
|
||||||
|
public DataCellStyle(Workbook workbook, short align, boolean error) {
|
||||||
|
super(workbook);
|
||||||
|
style.setAlignment(align);
|
||||||
|
if (error) {
|
||||||
|
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
|
||||||
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataCellStyle(Workbook workbook, short align) {
|
||||||
|
this(workbook, align, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataCellStyle(Workbook workbook, boolean error) {
|
||||||
|
this(workbook, CellStyle.ALIGN_LEFT, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataCellStyle(Workbook workbook) {
|
||||||
|
this(workbook, CellStyle.ALIGN_LEFT, false);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
public class ErrorCellStyle extends BaseCellStyle {
|
||||||
|
|
||||||
|
public ErrorCellStyle(Workbook workbook) {
|
||||||
|
super(workbook);
|
||||||
|
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
|
||||||
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
|
||||||
|
}
|
||||||
|
|
||||||
|
public CellStyle getStyle() {
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStyle(CellStyle style) {
|
||||||
|
this.style = style;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
public class HeadCellStyle extends BaseCellStyle {
|
||||||
|
public HeadCellStyle(Workbook workbook) {
|
||||||
|
super(workbook);
|
||||||
|
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); //背景颜色-灰色
|
||||||
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式
|
||||||
|
style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
|
||||||
|
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 普通字体.颜色黑
|
||||||
|
* Created on 2015/1/29.
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class NormalFont extends BaseFont {
|
||||||
|
public NormalFont(Workbook workbook) {
|
||||||
|
super(workbook);
|
||||||
|
font.setColor(HSSFColor.BLACK.index); //字体颜色-黑色
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 普通字体.颜色黑
|
||||||
|
* Created on 2015/1/29.
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class RedFont extends BaseFont {
|
||||||
|
public RedFont(Workbook workbook) {
|
||||||
|
super(workbook);
|
||||||
|
font.setColor(HSSFColor.RED.index); //字体颜色-黑色
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package ${basePackage}.frame.excel.style;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
public class SuccessCellStyle extends BaseCellStyle {
|
||||||
|
|
||||||
|
public SuccessCellStyle(Workbook workbook) {
|
||||||
|
super(workbook);
|
||||||
|
style.setFillForegroundColor(HSSFColor.GREEN.index); //背景颜色红色
|
||||||
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
|
||||||
|
}
|
||||||
|
|
||||||
|
public CellStyle getStyle() {
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStyle(CellStyle style) {
|
||||||
|
this.style = style;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ${basePackage}.frame.schedule;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
import org.springframework.scheduling.support.CronTrigger;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
|
public abstract class RunCronTask extends RunTask {
|
||||||
|
|
||||||
|
abstract String cron();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||||
|
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||||
|
return poolTaskScheduler.schedule(this, new CronTrigger(cron()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ${basePackage}.frame.schedule;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
|
public abstract class RunDelayRepeatTask extends RunTask {
|
||||||
|
|
||||||
|
public abstract Duration interval();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||||
|
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||||
|
return poolTaskScheduler.scheduleWithFixedDelay(this, interval());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ${basePackage}.frame.schedule;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
|
public abstract class RunFixRepeatTask extends RunTask {
|
||||||
|
|
||||||
|
public abstract Duration interval();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||||
|
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||||
|
return poolTaskScheduler.scheduleAtFixedRate(this, interval());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package ${basePackage}.frame.schedule;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSession;
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import ${basePackage}.frame.auth.LocalData;
|
||||||
|
import ${basePackage}.frame.utils.LogUtil;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public abstract class RunSqlTask extends RunFixRepeatTask {
|
||||||
|
|
||||||
|
private Connection connection;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
if (connection == null || connection.isClosed()) {
|
||||||
|
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
|
||||||
|
SqlSession sqlSession = factory.openSession(true);
|
||||||
|
connection = sqlSession.getConnection();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LogUtil.e("schedule: get connection failed!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(getSql());
|
||||||
|
preparedStatement.execute();
|
||||||
|
preparedStatement.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LogUtil.e("RunSqlTask exec failed! SQL:[" + getSql() + "]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getSql();
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package ${basePackage}.frame.schedule;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
|
public abstract class RunTask implements Runnable {
|
||||||
|
|
||||||
|
public abstract String taskId();
|
||||||
|
|
||||||
|
public abstract ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler);
|
||||||
|
|
||||||
|
public void configChange(ThreadPoolTaskScheduler scheduler) {
|
||||||
|
ScheduledFuture<?> schedule = scheduler.schedule(this, new Date());
|
||||||
|
if (!schedule.cancel(true)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AESUtil 对称加密和解密工具类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class AESUtil {
|
||||||
|
|
||||||
|
private static final String ALGORITHM = "AES";
|
||||||
|
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 待加密字节数组
|
||||||
|
* @param secret 密钥
|
||||||
|
* @return base64字符串
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(byte[] data, String secret) {
|
||||||
|
try {
|
||||||
|
if (secret.length() != 16) {
|
||||||
|
throw new IllegalArgumentException("secret's length is not 16");
|
||||||
|
}
|
||||||
|
SecretKeySpec key = new SecretKeySpec(secret.getBytes(), ALGORITHM);
|
||||||
|
Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
|
||||||
|
return cipher.doFinal(data);// 加密
|
||||||
|
} catch (NoSuchPaddingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvalidKeyException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalBlockSizeException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (BadPaddingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 待加密字节数组
|
||||||
|
* @param secret 密钥
|
||||||
|
* @return base64字符串
|
||||||
|
*/
|
||||||
|
public static String encrypt2Base64(byte[] data, String secret) {
|
||||||
|
byte[] encrypt = encrypt(data, secret);
|
||||||
|
return Base64Util.encodeToString(encrypt, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param data 待解密字节数组
|
||||||
|
* @param secret 密钥
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(byte[] data, String secret) {
|
||||||
|
try {
|
||||||
|
if (secret.length() != 16) {
|
||||||
|
throw new IllegalArgumentException("secret's length is not 16");
|
||||||
|
}
|
||||||
|
SecretKeySpec key = new SecretKeySpec(secret.getBytes(), ALGORITHM);
|
||||||
|
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||||
|
return cipher.doFinal(data);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (NoSuchPaddingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvalidKeyException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalBlockSizeException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (BadPaddingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] decryptBase64(String base64Data, String secret) {
|
||||||
|
byte[] decode = Base64Util.decode(base64Data);
|
||||||
|
return decrypt(decode, secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decrypt2String(String base64Data, String secret) {
|
||||||
|
byte[] bytes = decryptBase64(base64Data, secret);
|
||||||
|
try {
|
||||||
|
return new String(bytes, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,497 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base64Util
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class Base64Util {
|
||||||
|
private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
|
||||||
|
private static final int[] IA = new int[256];
|
||||||
|
|
||||||
|
static {
|
||||||
|
Arrays.fill(IA, -1);
|
||||||
|
for (int i = 0, iS = CA.length; i < iS; i++)
|
||||||
|
IA[CA[i]] = i;
|
||||||
|
IA['='] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static String encodeToString(byte[] sArr) {
|
||||||
|
return new String(encodeToChar(sArr, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static char[] encodeToChar(byte[] sArr, boolean lineSep) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = sArr != null ? sArr.length : 0;
|
||||||
|
if (sLen == 0)
|
||||||
|
return new char[0];
|
||||||
|
|
||||||
|
int eLen = (sLen / 3) * 3; // Length of even 24-bits.
|
||||||
|
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
|
||||||
|
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
|
||||||
|
char[] dArr = new char[dLen];
|
||||||
|
|
||||||
|
// Encode even 24-bits
|
||||||
|
for (int s = 0, d = 0, cc = 0; s < eLen; ) {
|
||||||
|
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
|
||||||
|
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
|
||||||
|
|
||||||
|
// Encode the int into four chars
|
||||||
|
dArr[d++] = CA[(i >>> 18) & 0x3f];
|
||||||
|
dArr[d++] = CA[(i >>> 12) & 0x3f];
|
||||||
|
dArr[d++] = CA[(i >>> 6) & 0x3f];
|
||||||
|
dArr[d++] = CA[i & 0x3f];
|
||||||
|
|
||||||
|
// Add optional line separator
|
||||||
|
if (lineSep && ++cc == 19 && d < dLen - 2) {
|
||||||
|
dArr[d++] = '\r';
|
||||||
|
dArr[d++] = '\n';
|
||||||
|
cc = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pad and encode last bits if source isn't even 24 bits.
|
||||||
|
int left = sLen - eLen; // 0 - 2.
|
||||||
|
if (left > 0) {
|
||||||
|
// Prepare the int
|
||||||
|
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
|
||||||
|
|
||||||
|
// Set last four chars
|
||||||
|
dArr[dLen - 4] = CA[i >> 12];
|
||||||
|
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
|
||||||
|
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
|
||||||
|
dArr[dLen - 1] = '=';
|
||||||
|
}
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045.
|
||||||
|
*
|
||||||
|
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
|
||||||
|
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
|
||||||
|
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
|
||||||
|
* little faster.
|
||||||
|
* @return A BASE64 encoded array. Never <code>null</code>.
|
||||||
|
*/
|
||||||
|
public final static String encodeToString(byte[] sArr, boolean lineSep) {
|
||||||
|
// Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.
|
||||||
|
return new String(encodeToChar(sArr, lineSep));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes a raw byte array into a BASE64 <code>byte[]</code> representation i accordance with RFC 2045.
|
||||||
|
*
|
||||||
|
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
|
||||||
|
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
|
||||||
|
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
|
||||||
|
* little faster.
|
||||||
|
* @return A BASE64 encoded array. Never <code>null</code>.
|
||||||
|
*/
|
||||||
|
public final static byte[] encodeToByte(byte[] sArr, boolean lineSep) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = sArr != null ? sArr.length : 0;
|
||||||
|
if (sLen == 0)
|
||||||
|
return new byte[0];
|
||||||
|
|
||||||
|
int eLen = (sLen / 3) * 3; // Length of even 24-bits.
|
||||||
|
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
|
||||||
|
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
|
||||||
|
byte[] dArr = new byte[dLen];
|
||||||
|
|
||||||
|
// Encode even 24-bits
|
||||||
|
for (int s = 0, d = 0, cc = 0; s < eLen; ) {
|
||||||
|
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
|
||||||
|
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
|
||||||
|
|
||||||
|
// Encode the int into four chars
|
||||||
|
dArr[d++] = (byte) CA[(i >>> 18) & 0x3f];
|
||||||
|
dArr[d++] = (byte) CA[(i >>> 12) & 0x3f];
|
||||||
|
dArr[d++] = (byte) CA[(i >>> 6) & 0x3f];
|
||||||
|
dArr[d++] = (byte) CA[i & 0x3f];
|
||||||
|
|
||||||
|
// Add optional line separator
|
||||||
|
if (lineSep && ++cc == 19 && d < dLen - 2) {
|
||||||
|
dArr[d++] = '\r';
|
||||||
|
dArr[d++] = '\n';
|
||||||
|
cc = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pad and encode last bits if source isn't an even 24 bits.
|
||||||
|
int left = sLen - eLen; // 0 - 2.
|
||||||
|
if (left > 0) {
|
||||||
|
// Prepare the int
|
||||||
|
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
|
||||||
|
|
||||||
|
// Set last four chars
|
||||||
|
dArr[dLen - 4] = (byte) CA[i >> 12];
|
||||||
|
dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f];
|
||||||
|
dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '=';
|
||||||
|
dArr[dLen - 1] = '=';
|
||||||
|
}
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static byte[] decode(char[] sArr) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = sArr != null ? sArr.length : 0;
|
||||||
|
if (sLen == 0)
|
||||||
|
return new byte[0];
|
||||||
|
|
||||||
|
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
|
||||||
|
// so we don't have to reallocate & copy it later.
|
||||||
|
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
|
||||||
|
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
|
||||||
|
if (IA[sArr[i]] < 0)
|
||||||
|
sepCnt++;
|
||||||
|
|
||||||
|
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
|
||||||
|
if ((sLen - sepCnt) % 4 != 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
int pad = 0;
|
||||||
|
for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0; )
|
||||||
|
if (sArr[i] == '=')
|
||||||
|
pad++;
|
||||||
|
|
||||||
|
int len = ((sLen - sepCnt) * 6 >> 3) - pad;
|
||||||
|
|
||||||
|
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||||
|
|
||||||
|
for (int s = 0, d = 0; d < len; ) {
|
||||||
|
// Assemble three bytes into an int from four "valid" characters.
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found.
|
||||||
|
int c = IA[sArr[s++]];
|
||||||
|
if (c >= 0)
|
||||||
|
i |= c << (18 - j * 6);
|
||||||
|
else
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
// Add the bytes
|
||||||
|
dArr[d++] = (byte) (i >> 16);
|
||||||
|
if (d < len) {
|
||||||
|
dArr[d++] = (byte) (i >> 8);
|
||||||
|
if (d < len)
|
||||||
|
dArr[d++] = (byte) i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with
|
||||||
|
* and without line separators.
|
||||||
|
*
|
||||||
|
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||||
|
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
|
||||||
|
* (including '=') isn't divideable by 4. (I.e. definitely corrupted).
|
||||||
|
*/
|
||||||
|
public final static byte[] decode(byte[] sArr) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = sArr.length;
|
||||||
|
|
||||||
|
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
|
||||||
|
// so we don't have to reallocate & copy it later.
|
||||||
|
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
|
||||||
|
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
|
||||||
|
if (IA[sArr[i] & 0xff] < 0)
|
||||||
|
sepCnt++;
|
||||||
|
|
||||||
|
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
|
||||||
|
if ((sLen - sepCnt) % 4 != 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
int pad = 0;
|
||||||
|
for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0; )
|
||||||
|
if (sArr[i] == '=')
|
||||||
|
pad++;
|
||||||
|
|
||||||
|
int len = ((sLen - sepCnt) * 6 >> 3) - pad;
|
||||||
|
|
||||||
|
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||||
|
|
||||||
|
for (int s = 0, d = 0; d < len; ) {
|
||||||
|
// Assemble three bytes into an int from four "valid" characters.
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found.
|
||||||
|
int c = IA[sArr[s++] & 0xff];
|
||||||
|
if (c >= 0)
|
||||||
|
i |= c << (18 - j * 6);
|
||||||
|
else
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the bytes
|
||||||
|
dArr[d++] = (byte) (i >> 16);
|
||||||
|
if (d < len) {
|
||||||
|
dArr[d++] = (byte) (i >> 8);
|
||||||
|
if (d < len)
|
||||||
|
dArr[d++] = (byte) i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as
|
||||||
|
* fast as {@link =#=decode(byte[])}. The preconditions are:<br>
|
||||||
|
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
|
||||||
|
* + Line separator must be "\r\n", as specified in RFC 2045
|
||||||
|
* + The array must not contain illegal characters within the encoded string<br>
|
||||||
|
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
|
||||||
|
*
|
||||||
|
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||||
|
* @return The decoded array of bytes. May be of length 0.
|
||||||
|
*/
|
||||||
|
public final static byte[] decodeFast(byte[] sArr) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = sArr.length;
|
||||||
|
if (sLen == 0)
|
||||||
|
return new byte[0];
|
||||||
|
|
||||||
|
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
|
||||||
|
|
||||||
|
// Trim illegal chars from start
|
||||||
|
while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0)
|
||||||
|
sIx++;
|
||||||
|
|
||||||
|
// Trim illegal chars from end
|
||||||
|
while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0)
|
||||||
|
eIx--;
|
||||||
|
|
||||||
|
// get the padding count (=) (0, 1 or 2)
|
||||||
|
int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.
|
||||||
|
int cCnt = eIx - sIx + 1; // Content count including possible separators
|
||||||
|
int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
|
||||||
|
|
||||||
|
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
|
||||||
|
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||||
|
|
||||||
|
// Decode all but the last 0 - 2 bytes.
|
||||||
|
int d = 0;
|
||||||
|
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
|
||||||
|
// Assemble three bytes into an int from four "valid" characters.
|
||||||
|
int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
|
||||||
|
|
||||||
|
// Add the bytes
|
||||||
|
dArr[d++] = (byte) (i >> 16);
|
||||||
|
dArr[d++] = (byte) (i >> 8);
|
||||||
|
dArr[d++] = (byte) i;
|
||||||
|
|
||||||
|
// If line separator, jump over it.
|
||||||
|
if (sepCnt > 0 && ++cc == 19) {
|
||||||
|
sIx += 2;
|
||||||
|
cc = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d < len) {
|
||||||
|
// Decode last 1-3 bytes (incl '=') into 1-3 bytes
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; sIx <= eIx - pad; j++)
|
||||||
|
i |= IA[sArr[sIx++]] << (18 - j * 6);
|
||||||
|
|
||||||
|
for (int r = 16; d < len; r -= 8)
|
||||||
|
dArr[d++] = (byte) (i >> r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as
|
||||||
|
* fast as {@link =#=decode(char[])}. The preconditions are:<br>
|
||||||
|
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
|
||||||
|
* + Line separator must be "\r\n", as specified in RFC 2045
|
||||||
|
* + The array must not contain illegal characters within the encoded string<br>
|
||||||
|
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
|
||||||
|
*
|
||||||
|
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||||
|
* @return The decoded array of bytes. May be of length 0.
|
||||||
|
*/
|
||||||
|
public final static byte[] decodeFast(char[] sArr) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = sArr.length;
|
||||||
|
if (sLen == 0)
|
||||||
|
return new byte[0];
|
||||||
|
|
||||||
|
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
|
||||||
|
|
||||||
|
// Trim illegal chars from start
|
||||||
|
while (sIx < eIx && IA[sArr[sIx]] < 0)
|
||||||
|
sIx++;
|
||||||
|
|
||||||
|
// Trim illegal chars from end
|
||||||
|
while (eIx > 0 && IA[sArr[eIx]] < 0)
|
||||||
|
eIx--;
|
||||||
|
|
||||||
|
// get the padding count (=) (0, 1 or 2)
|
||||||
|
int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.
|
||||||
|
int cCnt = eIx - sIx + 1; // Content count including possible separators
|
||||||
|
int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
|
||||||
|
|
||||||
|
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
|
||||||
|
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||||
|
|
||||||
|
// Decode all but the last 0 - 2 bytes.
|
||||||
|
int d = 0;
|
||||||
|
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
|
||||||
|
// Assemble three bytes into an int from four "valid" characters.
|
||||||
|
int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
|
||||||
|
|
||||||
|
// Add the bytes
|
||||||
|
dArr[d++] = (byte) (i >> 16);
|
||||||
|
dArr[d++] = (byte) (i >> 8);
|
||||||
|
dArr[d++] = (byte) i;
|
||||||
|
|
||||||
|
// If line separator, jump over it.
|
||||||
|
if (sepCnt > 0 && ++cc == 19) {
|
||||||
|
sIx += 2;
|
||||||
|
cc = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d < len) {
|
||||||
|
// Decode last 1-3 bytes (incl '=') into 1-3 bytes
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; sIx <= eIx - pad; j++)
|
||||||
|
i |= IA[sArr[sIx++]] << (18 - j * 6);
|
||||||
|
|
||||||
|
for (int r = 16; d < len; r -= 8)
|
||||||
|
dArr[d++] = (byte) (i >> r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a BASE64 encoded <code>String</code>. All illegal characters will be ignored and can handle both strings with
|
||||||
|
* and without line separators.<br>
|
||||||
|
* <b>Note!</b> It can be up to about 2x the speed to call <code>decode(str.toCharArray())</code> instead. That
|
||||||
|
* will create a temporary array though. This version will use <code>str.charAt(i)</code> to iterate the string.
|
||||||
|
*
|
||||||
|
* @param str The source string. <code>null</code> or length 0 will return an empty array.
|
||||||
|
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
|
||||||
|
* (including '=') isn't divideable by 4. (I.e. definitely corrupted).
|
||||||
|
*/
|
||||||
|
public final static byte[] decode(String str) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = str != null ? str.length() : 0;
|
||||||
|
if (sLen == 0)
|
||||||
|
return new byte[0];
|
||||||
|
|
||||||
|
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
|
||||||
|
// so we don't have to reallocate & copy it later.
|
||||||
|
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
|
||||||
|
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
|
||||||
|
if (IA[str.charAt(i)] < 0)
|
||||||
|
sepCnt++;
|
||||||
|
|
||||||
|
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
|
||||||
|
if ((sLen - sepCnt) % 4 != 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Count '=' at end
|
||||||
|
int pad = 0;
|
||||||
|
for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0; )
|
||||||
|
if (str.charAt(i) == '=')
|
||||||
|
pad++;
|
||||||
|
|
||||||
|
int len = ((sLen - sepCnt) * 6 >> 3) - pad;
|
||||||
|
|
||||||
|
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||||
|
|
||||||
|
for (int s = 0, d = 0; d < len; ) {
|
||||||
|
// Assemble three bytes into an int from four "valid" characters.
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found.
|
||||||
|
int c = IA[str.charAt(s++)];
|
||||||
|
if (c >= 0)
|
||||||
|
i |= c << (18 - j * 6);
|
||||||
|
else
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
// Add the bytes
|
||||||
|
dArr[d++] = (byte) (i >> 16);
|
||||||
|
if (d < len) {
|
||||||
|
dArr[d++] = (byte) (i >> 8);
|
||||||
|
if (d < len)
|
||||||
|
dArr[d++] = (byte) i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as
|
||||||
|
* fast as {@link =#=decode(String)}. The preconditions are:<br>
|
||||||
|
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
|
||||||
|
* + Line separator must be "\r\n", as specified in RFC 2045
|
||||||
|
* + The array must not contain illegal characters within the encoded string<br>
|
||||||
|
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
|
||||||
|
*
|
||||||
|
* @param str The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||||
|
* @return The decoded array of bytes. May be of length 0.
|
||||||
|
*/
|
||||||
|
public final static byte[] decodeFast(String str) {
|
||||||
|
// Check special case
|
||||||
|
int sLen = str.length();
|
||||||
|
if (sLen == 0)
|
||||||
|
return new byte[0];
|
||||||
|
|
||||||
|
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
|
||||||
|
|
||||||
|
// Trim illegal chars from start
|
||||||
|
while (sIx < eIx && IA[str.charAt(sIx) & 0xff] < 0)
|
||||||
|
sIx++;
|
||||||
|
|
||||||
|
// Trim illegal chars from end
|
||||||
|
while (eIx > 0 && IA[str.charAt(eIx) & 0xff] < 0)
|
||||||
|
eIx--;
|
||||||
|
|
||||||
|
// get the padding count (=) (0, 1 or 2)
|
||||||
|
int pad = str.charAt(eIx) == '=' ? (str.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.
|
||||||
|
int cCnt = eIx - sIx + 1; // Content count including possible separators
|
||||||
|
int sepCnt = sLen > 76 ? (str.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;
|
||||||
|
|
||||||
|
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
|
||||||
|
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||||
|
|
||||||
|
// Decode all but the last 0 - 2 bytes.
|
||||||
|
int d = 0;
|
||||||
|
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
|
||||||
|
// Assemble three bytes into an int from four "valid" characters.
|
||||||
|
int i = IA[str.charAt(sIx++)] << 18 | IA[str.charAt(sIx++)] << 12 | IA[str.charAt(sIx++)] << 6 | IA[str.charAt(sIx++)];
|
||||||
|
|
||||||
|
// Add the bytes
|
||||||
|
dArr[d++] = (byte) (i >> 16);
|
||||||
|
dArr[d++] = (byte) (i >> 8);
|
||||||
|
dArr[d++] = (byte) i;
|
||||||
|
|
||||||
|
// If line separator, jump over it.
|
||||||
|
if (sepCnt > 0 && ++cc == 19) {
|
||||||
|
sIx += 2;
|
||||||
|
cc = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d < len) {
|
||||||
|
// Decode last 1-3 bytes (incl '=') into 1-3 bytes
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; sIx <= eIx - pad; j++)
|
||||||
|
i |= IA[str.charAt(sIx++)] << (18 - j * 6);
|
||||||
|
|
||||||
|
for (int r = 16; d < len; r -= 8)
|
||||||
|
dArr[d++] = (byte) (i >> r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dArr;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassUtil
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ClassUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模板类所有字段
|
||||||
|
*
|
||||||
|
* @param clazz 模板对象
|
||||||
|
* @param parentFirst 是否关注父类的字段
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Field[] getFields(Class clazz, boolean parentFirst) {
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
|
||||||
|
if (parentFirst) {
|
||||||
|
Field[] parentFields = getParentFields(clazz.getSuperclass());
|
||||||
|
return concat(fields, parentFields);
|
||||||
|
}
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是不是集合的实现类
|
||||||
|
*
|
||||||
|
* @param clazz
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isCollection(Class<?> clazz) {
|
||||||
|
return Collection.class.isAssignableFrom(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取GET方法
|
||||||
|
*
|
||||||
|
* @param name 成员变量名
|
||||||
|
* @param pojoClass POJO对象
|
||||||
|
* @return 方法
|
||||||
|
*/
|
||||||
|
public static Method getMethod(String name, Class<?> pojoClass) throws RuntimeException {
|
||||||
|
String getMethodName = "get" + StringUtil.upperFirstWord(name);
|
||||||
|
try {
|
||||||
|
return pojoClass.getMethod(getMethodName);
|
||||||
|
} catch (Exception e) {
|
||||||
|
getMethodName = "is" + StringUtil.upperFirstWord(name);
|
||||||
|
try {
|
||||||
|
return pojoClass.getMethod(getMethodName);
|
||||||
|
} catch (NoSuchMethodException e1) {
|
||||||
|
throw new RuntimeException("can not find[" + name + "]method");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取SET方法
|
||||||
|
*
|
||||||
|
* @param name 成员变量名
|
||||||
|
* @param pojoClass POJO对象
|
||||||
|
* @return 方法
|
||||||
|
*/
|
||||||
|
public static Method setMethod(String name, Class<?> pojoClass, Class<?> type) {
|
||||||
|
String setMethodName = "set" + StringUtil.upperFirstWord(name);
|
||||||
|
try {
|
||||||
|
return pojoClass.getMethod(setMethodName, type);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isJavaClass(Field field) {
|
||||||
|
Class<?> fieldType = field.getType();
|
||||||
|
boolean isBaseClass = false;
|
||||||
|
if (fieldType.isArray()) {
|
||||||
|
isBaseClass = false;
|
||||||
|
} else if (fieldType.isPrimitive() || fieldType.getPackage() == null
|
||||||
|
|| fieldType.getPackage().getName().equals("java.lang")
|
||||||
|
|| fieldType.getPackage().getName().equals("java.math")
|
||||||
|
|| fieldType.getPackage().getName().equals("java.sql")
|
||||||
|
|| fieldType.getPackage().getName().equals("java.util")) {
|
||||||
|
isBaseClass = true;
|
||||||
|
}
|
||||||
|
return isBaseClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Field[] getFields(Class clazz) {
|
||||||
|
return getFields(clazz, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Field[] getParentFields(Class parentClazz) {
|
||||||
|
if (parentClazz != null) {
|
||||||
|
Field[] fields = parentClazz.getDeclaredFields();
|
||||||
|
Field[] parentFields = getParentFields(parentClazz.getSuperclass());
|
||||||
|
return concat(fields, parentFields);
|
||||||
|
}
|
||||||
|
return new Field[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Field[] concat(Field[] f1, Field[] f2) {
|
||||||
|
Field[] fields = new Field[f1.length + f2.length];
|
||||||
|
System.arraycopy(f1, 0, fields, 0, f1.length);
|
||||||
|
System.arraycopy(f2, 0, fields, f1.length, f2.length);
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MD5Util - MD5工具类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class MD5Util {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成加密密码
|
||||||
|
*
|
||||||
|
* @param value 待加密字符串
|
||||||
|
* @param salts 加密盐
|
||||||
|
* @return 加密字符串
|
||||||
|
*/
|
||||||
|
public static String generatePwd(String value, String... salts) {
|
||||||
|
String pwd = encode(value);
|
||||||
|
for (String s : salts) {
|
||||||
|
pwd = encode(s + value + s);
|
||||||
|
}
|
||||||
|
return pwd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成加密密码
|
||||||
|
*
|
||||||
|
* @param value 待加密字符串
|
||||||
|
* @return 加密字符串
|
||||||
|
*/
|
||||||
|
public static String generatePwd(String value) {
|
||||||
|
return generatePwd(value, "MD5");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密字符串
|
||||||
|
*
|
||||||
|
* @param value 待加密字符串
|
||||||
|
* @return 信息摘要Hex字符串
|
||||||
|
*/
|
||||||
|
public static String encode(String value) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("md5");
|
||||||
|
byte[] e = md.digest(value.getBytes());
|
||||||
|
return toHexString(e);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密字符串
|
||||||
|
*
|
||||||
|
* @param bytes 待加密字节数组
|
||||||
|
* @return 信息摘要Hex字符串
|
||||||
|
*/
|
||||||
|
public static String encode(byte[] bytes) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("md5");
|
||||||
|
byte[] e = md.digest(bytes);
|
||||||
|
return toHexString(e);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bytes 信息摘要字节数组
|
||||||
|
* @return Hex字符串
|
||||||
|
*/
|
||||||
|
private static String toHexString(byte bytes[]) {
|
||||||
|
StringBuilder hs = new StringBuilder();
|
||||||
|
String stmp = "";
|
||||||
|
for (int n = 0; n < bytes.length; n++) {
|
||||||
|
stmp = Integer.toHexString(bytes[n] & 0xff);
|
||||||
|
if (stmp.length() == 1)
|
||||||
|
hs.append("0").append(stmp);
|
||||||
|
else
|
||||||
|
hs.append(stmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hs.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,145 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.TreeNode;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.*;
|
||||||
|
import org.dozer.DozerBeanMapper;
|
||||||
|
import org.dozer.Mapper;
|
||||||
|
import ${basePackage}.frame.auth.LocalData;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MapUtil - 映射转化工具类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class MapperUtil {
|
||||||
|
private static ObjectMapper om;
|
||||||
|
private static Mapper mapper;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {// 优先获取SpringBoot默认配置
|
||||||
|
om = LocalData.getBean(ObjectMapper.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if (null == om) {
|
||||||
|
//初始化
|
||||||
|
om = new ObjectMapper();
|
||||||
|
// 日期格式化
|
||||||
|
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
//序列化时忽略null属性
|
||||||
|
om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
|
//序列化时排序
|
||||||
|
om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
|
||||||
|
//反序列化是忽略多余字段
|
||||||
|
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
//支持空类序列化时出错InvalidDefinitionException
|
||||||
|
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
mapper = new DozerBeanMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TreeNode toTree(String json) {
|
||||||
|
try {
|
||||||
|
return om.readTree(json);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return om.createObjectNode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TreeNode toTree(Object json) {
|
||||||
|
return om.valueToTree(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toJson(Object object) {
|
||||||
|
return toJson(object, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toJson(Object object, boolean pretty) {
|
||||||
|
try {
|
||||||
|
if (pretty) {
|
||||||
|
return om.writerWithDefaultPrettyPrinter().writeValueAsString(object);
|
||||||
|
} else {
|
||||||
|
return om.writeValueAsString(object);
|
||||||
|
}
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return "{}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toJson(JsonNode jsonNode) {
|
||||||
|
return jsonNode.asText();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T toJava(String json, Class<T> cls) {
|
||||||
|
try {
|
||||||
|
if (json == null || "".equals(json)) {
|
||||||
|
return cls.newInstance();
|
||||||
|
}
|
||||||
|
return om.readValue(json, cls);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T toJava(TreeNode treeNode, Class<T> cls) {
|
||||||
|
try {
|
||||||
|
return om.treeToValue(treeNode, cls);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T toJava(String json, TypeReference<T> valueTypeRef) {
|
||||||
|
try {
|
||||||
|
return om.readValue(json, valueTypeRef);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> toJavaList(String json, TypeReference<List<T>> reference) {
|
||||||
|
try {
|
||||||
|
return om.readValue(json, reference);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> toJavaList(InputStream json, TypeReference<List<T>> reference) {
|
||||||
|
try {
|
||||||
|
return om.readValue(json, reference);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T map(Object o, Class<T> aClass) {
|
||||||
|
return mapper.map(o, aClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void map(Object o, Object o1) {
|
||||||
|
mapper.map(o, o1);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message - 基本消息类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class Message {
|
||||||
|
public static final String ERROR_500 = "服务器走了下神,稍后再试一次";
|
||||||
|
public static final String NOT_EXIST_METHOD = "调用的方法不存在";
|
||||||
|
|
||||||
|
public static String CREATE_FAILURE = "创建失败,请刷新后重新尝试";
|
||||||
|
public static String DELETE_FAILURE = "删除失败,请刷新后重新尝试";
|
||||||
|
public static String UPDATE_FAILURE = "更新失败,请刷新后重新尝试";
|
||||||
|
public static String FIND_FAILURE = "查询失败,请刷新后重新尝试";
|
||||||
|
public static String GET_FAILURE = "未获取相应内容";
|
||||||
|
public static String INSERT_DUPLICATE = "已经存在相同数据,返回列表页面确认";
|
||||||
|
}
|
@ -0,0 +1,117 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 程序操作工具
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ProcessUtil {
|
||||||
|
/**
|
||||||
|
* 启动windows系统下的exe文件
|
||||||
|
* @param path 可执行exe文件路径
|
||||||
|
*/
|
||||||
|
public static void execExe(String path) {
|
||||||
|
Runtime rn = Runtime.getRuntime();
|
||||||
|
Process p = null;
|
||||||
|
try {
|
||||||
|
p = rn.exec(path);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error exec!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行windows批处理文件路
|
||||||
|
* @param path 可执行批处理文件路径
|
||||||
|
*/
|
||||||
|
public static void execBat(String path) {
|
||||||
|
Runtime rn = Runtime.getRuntime();
|
||||||
|
Process p = null;
|
||||||
|
try {
|
||||||
|
p = rn.exec(path);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error exec!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行windows cmd命令
|
||||||
|
* @param command cmd命令
|
||||||
|
*/
|
||||||
|
public static String execCmd(String command) {
|
||||||
|
return exec("cmd /c " + command);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行运行环境命令
|
||||||
|
* @param command cmd命令
|
||||||
|
*/
|
||||||
|
public static String exec(String command) {
|
||||||
|
StringBuilder build = new StringBuilder();
|
||||||
|
Runtime runtime = Runtime.getRuntime();
|
||||||
|
Process process = null;
|
||||||
|
try {
|
||||||
|
process = runtime.exec(command);
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
|
||||||
|
String line = null;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
build.append(line);
|
||||||
|
}
|
||||||
|
process.destroy();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return build.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws IOException
|
||||||
|
* @desc 杀死进程
|
||||||
|
* @author zp
|
||||||
|
* @date 2018-3-29
|
||||||
|
*/
|
||||||
|
public static void killProc(String processName) throws IOException {
|
||||||
|
if (processName != null && !"".equals(processName)) {
|
||||||
|
execCmd("taskkill /F /IM " + processName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @desc 判断进程是否开启
|
||||||
|
* @author zp
|
||||||
|
* @date 2018-3-29
|
||||||
|
*/
|
||||||
|
public static boolean findProcess(String processName) {
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
try {
|
||||||
|
Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName + '"');
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
|
||||||
|
String line = null;
|
||||||
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
|
if (line.contains(processName)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (bufferedReader != null) {
|
||||||
|
try {
|
||||||
|
bufferedReader.close();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,308 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.security.*;
|
||||||
|
import java.security.interfaces.RSAPrivateKey;
|
||||||
|
import java.security.interfaces.RSAPublicKey;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSAUtil - RSA工具类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class RSAUtil {
|
||||||
|
|
||||||
|
private static String cryptPublicKeyBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTrwfsrJjCF+pP4S3A/wrD4U1txg53EuBC1mPt" +
|
||||||
|
"3vGXvSK2U0YNRVR3Q65ooHnPKmk4LwI8v+7+ATTxUg3qkuRiDuzBa5zLkYKM50LOgEWSdOKzbnbx" +
|
||||||
|
"a5FnE7IXawNt1p8+MVN1TTI7J/fZy6g1x0WBy1odE5Osru4WfZNOqQtjHwIDAQAB";
|
||||||
|
private static String cryptPrivateKeyBase64 = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJOvB+ysmMIX6k/hLcD/CsPhTW3G" +
|
||||||
|
"DncS4ELWY+3e8Ze9IrZTRg1FVHdDrmigec8qaTgvAjy/7v4BNPFSDeqS5GIO7MFrnMuRgoznQs6A" +
|
||||||
|
"RZJ04rNudvFrkWcTshdrA23Wnz4xU3VNMjsn99nLqDXHRYHLWh0Tk6yu7hZ9k06pC2MfAgMBAAEC" +
|
||||||
|
"gYBjLRjKRMI1HfBZgmPChsPI9YWU4XuXVVLLL8Rd2uktOHOWM2gIw3VMvmPimVoT2GxesZr0BwTN" +
|
||||||
|
"CSxvnuX/kHPTqtsIu1r5Iup3mGbvlj3sn8RvG0yvUDglDN7QVDqqN7XWvHJSBVfBzDXeExA/WGnE" +
|
||||||
|
"6BOocNT9qkqA/UWNbCXGKQJBAN0Fd/P2D6EvCd2RztHhzVE6V8s/LwOTDnGn/YhdMpddy9TwZpBi" +
|
||||||
|
"r7I6lzcLWQ1HfDUive3t+DGXqPqr/4FfkG0CQQCrDlZKf216QrXOmJ70LQSbflgvGYU+b6kLFyEh" +
|
||||||
|
"+15HcIBfKUQCU+XUK4UzLMQDYxdngTNMNyq4AQ9Sh0tUTUI7AkEAtkq9XayzxWhLhcCtyTOoqPcq" +
|
||||||
|
"1Aqf1x3iCuHYXTEo+ek1pcJFhY6vhJuIfrDQWQB9tEGcTvI4A4cnquBTkzvjnQJAYid58ImqYmuB" +
|
||||||
|
"M6l0HJzwdeFL7MryIF+mWozNIFjDQq8VmoVtVwCZcuP+LN1VJLRpq6UBsIw/YRKKnkqwORGUHQJA" +
|
||||||
|
"UuR0G/3Hai+vKDA14tIYIH6C4zNmbULxAEuQVh9thfafWNmiDcifApvkxQ2ewXwEGeJtz44zv6iY" +
|
||||||
|
"3f3yq+a2OQ==";
|
||||||
|
|
||||||
|
private static String signPublicKeyBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTrwfsrJjCF+pP4S3A/wrD4U1txg53EuBC1mPt" +
|
||||||
|
"3vGXvSK2U0YNRVR3Q65ooHnPKmk4LwI8v+7+ATTxUg3qkuRiDuzBa5zLkYKM50LOgEWSdOKzbnbx" +
|
||||||
|
"a5FnE7IXawNt1p8+MVN1TTI7J/fZy6g1x0WBy1odE5Osru4WfZNOqQtjHwIDAQAB";
|
||||||
|
private static String signPrivateKeyBase64 = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJOvB+ysmMIX6k/hLcD/CsPhTW3G" +
|
||||||
|
"DncS4ELWY+3e8Ze9IrZTRg1FVHdDrmigec8qaTgvAjy/7v4BNPFSDeqS5GIO7MFrnMuRgoznQs6A" +
|
||||||
|
"RZJ04rNudvFrkWcTshdrA23Wnz4xU3VNMjsn99nLqDXHRYHLWh0Tk6yu7hZ9k06pC2MfAgMBAAEC" +
|
||||||
|
"gYBjLRjKRMI1HfBZgmPChsPI9YWU4XuXVVLLL8Rd2uktOHOWM2gIw3VMvmPimVoT2GxesZr0BwTN" +
|
||||||
|
"CSxvnuX/kHPTqtsIu1r5Iup3mGbvlj3sn8RvG0yvUDglDN7QVDqqN7XWvHJSBVfBzDXeExA/WGnE" +
|
||||||
|
"6BOocNT9qkqA/UWNbCXGKQJBAN0Fd/P2D6EvCd2RztHhzVE6V8s/LwOTDnGn/YhdMpddy9TwZpBi" +
|
||||||
|
"r7I6lzcLWQ1HfDUive3t+DGXqPqr/4FfkG0CQQCrDlZKf216QrXOmJ70LQSbflgvGYU+b6kLFyEh" +
|
||||||
|
"+15HcIBfKUQCU+XUK4UzLMQDYxdngTNMNyq4AQ9Sh0tUTUI7AkEAtkq9XayzxWhLhcCtyTOoqPcq" +
|
||||||
|
"1Aqf1x3iCuHYXTEo+ek1pcJFhY6vhJuIfrDQWQB9tEGcTvI4A4cnquBTkzvjnQJAYid58ImqYmuB" +
|
||||||
|
"M6l0HJzwdeFL7MryIF+mWozNIFjDQq8VmoVtVwCZcuP+LN1VJLRpq6UBsIw/YRKKnkqwORGUHQJA" +
|
||||||
|
"UuR0G/3Hai+vKDA14tIYIH6C4zNmbULxAEuQVh9thfafWNmiDcifApvkxQ2ewXwEGeJtz44zv6iY" +
|
||||||
|
"3f3yq+a2OQ==";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建密钥和私钥
|
||||||
|
*/
|
||||||
|
public static void createKey() {
|
||||||
|
try {
|
||||||
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
keyPairGenerator.initialize(1024);
|
||||||
|
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||||
|
|
||||||
|
//公钥
|
||||||
|
RSAPublicKey aPublic = (RSAPublicKey) keyPair.getPublic();
|
||||||
|
//私钥
|
||||||
|
RSAPrivateKey aPrivate = (RSAPrivateKey) keyPair.getPrivate();
|
||||||
|
//把密钥对象对应的字节转为Base64字符存储
|
||||||
|
System.err.println("publicKeyBase64-->" + Base64Util.encodeToString(aPublic.getEncoded()));
|
||||||
|
System.err.println("privateKeyBase64-->" + Base64Util.encodeToString(aPrivate.getEncoded()));
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encrypt2Base64(byte[] data) {
|
||||||
|
byte[] encrypt = encrypt(data);
|
||||||
|
return Base64Util.encodeToString(encrypt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] encrypt(String data) {
|
||||||
|
return encrypt(data.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 待加密数据
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(byte[] data) {
|
||||||
|
try {
|
||||||
|
//生成公钥对象
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64Util.decode(cryptPublicKeyBase64));
|
||||||
|
PublicKey aPublic = keyFactory.generatePublic(x509EncodedKeySpec);
|
||||||
|
|
||||||
|
//分段加密开始
|
||||||
|
ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
||||||
|
Cipher rsa = Cipher.getInstance("RSA");
|
||||||
|
rsa.init(Cipher.ENCRYPT_MODE, aPublic);
|
||||||
|
int offset = 0;
|
||||||
|
while (offset < data.length) {
|
||||||
|
byte[] bytes = rsa.doFinal(Arrays.copyOfRange(data, offset, Math.min(offset + 117, data.length)));
|
||||||
|
bs.write(bytes);
|
||||||
|
offset += 117;
|
||||||
|
}
|
||||||
|
return bs.toByteArray();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 待加密明文
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(String data, PublicKey aPublic) {
|
||||||
|
try {
|
||||||
|
if (aPublic == null) {
|
||||||
|
System.err.println("PublicKey can not be null");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//分段加密开始
|
||||||
|
ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
||||||
|
Cipher rsa = Cipher.getInstance("RSA");
|
||||||
|
rsa.init(Cipher.ENCRYPT_MODE, aPublic);
|
||||||
|
int offset = 0;
|
||||||
|
byte[] b = data.getBytes();
|
||||||
|
while (offset < b.length) {
|
||||||
|
byte[] bytes = rsa.doFinal(Arrays.copyOfRange(b, offset, Math.min(offset + 117, b.length)));
|
||||||
|
bs.write(bytes);
|
||||||
|
offset += 117;
|
||||||
|
}
|
||||||
|
return bs.toByteArray();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param base64String base64编码字符串
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(String base64String) {
|
||||||
|
return decrypt(Base64Util.decode(base64String));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decrypt2String(String base64String) {
|
||||||
|
byte[] decrypt = decrypt(Base64Util.decode(base64String));
|
||||||
|
try {
|
||||||
|
return new String(decrypt, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param data 已加密字节
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(byte[] data) {
|
||||||
|
try {
|
||||||
|
//生成私钥对象
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64Util.decode(cryptPrivateKeyBase64));
|
||||||
|
PrivateKey aPrivate = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
|
||||||
|
|
||||||
|
Cipher rsa = Cipher.getInstance("RSA");
|
||||||
|
rsa.init(Cipher.DECRYPT_MODE, aPrivate);
|
||||||
|
//获得密文字节
|
||||||
|
ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
||||||
|
int offset = 0;
|
||||||
|
while (offset < data.length) {
|
||||||
|
byte[] bytes = rsa.doFinal(Arrays.copyOfRange(data, offset, Math.min(offset + 128, data.length)));
|
||||||
|
bs.write(bytes);
|
||||||
|
offset += 128;
|
||||||
|
}
|
||||||
|
return bs.toByteArray();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param data 已加密字节
|
||||||
|
* @param aPrivate 公钥
|
||||||
|
* @return 解密后的字节
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(byte[] data, PublicKey aPrivate) {
|
||||||
|
try {
|
||||||
|
if (aPrivate == null) {
|
||||||
|
System.err.println("PublicKey can not be null");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cipher rsa = Cipher.getInstance("RSA");
|
||||||
|
rsa.init(Cipher.DECRYPT_MODE, aPrivate);
|
||||||
|
//获得密文字节
|
||||||
|
ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
||||||
|
int offset = 0;
|
||||||
|
while (offset < data.length) {
|
||||||
|
byte[] bytes = rsa.doFinal(Arrays.copyOfRange(data, offset, Math.min(offset + 128, data.length)));
|
||||||
|
bs.write(bytes);
|
||||||
|
offset += 128;
|
||||||
|
}
|
||||||
|
return bs.toByteArray();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String sign2Base64(byte[] data) {
|
||||||
|
return sign2Base64(data, signPrivateKeyBase64);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String sign2Base64(byte[] data, String privateKey) {
|
||||||
|
byte[] sign = sign(data, privateKey);
|
||||||
|
return Base64Util.encodeToString(sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA签名
|
||||||
|
*
|
||||||
|
* @param data 待签名数据
|
||||||
|
* @param privateKey 私钥
|
||||||
|
* @return 签名字节数组
|
||||||
|
*/
|
||||||
|
public static byte[] sign(byte[] data, String privateKey) {
|
||||||
|
try {
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Util.decode(privateKey));
|
||||||
|
PrivateKey aPrivate = keyFactory.generatePrivate(priPKCS8);
|
||||||
|
|
||||||
|
Signature signature = Signature.getInstance("SHA1WithRSA");
|
||||||
|
|
||||||
|
signature.initSign(aPrivate);
|
||||||
|
signature.update(data);
|
||||||
|
return signature.sign();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA验签名检查
|
||||||
|
*
|
||||||
|
* @param data 待签名数据
|
||||||
|
* @param sign base64 签名字符串
|
||||||
|
* @param publicKey 公钥
|
||||||
|
* @return 布尔值
|
||||||
|
*/
|
||||||
|
public static boolean doCheck(byte[] data, String sign, String publicKey) {
|
||||||
|
try {
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
byte[] encodedKey = Base64Util.decode(publicKey);
|
||||||
|
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
|
||||||
|
Signature signature = Signature.getInstance("SHA1WithRSA");
|
||||||
|
signature.initVerify(pubKey);
|
||||||
|
signature.update(data);
|
||||||
|
return signature.verify(Base64Util.decode(sign));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean doCheck(byte[] data, String sign) {
|
||||||
|
return doCheck(data, sign, signPublicKeyBase64);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PublicKey parsePublicKey(String cryptPublicKeyBase64) {
|
||||||
|
try {
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64Util.decode(cryptPublicKeyBase64));
|
||||||
|
return keyFactory.generatePublic(x509EncodedKeySpec);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvalidKeySpecException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PrivateKey parsePrivateKey(String cryptPrivateKeyBase64) {
|
||||||
|
try {
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Util.decode(cryptPrivateKeyBase64));
|
||||||
|
return keyFactory.generatePrivate(priPKCS8);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvalidKeySpecException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RequestUtil
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class RequestUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取请求放IP
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getIp(HttpServletRequest request) {
|
||||||
|
String ip = request.getHeader("X-Forwarded-For");
|
||||||
|
|
||||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getHeader("Proxy-Client-IP");
|
||||||
|
}
|
||||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getRemoteAddr();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (ip.length() > 15) {
|
||||||
|
String[] ips = ip.split(",");
|
||||||
|
for (int index = 0; index < ips.length; index++) {
|
||||||
|
String strIp = (String) ips[index];
|
||||||
|
if (!("unknown".equalsIgnoreCase(strIp))) {
|
||||||
|
ip = strIp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取情况方客户端信息
|
||||||
|
*
|
||||||
|
* @param request 请求
|
||||||
|
* @return 客户端信息
|
||||||
|
*/
|
||||||
|
public static String getUserAgent(HttpServletRequest request) {
|
||||||
|
return request.getHeader("User-Agent");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取转发至错误页之前的请求URL
|
||||||
|
*
|
||||||
|
* @param request 请求
|
||||||
|
* @return 请求URL
|
||||||
|
*/
|
||||||
|
public static String getErrorUrl(HttpServletRequest request) {
|
||||||
|
if (request.getAttribute("javax.servlet.error.request_uri") != null) {
|
||||||
|
return (String) request.getAttribute("javax.servlet.error.request_uri");
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,144 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import org.springframework.boot.system.ApplicationHome;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.util.ResourceUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resource资源文件工具
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ResourceUtil extends ResourceUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取资源目录下所有文件名 如: /modules/dir/
|
||||||
|
*
|
||||||
|
* @param resourcePath 路径
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static List<String> getResourceFiles(String resourcePath) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
File applicationHome = getApplicationHome();
|
||||||
|
if (applicationHome.getAbsolutePath().endsWith(".jar")) {
|
||||||
|
try {
|
||||||
|
Pattern pattern = Pattern.compile(".*" + resourcePath + "(.+\\..+)");
|
||||||
|
JarFile jarFile = new JarFile(applicationHome);
|
||||||
|
Enumeration<JarEntry> entries = jarFile.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
JarEntry jarEntry = entries.nextElement();
|
||||||
|
String name = jarEntry.getName();
|
||||||
|
|
||||||
|
if (name.matches(".*" + resourcePath + "(.+\\..+)")) {
|
||||||
|
Matcher matcher = pattern.matcher(name);
|
||||||
|
if (matcher.find()) result.add(matcher.group(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
ClassPathResource cpr = new ClassPathResource(resourcePath);
|
||||||
|
File in = cpr.getFile();
|
||||||
|
for (File file : in.listFiles()) {
|
||||||
|
result.add(file.getName());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前运行jar文件
|
||||||
|
*
|
||||||
|
* @return jar路径
|
||||||
|
*/
|
||||||
|
public static File getApplicationHome() {
|
||||||
|
ApplicationHome home = new ApplicationHome(ResourceUtil.class);
|
||||||
|
return home.getSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取resource下文件
|
||||||
|
*
|
||||||
|
* @return 文件路径
|
||||||
|
*/
|
||||||
|
public static InputStream getResourceInput(String resource) {
|
||||||
|
try {
|
||||||
|
ClassPathResource classPathResource = new ClassPathResource(resource);
|
||||||
|
return classPathResource.getInputStream();
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复制jar下的资源文件到指定文件
|
||||||
|
*
|
||||||
|
* @return 成功或失败情况
|
||||||
|
*/
|
||||||
|
public static boolean copyResource2File(String resource, File file) {
|
||||||
|
InputStream resourceInput = getResourceInput(resource);
|
||||||
|
FileOutputStream fileOutputStream = null;
|
||||||
|
try {
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
fileOutputStream = new FileOutputStream(file);
|
||||||
|
FileUtil.copy(resourceInput, fileOutputStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (fileOutputStream != null) fileOutputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取资源的的字节数组
|
||||||
|
*
|
||||||
|
* @return 字节数组
|
||||||
|
*/
|
||||||
|
public static byte[] getResourceBytes(String resource) {
|
||||||
|
InputStream is = null;
|
||||||
|
byte[] result = null;
|
||||||
|
try {
|
||||||
|
is = getResourceInput(resource);
|
||||||
|
result = new byte[is.available()];
|
||||||
|
is.read(result);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (is != null) is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import ${basePackage}.frame.excel.WExcel;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ResponseUtil
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ResponseUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包装ResponseEntity
|
||||||
|
*
|
||||||
|
* @param bytes 字节数组
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static ResponseEntity<byte[]> apply(byte[] bytes, String fileName) {
|
||||||
|
try {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, "utf-8"));
|
||||||
|
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResponseEntity<byte[]> apply(WExcel sheet) {
|
||||||
|
return apply(sheet.getBytes(), sheet.getName() + ".xlsx");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
public class StringUtil {
|
||||||
|
|
||||||
|
public static int getByteLength(String str) {
|
||||||
|
int length = str.replaceAll("[^\\x00-\\xff]", "**").length();
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String upperFirstWord(String str) {
|
||||||
|
String temp = str.substring(0, 1);
|
||||||
|
return temp.toUpperCase() + str.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isEmpty(String value) {
|
||||||
|
int strLen;
|
||||||
|
if (value == null || (strLen = value.length()) == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < strLen; i++) {
|
||||||
|
if ((Character.isWhitespace(value.charAt(i)) == false)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean isNotEmpty(String value) {
|
||||||
|
return !isEmpty(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查对象是否为数字型字符串,包含负数开头的。
|
||||||
|
*/
|
||||||
|
public static boolean isNumeric(Object obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
char[] chars = obj.toString().toCharArray();
|
||||||
|
int length = chars.length;
|
||||||
|
if (length < 1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
if (length > 1 && chars[0] == '-')
|
||||||
|
i = 1;
|
||||||
|
|
||||||
|
for (; i < length; i++) {
|
||||||
|
if (!Character.isDigit(chars[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把通用字符编码的字符串转化为汉字编码。
|
||||||
|
*/
|
||||||
|
public static String unicodeToChinese(String unicode) {
|
||||||
|
StringBuilder out = new StringBuilder();
|
||||||
|
if (!isEmpty(unicode)) {
|
||||||
|
for (int i = 0; i < unicode.length(); i++) {
|
||||||
|
out.append(unicode.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String toUnderlineStyle(String name) {
|
||||||
|
StringBuilder newName = new StringBuilder();
|
||||||
|
for (int i = 0; i < name.length(); i++) {
|
||||||
|
char c = name.charAt(i);
|
||||||
|
if (Character.isUpperCase(c)) {
|
||||||
|
if (i > 0) {
|
||||||
|
newName.append("_");
|
||||||
|
}
|
||||||
|
newName.append(Character.toLowerCase(c));
|
||||||
|
} else {
|
||||||
|
newName.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newName.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Url工具类
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class UrlUtil {
|
||||||
|
|
||||||
|
public String getUrl(String url) {
|
||||||
|
if (url == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!url.startsWith("/")) {
|
||||||
|
return "/" + url;
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||||
|
// 协议
|
||||||
|
String scheme = request.getScheme();
|
||||||
|
// 域名
|
||||||
|
String serverName = request.getServerName();
|
||||||
|
// 端口
|
||||||
|
int serverPort = request.getServerPort();
|
||||||
|
// 上下文路径
|
||||||
|
String context = request.getContextPath();
|
||||||
|
|
||||||
|
return String.format(Locale.CHINA, "%s://%s:%d%s%s", scheme, serverName, serverPort, context, url);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package ${basePackage}.frame.utils;
|
||||||
|
|
||||||
|
import ${basePackage}.frame.base.BaseRequest;
|
||||||
|
import ${basePackage}.frame.base.BaseResponse;
|
||||||
|
import ${basePackage}.frame.base.ErrorType;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import javax.validation.ValidatorFactory;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证工具类。提供一些通用简单的数据验证功能
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 2017-01-01
|
||||||
|
*/
|
||||||
|
public class ValidationUtil {
|
||||||
|
|
||||||
|
private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||||
|
|
||||||
|
public static <T extends BaseResponse> T validate(BaseRequest req, T response) {
|
||||||
|
|
||||||
|
if (req == null) {
|
||||||
|
response.addError(ErrorType.EXPECTATION_NULL, "请求对象不能为空");
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Validator validator = factory.getValidator();
|
||||||
|
|
||||||
|
Set<ConstraintViolation<BaseRequest>> constraintViolations = validator.validate(req);
|
||||||
|
|
||||||
|
if (constraintViolations.size() > 0) {
|
||||||
|
for (ConstraintViolation<BaseRequest> violation : constraintViolations) {
|
||||||
|
response.addError(ErrorType.INVALID_PARAMETER, violation.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LogUtil.dumpException(e);
|
||||||
|
response.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> validate(Object req) {
|
||||||
|
List<String> validResult = new ArrayList<>();
|
||||||
|
if (req == null) {
|
||||||
|
validResult.add("当前数据为空");
|
||||||
|
return validResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Validator validator = factory.getValidator();
|
||||||
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(req);
|
||||||
|
if (constraintViolations.size() > 0) {
|
||||||
|
for (ConstraintViolation<Object> violation : constraintViolations) {
|
||||||
|
validResult.add(violation.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
validResult.add("数据检查错误");
|
||||||
|
}
|
||||||
|
return validResult;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ${basePackage}.frame.validation;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Constraint(validatedBy = DictValidator.class)
|
||||||
|
public @interface Dict {
|
||||||
|
String message() default "字典验证错误";
|
||||||
|
|
||||||
|
String name() default "";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue