Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
zhangshaowu
/
pipi-helper
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 9c1b8099
authored
Oct 14, 2022
by
zhangshaowu
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
排班信息
1 parent
df011e1f
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
539 additions
and
225 deletions
pom.xml
src/main/java/com/pipihelper/project/configurer/WebMvcConfigurer.java
src/main/java/com/pipihelper/project/feishu/service/FeiShuEventService.java
src/main/java/com/pipihelper/project/interceptor/ResponseAop.java
src/main/java/com/pipihelper/project/rostering/model/DateRuleModel.java
src/main/java/com/pipihelper/project/rostering/model/ShiftRuleModel.java
src/main/java/com/pipihelper/project/rostering/model/StaffRuleModel.java
src/main/java/com/pipihelper/project/rostering/service/RosteringService.java
src/main/java/com/pipihelper/project/rostering/service/impl/RosteringServiceImpl.java
src/test/java/CodeGenerator.java
pom.xml
View file @
9c1b809
...
@@ -28,6 +28,10 @@
...
@@ -28,6 +28,10 @@
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-aop
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-jdbc
</artifactId>
<artifactId>
spring-boot-starter-jdbc
</artifactId>
</dependency>
</dependency>
<dependency>
<dependency>
...
...
src/main/java/com/pipihelper/project/configurer/WebMvcConfigurer.java
View file @
9c1b809
...
@@ -2,6 +2,7 @@ package com.pipihelper.project.configurer;
...
@@ -2,6 +2,7 @@ package com.pipihelper.project.configurer;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Collections
;
...
@@ -56,8 +57,8 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
...
@@ -56,8 +57,8 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
// 按需配置,更多参考FastJson文档哈
// 按需配置,更多参考FastJson文档哈
converter
.
setFastJsonConfig
(
config
);
converter
.
setFastJsonConfig
(
config
);
converter
.
setDefaultCharset
(
Charset
.
forName
(
"UTF-8"
)
);
converter
.
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
converter
.
setSupportedMediaTypes
(
Arrays
.
as
List
(
MediaType
.
APPLICATION_JSON_UTF8
));
converter
.
setSupportedMediaTypes
(
Collections
.
singleton
List
(
MediaType
.
APPLICATION_JSON_UTF8
));
converters
.
add
(
converter
);
converters
.
add
(
converter
);
}
}
...
...
src/main/java/com/pipihelper/project/feishu/service/FeiShuEventService.java
View file @
9c1b809
...
@@ -6,7 +6,6 @@ import com.pipihelper.project.feishu.dto.FeiShuEventHeaderDTO;
...
@@ -6,7 +6,6 @@ import com.pipihelper.project.feishu.dto.FeiShuEventHeaderDTO;
import
com.pipihelper.project.feishu.dto.FeiShuEventReceiveMessageDTO
;
import
com.pipihelper.project.feishu.dto.FeiShuEventReceiveMessageDTO
;
import
com.pipihelper.project.tx.dto.TxConfig
;
import
com.pipihelper.project.tx.dto.TxConfig
;
import
com.pipihelper.project.tx.service.TxApiService
;
import
com.pipihelper.project.tx.service.TxApiService
;
import
com.pipitest.project.feishu.dto.*
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.scheduling.annotation.Async
;
...
...
src/main/java/com/pipihelper/project/interceptor/ResponseAop.java
0 → 100644
View file @
9c1b809
package
com
.
pipihelper
.
project
.
interceptor
;
import
com.pipihelper.project.core.Result
;
import
com.pipihelper.project.core.ResultCode
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.springframework.stereotype.Component
;
/**
* 白名单拦截器,用于控制允许访问的请求
*
* @author zlikun
* @date 2021/10/21 14:39
*/
@Slf4j
@Component
@Aspect
public
class
ResponseAop
{
//这里改成自己项目的控制层路径
@Pointcut
(
"execution(public * com.pipihelper.project.controller..*(..))"
)
public
void
httpResponse
()
{
}
//环切
@Around
(
"httpResponse()"
)
public
Result
handlerController
(
ProceedingJoinPoint
proceedingJoinPoint
)
{
Result
result
=
new
Result
();
try
{
//获取方法的执行结果
Object
proceed
=
proceedingJoinPoint
.
proceed
();
//如果方法的执行结果是Result,则将该对象直接返回
if
(
proceed
instanceof
Result
)
{
result
=
(
Result
)
proceed
;
}
else
{
//否则,就要封装到Result的data中
result
.
setData
(
proceed
);
}
}
catch
(
Throwable
throwable
)
{
//如果出现了异常,调用异常处理方法将错误信息封装到Result中并返回
result
=
handlerException
(
throwable
);
}
return
result
;
}
//异常处理
private
Result
handlerException
(
Throwable
throwable
)
{
Result
result
=
new
Result
();
//这里需要注意,返回枚举类中的枚举在写的时候应该和异常的名称相对应,以便动态的获取异常代码和异常信息,我这边是统一返回500,msg存报的异常
//获取异常名称的方法
String
errorName
=
throwable
.
toString
();
errorName
=
errorName
.
substring
(
errorName
.
lastIndexOf
(
"."
)
+
1
);
result
.
setMessage
(
errorName
);
result
.
setCode
(
ResultCode
.
INTERNAL_SERVER_ERROR
);
return
result
;
}
}
src/main/java/com/pipihelper/project/rostering/model/DateRuleModel.java
0 → 100644
View file @
9c1b809
package
com
.
pipihelper
.
project
.
rostering
.
model
;
import
lombok.Data
;
import
java.util.Date
;
/**
* 指定日期内出现X个N班次
* (x=数字 n=班次名称)
* @description: 时间规则配置
* @author: zsw
* @create: 2022-10-14 18:48
**/
@Data
public
class
DateRuleModel
{
/**
*
*/
private
Date
date
;
private
String
shift
;
private
Integer
times
;
}
src/main/java/com/pipihelper/project/rostering/model/ShiftRuleModel.java
0 → 100644
View file @
9c1b809
package
com
.
pipihelper
.
project
.
rostering
.
model
;
import
com.alibaba.fastjson.JSONObject
;
import
com.google.common.collect.Lists
;
import
lombok.Data
;
import
java.util.Date
;
import
java.util.List
;
/**
* 班次规则
* @description:
* @author: zsw
* @create: 2022-10-14 16:52
**/
@Data
public
class
ShiftRuleModel
{
/**
* 班次名称
*/
private
String
name
;
/**
* 每天每班次出现的人数
*/
private
Integer
everyDay
;
/**
* 班次前允许
*/
private
List
<
String
>
frontAllow
;
/**
* 班次前不允许
*/
private
List
<
String
>
frontDenied
;
/**
* 班次后允许
*/
private
List
<
String
>
backAllow
;
/**
* 班次后不允许
*/
private
List
<
String
>
backDenied
;
/**
* 班次最大连续次数
*/
private
Integer
maxContinuity
;
/**
* 班次最大次数后指定班次
*/
private
String
maxContinuityFollow
;
//{"backAllow":["早班"],"backDenied":["晚班"],"everyDay":5,"frontAllow":["早班"],"frontDenied":["晚班"],"maxContinuity":5,"maxContinuityFollow":"休息","name":"早班"}
public
static
void
main
(
String
[]
args
)
{
ShiftRuleModel
shiftRuleModel
=
new
ShiftRuleModel
();
shiftRuleModel
.
setName
(
"早班"
);
shiftRuleModel
.
setEveryDay
(
5
);
shiftRuleModel
.
setFrontAllow
(
Lists
.
newArrayList
(
"早班"
));
shiftRuleModel
.
setFrontDenied
(
Lists
.
newArrayList
(
"晚班"
));
shiftRuleModel
.
setBackAllow
(
Lists
.
newArrayList
(
"早班"
));
shiftRuleModel
.
setBackDenied
(
Lists
.
newArrayList
(
"晚班"
));
shiftRuleModel
.
setMaxContinuity
(
5
);
shiftRuleModel
.
setMaxContinuityFollow
(
"休息"
);
String
s
=
JSONObject
.
toJSONString
(
shiftRuleModel
);
System
.
out
.
println
(
s
);
}
}
src/main/java/com/pipihelper/project/rostering/model/StaffRuleModel.java
0 → 100644
View file @
9c1b809
package
com
.
pipihelper
.
project
.
rostering
.
model
;
import
cn.hutool.core.date.DateUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.serializer.SerializerFeature
;
import
lombok.Data
;
import
java.util.Date
;
/**
* @description: 人员配置
* @author: zsw
* @create: 2022-10-14 18:10
**/
@Data
public
class
StaffRuleModel
{
/**
* 班次名称
*/
private
String
name
;
/**
* 当月班次最大出现次数
*/
private
Threshold
maxTimes
;
/**
* 当月班次最小出现次数
*/
private
Threshold
minTimes
;
/**
* 指定班次
*/
private
Threshold
fixedShift
;
@Data
public
static
class
Threshold
{
/**
* 班次
*/
private
String
shift
;
/**
* 次数
*/
private
Integer
times
;
/**
* 日期
*/
private
Date
date
;
}
//{"fixedShift":{"date":"2022-10-14 00:00:00","shift":"AAAA"},"maxTimes":{"shift":"AAA","times":5},"minTimes":{"shift":"AAA","times":5},"name":"xxxx"}
public
static
void
main
(
String
[]
args
)
{
StaffRuleModel
staffRuleModel
=
new
StaffRuleModel
();
staffRuleModel
.
setName
(
"xxxx"
);
Threshold
maxTimes
=
new
Threshold
();
maxTimes
.
setShift
(
"AAA"
);
maxTimes
.
setTimes
(
5
);
Threshold
minTimes
=
new
Threshold
();
minTimes
.
setShift
(
"AAA"
);
minTimes
.
setTimes
(
5
);
Threshold
fixedShift
=
new
Threshold
();
fixedShift
.
setDate
(
DateUtil
.
parse
(
"2022-10-14 00:00:00"
));
fixedShift
.
setShift
(
"AAAA"
);
staffRuleModel
.
setMaxTimes
(
maxTimes
);
staffRuleModel
.
setMinTimes
(
minTimes
);
staffRuleModel
.
setFixedShift
(
fixedShift
);
String
s
=
JSONObject
.
toJSONString
(
staffRuleModel
,
SerializerFeature
.
WriteDateUseDateFormat
);
System
.
out
.
println
(
s
);
}
}
src/main/java/com/pipihelper/project/rostering/service/RosteringService.java
View file @
9c1b809
package
com
.
pipihelper
.
project
.
rostering
.
service
;
package
com
.
pipihelper
.
project
.
rostering
.
service
;
import
com.pipihelper.project.rostering.model.DateRuleModel
;
import
com.pipihelper.project.rostering.model.StaffRuleModel
;
import
com.pipihelper.project.rostering.model.RosteringModel
;
import
com.pipihelper.project.rostering.model.ShiftRuleModel
;
import
java.util.List
;
/**
/**
* @description:
* @description:
* @author: zsw
* @author: zsw
...
@@ -7,5 +14,20 @@ package com.pipihelper.project.rostering.service;
...
@@ -7,5 +14,20 @@ package com.pipihelper.project.rostering.service;
**/
**/
public
interface
RosteringService
{
public
interface
RosteringService
{
/**
* 生成班次
* @param staffs 人员
* @param shifts 班次
* @param shiftRuleModels 班次规则
* @param staffRuleModels 原因规则
* @param dateRuleModels 时间规则
* @return
*/
List
<
RosteringModel
>
gen
(
List
<
String
>
staffs
,
List
<
String
>
shifts
,
List
<
ShiftRuleModel
>
shiftRuleModels
,
List
<
StaffRuleModel
>
staffRuleModels
,
List
<
DateRuleModel
>
dateRuleModels
);
}
}
src/main/java/com/pipihelper/project/rostering/service/impl/RosteringServiceImpl.java
View file @
9c1b809
package
com
.
pipihelper
.
project
.
rostering
.
service
.
impl
;
package
com
.
pipihelper
.
project
.
rostering
.
service
.
impl
;
import
com.pipihelper.project.rostering.model.DateRuleModel
;
import
com.pipihelper.project.rostering.model.RosteringModel
;
import
com.pipihelper.project.rostering.model.ShiftRuleModel
;
import
com.pipihelper.project.rostering.model.StaffRuleModel
;
import
com.pipihelper.project.rostering.service.RosteringService
;
import
com.pipihelper.project.rostering.service.RosteringService
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
/**
/**
* @description:
* @description:
* @author: zsw
* @author: zsw
...
@@ -10,4 +16,40 @@ import org.springframework.stereotype.Service;
...
@@ -10,4 +16,40 @@ import org.springframework.stereotype.Service;
**/
**/
@Service
@Service
public
class
RosteringServiceImpl
implements
RosteringService
{
public
class
RosteringServiceImpl
implements
RosteringService
{
@Override
public
List
<
RosteringModel
>
gen
(
List
<
String
>
staffs
,
List
<
String
>
shifts
,
List
<
ShiftRuleModel
>
shiftRuleModels
,
List
<
StaffRuleModel
>
staffRuleModels
,
List
<
DateRuleModel
>
dateRuleModels
)
{
//校验
verify
(
staffs
,
shifts
,
shiftRuleModels
,
staffRuleModels
);
return
null
;
}
private
void
verify
(
List
<
String
>
staffs
,
List
<
String
>
shifts
,
List
<
ShiftRuleModel
>
shiftRuleModels
,
List
<
StaffRuleModel
>
staffRuleModels
)
{
if
(
shiftRuleModels
.
stream
().
noneMatch
(
e
->
shifts
.
contains
(
e
.
getName
())))
{
throw
new
RuntimeException
(
"班次配置不合法"
);
}
if
(
staffRuleModels
.
stream
().
noneMatch
(
e
->
staffs
.
contains
(
e
.
getName
())))
{
throw
new
RuntimeException
(
"员工配置不合法"
);
}
}
}
}
src/test/java/CodeGenerator.java
View file @
9c1b809
import
com.google.common.base.CaseFormat
;
//
import com.google.common.base.CaseFormat;
import
freemarker.template.TemplateExceptionHandler
;
//
import freemarker.template.TemplateExceptionHandler;
import
org.apache.commons.lang3.StringUtils
;
//
import org.apache.commons.lang3.StringUtils;
import
org.mybatis.generator.api.MyBatisGenerator
;
//
import org.mybatis.generator.api.MyBatisGenerator;
import
org.mybatis.generator.config.*
;
//
import org.mybatis.generator.config.*;
import
org.mybatis.generator.internal.DefaultShellCallback
;
//
import org.mybatis.generator.internal.DefaultShellCallback;
//
import
java.io.File
;
//
import java.io.File;
import
java.io.FileWriter
;
//
import java.io.FileWriter;
import
java.io.IOException
;
//
import java.io.IOException;
import
java.text.SimpleDateFormat
;
//
import java.text.SimpleDateFormat;
import
java.util.*
;
//
import java.util.*;
//
/**
/
//
**
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
//
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
*/
//
*/
public
class
CodeGenerator
{
//
public class CodeGenerator {
//JDBC配置,请修改为你项目的实际配置
//
//JDBC配置,请修改为你项目的实际配置
private
static
final
String
JDBC_URL
=
"jdbc:mysql://124.71.186.146:3316/t_test"
;
//
private static final String JDBC_URL = "jdbc:mysql://124.71.186.146:3316/t_test";
private
static
final
String
JDBC_USERNAME
=
"weishuangshuang"
;
//
private static final String JDBC_USERNAME = "weishuangshuang";
private
static
final
String
JDBC_PASSWORD
=
"weishuangshuang2020PipiTest"
;
//
private static final String JDBC_PASSWORD = "weishuangshuang2020PipiTest";
private
static
final
String
JDBC_DIVER_CLASS_NAME
=
"com.mysql.jdbc.Driver"
;
//
private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
//
private
static
final
String
PROJECT_PATH
=
System
.
getProperty
(
"user.dir"
);
//项目在硬盘上的基础路径
//
private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
private
static
final
String
TEMPLATE_FILE_PATH
=
PROJECT_PATH
+
"/src/test/resources/generator/template"
;
//模板位置
//
private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";//模板位置
//
private
static
final
String
JAVA_PATH
=
"/src/main/java"
;
//java文件路径
//
private static final String JAVA_PATH = "/src/main/java"; //java文件路径
private
static
final
String
RESOURCES_PATH
=
"/src/main/resources"
;
//资源文件路径
//
private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径
//
private
static
final
String
PACKAGE_PATH_SERVICE
=
packageConvertPath
(
SERVICE_PACKAGE
);
//生成的Service存放路径
////
private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径
private
static
final
String
PACKAGE_PATH_SERVICE_IMPL
=
packageConvertPath
(
SERVICE_IMPL_PACKAGE
);
//生成的Service实现存放路径
////
private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径
private
static
final
String
PACKAGE_PATH_CONTROLLER
=
packageConvertPath
(
CONTROLLER_PACKAGE
);
//生成的Controller存放路径
////
private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径
//
private
static
final
String
AUTHOR
=
"CodeGenerator"
;
//@author
//
private static final String AUTHOR = "CodeGenerator";//@author
private
static
final
String
DATE
=
new
SimpleDateFormat
(
"yyyy/MM/dd"
).
format
(
new
Date
());
//@date
//
private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date
//
public
static
void
main
(
String
[]
args
)
{
//
public static void main(String[] args) {
genCode
(
"t_feishu_customer_feedback"
);
//
genCode("t_feishu_customer_feedback");
//genCodeByCustomModelName("输入表名","输入自定义Model名称");
//
//genCodeByCustomModelName("输入表名","输入自定义Model名称");
}
//
}
//
/**
//
/**
* 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
//
* 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
* 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
//
* 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
* @param tableNames 数据表名称...
//
* @param tableNames 数据表名称...
*/
//
*/
public
static
void
genCode
(
String
...
tableNames
)
{
//
public static void genCode(String... tableNames) {
for
(
String
tableName
:
tableNames
)
{
//
for (String tableName : tableNames) {
genCodeByCustomModelName
(
tableName
,
null
);
//
genCodeByCustomModelName(tableName, null);
}
//
}
}
//
}
//
/**
//
/**
* 通过数据表名称,和自定义的 Model 名称生成代码
//
* 通过数据表名称,和自定义的 Model 名称生成代码
* 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
//
* 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
* @param tableName 数据表名称
//
* @param tableName 数据表名称
* @param modelName 自定义的 Model 名称
//
* @param modelName 自定义的 Model 名称
*/
//
*/
public
static
void
genCodeByCustomModelName
(
String
tableName
,
String
modelName
)
{
//
public static void genCodeByCustomModelName(String tableName, String modelName) {
genModelAndMapper
(
tableName
,
modelName
);
//
genModelAndMapper(tableName, modelName);
genService
(
tableName
,
modelName
);
//
genService(tableName, modelName);
genController
(
tableName
,
modelName
);
//
genController(tableName, modelName);
}
//
}
//
//
public
static
void
genModelAndMapper
(
String
tableName
,
String
modelName
)
{
//
public static void genModelAndMapper(String tableName, String modelName) {
Context
context
=
new
Context
(
ModelType
.
FLAT
);
//
Context context = new Context(ModelType.FLAT);
context
.
setId
(
"Potato"
);
//
context.setId("Potato");
context
.
setTargetRuntime
(
"MyBatis3Simple"
);
//
context.setTargetRuntime("MyBatis3Simple");
context
.
addProperty
(
PropertyRegistry
.
CONTEXT_BEGINNING_DELIMITER
,
"`"
);
//
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
context
.
addProperty
(
PropertyRegistry
.
CONTEXT_ENDING_DELIMITER
,
"`"
);
//
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
//
JDBCConnectionConfiguration
jdbcConnectionConfiguration
=
new
JDBCConnectionConfiguration
();
//
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration
.
setConnectionURL
(
JDBC_URL
);
//
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
jdbcConnectionConfiguration
.
setUserId
(
JDBC_USERNAME
);
//
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
jdbcConnectionConfiguration
.
setPassword
(
JDBC_PASSWORD
);
//
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
jdbcConnectionConfiguration
.
setDriverClass
(
JDBC_DIVER_CLASS_NAME
);
//
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
context
.
setJdbcConnectionConfiguration
(
jdbcConnectionConfiguration
);
//
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
//
PluginConfiguration
pluginConfiguration
=
new
PluginConfiguration
();
//
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration
.
setConfigurationType
(
"tk.mybatis.mapper.generator.MapperPlugin"
);
//
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
pluginConfiguration
.
addProperty
(
"mappers"
,
MAPPER_INTERFACE_REFERENCE
);
//
pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
context
.
addPluginConfiguration
(
pluginConfiguration
);
//
context.addPluginConfiguration(pluginConfiguration);
//
JavaModelGeneratorConfiguration
javaModelGeneratorConfiguration
=
new
JavaModelGeneratorConfiguration
();
//
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration
.
setTargetProject
(
PROJECT_PATH
+
JAVA_PATH
);
//
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaModelGeneratorConfiguration
.
setTargetPackage
(
MODEL_PACKAGE
);
//
javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
context
.
setJavaModelGeneratorConfiguration
(
javaModelGeneratorConfiguration
);
//
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
//
SqlMapGeneratorConfiguration
sqlMapGeneratorConfiguration
=
new
SqlMapGeneratorConfiguration
();
//
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration
.
setTargetProject
(
PROJECT_PATH
+
RESOURCES_PATH
);
//
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
sqlMapGeneratorConfiguration
.
setTargetPackage
(
"mapper"
);
//
sqlMapGeneratorConfiguration.setTargetPackage("mapper");
context
.
setSqlMapGeneratorConfiguration
(
sqlMapGeneratorConfiguration
);
//
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
//
JavaClientGeneratorConfiguration
javaClientGeneratorConfiguration
=
new
JavaClientGeneratorConfiguration
();
//
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration
.
setTargetProject
(
PROJECT_PATH
+
JAVA_PATH
);
//
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaClientGeneratorConfiguration
.
setTargetPackage
(
MAPPER_PACKAGE
);
//
javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
javaClientGeneratorConfiguration
.
setConfigurationType
(
"XMLMAPPER"
);
//
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
context
.
setJavaClientGeneratorConfiguration
(
javaClientGeneratorConfiguration
);
//
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
//
TableConfiguration
tableConfiguration
=
new
TableConfiguration
(
context
);
//
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration
.
setTableName
(
tableName
);
//
tableConfiguration.setTableName(tableName);
if
(
StringUtils
.
isNotEmpty
(
modelName
))
tableConfiguration
.
setDomainObjectName
(
modelName
);
//
if (StringUtils.isNotEmpty(modelName))tableConfiguration.setDomainObjectName(modelName);
tableConfiguration
.
setGeneratedKey
(
new
GeneratedKey
(
"id"
,
"Mysql"
,
true
,
null
));
//
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
context
.
addTableConfiguration
(
tableConfiguration
);
//
context.addTableConfiguration(tableConfiguration);
//
List
<
String
>
warnings
;
//
List<String> warnings;
MyBatisGenerator
generator
;
//
MyBatisGenerator generator;
try
{
//
try {
Configuration
config
=
new
Configuration
();
//
Configuration config = new Configuration();
config
.
addContext
(
context
);
//
config.addContext(context);
config
.
validate
();
//
config.validate();
//
boolean
overwrite
=
true
;
//
boolean overwrite = true;
DefaultShellCallback
callback
=
new
DefaultShellCallback
(
overwrite
);
//
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
warnings
=
new
ArrayList
<
String
>();
//
warnings = new ArrayList<String>();
generator
=
new
MyBatisGenerator
(
config
,
callback
,
warnings
);
//
generator = new MyBatisGenerator(config, callback, warnings);
generator
.
generate
(
null
);
//
generator.generate(null);
}
catch
(
Exception
e
)
{
//
} catch (Exception e) {
throw
new
RuntimeException
(
"生成Model和Mapper失败"
,
e
);
//
throw new RuntimeException("生成Model和Mapper失败", e);
}
//
}
//
if
(
generator
.
getGeneratedJavaFiles
().
isEmpty
()
||
generator
.
getGeneratedXmlFiles
().
isEmpty
())
{
//
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
throw
new
RuntimeException
(
"生成Model和Mapper失败:"
+
warnings
);
//
throw new RuntimeException("生成Model和Mapper失败:" + warnings);
}
//
}
if
(
StringUtils
.
isEmpty
(
modelName
))
modelName
=
tableNameConvertUpperCamel
(
tableName
);
//
if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
System
.
out
.
println
(
modelName
+
".java 生成成功"
);
//
System.out.println(modelName + ".java 生成成功");
System
.
out
.
println
(
modelName
+
"Mapper.java 生成成功"
);
//
System.out.println(modelName + "Mapper.java 生成成功");
System
.
out
.
println
(
modelName
+
"Mapper.xml 生成成功"
);
//
System.out.println(modelName + "Mapper.xml 生成成功");
}
//
}
//
public
static
void
genService
(
String
tableName
,
String
modelName
)
{
//
public static void genService(String tableName, String modelName) {
try
{
//
try {
freemarker
.
template
.
Configuration
cfg
=
getConfiguration
();
//
freemarker.template.Configuration cfg = getConfiguration();
//
Map
<
String
,
Object
>
data
=
new
HashMap
<>();
//
Map<String, Object> data = new HashMap<>();
data
.
put
(
"date"
,
DATE
);
//
data.put("date", DATE);
data
.
put
(
"author"
,
AUTHOR
);
//
data.put("author", AUTHOR);
String
modelNameUpperCamel
=
StringUtils
.
isEmpty
(
modelName
)
?
tableNameConvertUpperCamel
(
tableName
)
:
modelName
;
//
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data
.
put
(
"modelNameUpperCamel"
,
modelNameUpperCamel
);
//
data.put("modelNameUpperCamel", modelNameUpperCamel);
data
.
put
(
"modelNameLowerCamel"
,
tableNameConvertLowerCamel
(
tableName
));
//
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
data
.
put
(
"basePackage"
,
BASE_PACKAGE
);
//
data.put("basePackage", BASE_PACKAGE);
//
File
file
=
new
File
(
PROJECT_PATH
+
JAVA_PATH
+
PACKAGE_PATH_SERVICE
+
modelNameUpperCamel
+
"Service.java"
);
//
File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
if
(!
file
.
getParentFile
().
exists
())
{
//
if (!file.getParentFile().exists()) {
file
.
getParentFile
().
mkdirs
();
//
file.getParentFile().mkdirs();
}
//
}
cfg
.
getTemplate
(
"service.ftl"
).
process
(
data
,
//
cfg.getTemplate("service.ftl").process(data,
new
FileWriter
(
file
));
//
new FileWriter(file));
System
.
out
.
println
(
modelNameUpperCamel
+
"Service.java 生成成功"
);
//
System.out.println(modelNameUpperCamel + "Service.java 生成成功");
//
File
file1
=
new
File
(
PROJECT_PATH
+
JAVA_PATH
+
PACKAGE_PATH_SERVICE_IMPL
+
modelNameUpperCamel
+
"ServiceImpl.java"
);
//
File file1 = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
if
(!
file1
.
getParentFile
().
exists
())
{
//
if (!file1.getParentFile().exists()) {
file1
.
getParentFile
().
mkdirs
();
//
file1.getParentFile().mkdirs();
}
//
}
cfg
.
getTemplate
(
"service-impl.ftl"
).
process
(
data
,
//
cfg.getTemplate("service-impl.ftl").process(data,
new
FileWriter
(
file1
));
//
new FileWriter(file1));
System
.
out
.
println
(
modelNameUpperCamel
+
"ServiceImpl.java 生成成功"
);
//
System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
}
catch
(
Exception
e
)
{
//
} catch (Exception e) {
throw
new
RuntimeException
(
"生成Service失败"
,
e
);
//
throw new RuntimeException("生成Service失败", e);
}
//
}
}
//
}
//
public
static
void
genController
(
String
tableName
,
String
modelName
)
{
//
public static void genController(String tableName, String modelName) {
try
{
//
try {
freemarker
.
template
.
Configuration
cfg
=
getConfiguration
();
//
freemarker.template.Configuration cfg = getConfiguration();
//
Map
<
String
,
Object
>
data
=
new
HashMap
<>();
//
Map<String, Object> data = new HashMap<>();
data
.
put
(
"date"
,
DATE
);
//
data.put("date", DATE);
data
.
put
(
"author"
,
AUTHOR
);
//
data.put("author", AUTHOR);
String
modelNameUpperCamel
=
StringUtils
.
isEmpty
(
modelName
)
?
tableNameConvertUpperCamel
(
tableName
)
:
modelName
;
//
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data
.
put
(
"baseRequestMapping"
,
modelNameConvertMappingPath
(
modelNameUpperCamel
));
//
data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
data
.
put
(
"modelNameUpperCamel"
,
modelNameUpperCamel
);
//
data.put("modelNameUpperCamel", modelNameUpperCamel);
data
.
put
(
"modelNameLowerCamel"
,
CaseFormat
.
UPPER_CAMEL
.
to
(
CaseFormat
.
LOWER_CAMEL
,
modelNameUpperCamel
));
//
data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
data
.
put
(
"basePackage"
,
BASE_PACKAGE
);
//
data.put("basePackage", BASE_PACKAGE);
//
File
file
=
new
File
(
PROJECT_PATH
+
JAVA_PATH
+
PACKAGE_PATH_CONTROLLER
+
modelNameUpperCamel
+
"Controller.java"
);
//
File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
if
(!
file
.
getParentFile
().
exists
())
{
//
if (!file.getParentFile().exists()) {
file
.
getParentFile
().
mkdirs
();
//
file.getParentFile().mkdirs();
}
//
}
//cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
//
//cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
cfg
.
getTemplate
(
"controller.ftl"
).
process
(
data
,
new
FileWriter
(
file
));
//
cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));
//
System
.
out
.
println
(
modelNameUpperCamel
+
"Controller.java 生成成功"
);
//
System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
}
catch
(
Exception
e
)
{
//
} catch (Exception e) {
throw
new
RuntimeException
(
"生成Controller失败"
,
e
);
//
throw new RuntimeException("生成Controller失败", e);
}
//
}
//
}
//
}
//
private
static
freemarker
.
template
.
Configuration
getConfiguration
()
throws
IOException
{
//
private static freemarker.template.Configuration getConfiguration() throws IOException {
freemarker
.
template
.
Configuration
cfg
=
new
freemarker
.
template
.
Configuration
(
freemarker
.
template
.
Configuration
.
VERSION_2_3_23
);
//
freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
cfg
.
setDirectoryForTemplateLoading
(
new
File
(
TEMPLATE_FILE_PATH
));
//
cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
cfg
.
setDefaultEncoding
(
"UTF-8"
);
//
cfg.setDefaultEncoding("UTF-8");
cfg
.
setTemplateExceptionHandler
(
TemplateExceptionHandler
.
IGNORE_HANDLER
);
//
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
return
cfg
;
//
return cfg;
}
//
}
//
private
static
String
tableNameConvertLowerCamel
(
String
tableName
)
{
//
private static String tableNameConvertLowerCamel(String tableName) {
return
CaseFormat
.
LOWER_UNDERSCORE
.
to
(
CaseFormat
.
LOWER_CAMEL
,
tableName
.
toLowerCase
());
//
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
}
//
}
//
private
static
String
tableNameConvertUpperCamel
(
String
tableName
)
{
//
private static String tableNameConvertUpperCamel(String tableName) {
return
CaseFormat
.
LOWER_UNDERSCORE
.
to
(
CaseFormat
.
UPPER_CAMEL
,
tableName
.
toLowerCase
());
//
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
//
}
//
}
//
private
static
String
tableNameConvertMappingPath
(
String
tableName
)
{
//
private static String tableNameConvertMappingPath(String tableName) {
tableName
=
tableName
.
toLowerCase
();
//兼容使用大写的表名
//
tableName = tableName.toLowerCase();//兼容使用大写的表名
return
"/"
+
(
tableName
.
contains
(
"_"
)
?
tableName
.
replaceAll
(
"_"
,
"/"
)
:
tableName
);
//
return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
}
//
}
//
private
static
String
modelNameConvertMappingPath
(
String
modelName
)
{
//
private static String modelNameConvertMappingPath(String modelName) {
String
tableName
=
CaseFormat
.
UPPER_CAMEL
.
to
(
CaseFormat
.
LOWER_UNDERSCORE
,
modelName
);
//
String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
return
tableNameConvertMappingPath
(
tableName
);
//
return tableNameConvertMappingPath(tableName);
}
//
}
//
private
static
String
packageConvertPath
(
String
packageName
)
{
//
private static String packageConvertPath(String packageName) {
return
String
.
format
(
"/%s/"
,
packageName
.
contains
(
"."
)
?
packageName
.
replaceAll
(
"\\."
,
"/"
)
:
packageName
);
//
return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
}
//
}
//
}
//
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment