├── .gitignore ├── actuator ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── tuacy │ │ └── actuator │ │ ├── ActuatorApplication.java │ │ ├── endpoint │ │ └── DiskSpaceEndPoint.java │ │ ├── indicator │ │ ├── AllDiskSpaceAbstractHealthIndicator.java │ │ └── AllDiskSpaceHealthIndicator.java │ │ ├── meter │ │ └── DiskMetrics.java │ │ └── utils │ │ └── FileSizeUtil.java │ └── resources │ ├── application.yml │ └── bootstrap.yml ├── framework-common ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── tuacy │ └── microservice │ └── framework │ └── common │ ├── constant │ └── ResponseResultType.java │ ├── controller │ └── BaseController.java │ ├── entity │ ├── base │ │ ├── BaseEntity.java │ │ └── BaseResponse.java │ ├── param │ │ ├── BasePageParam.java │ │ └── BaseParam.java │ └── response │ │ ├── ResponseBaseEntity.java │ │ ├── ResponseCodeEntity.java │ │ ├── ResponseDataEntity.java │ │ ├── ResponseListEntity.java │ │ └── ResponsePageEntity.java │ └── utils │ ├── CommonUtil.java │ ├── HttpUtil.java │ └── StringUtil.java ├── mybatis-interceptor ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── tuacy │ │ └── mybatis │ │ └── interceptor │ │ ├── MyBatisInterceptorApplication.java │ │ ├── config │ │ └── MybatisConfiguration.java │ │ ├── controller │ │ ├── AlarmManageController.java │ │ └── UserManageController.java │ │ ├── entity │ │ ├── param │ │ │ ├── AlarmInfoInsetParam.java │ │ │ ├── GetAllUserParam.java │ │ │ └── GetUserPageParam.java │ │ └── vo │ │ │ ├── AlarmInfoVo.java │ │ │ └── UserInfoVo.java │ │ ├── interceptor │ │ ├── encryptresultfield │ │ │ ├── EncryptResultFieldAnnotation.java │ │ │ ├── EncryptResultFieldInterceptor.java │ │ │ └── IEncryptResultFieldStrategy.java │ │ ├── log │ │ │ └── LogInterceptor.java │ │ ├── page │ │ │ ├── PageInterceptor.java │ │ │ └── PageView.java │ │ ├── tableshard │ │ │ ├── DayTableShardStrategy.java │ │ │ ├── ITableShardStrategy.java │ │ │ ├── MonthTableShardStrategy.java │ │ │ ├── TableShardAnnotation.java │ │ │ ├── TableShardInterceptor.java │ │ │ └── YearTableShardStrategy.java │ │ └── utils │ │ │ └── ReflectHelper.java │ │ ├── mapper │ │ ├── AlarmManageMapper.java │ │ ├── UserManageMapper.java │ │ └── UserMapper.java │ │ ├── model │ │ └── User.java │ │ ├── service │ │ ├── IAlarmManageService.java │ │ ├── IUserManageService.java │ │ └── impl │ │ │ ├── AlarmManageServiceImpl.java │ │ │ └── UserManageServiceImpl.java │ │ └── strategy │ │ ├── AlarmTableShardStrategy.java │ │ └── PasswordEncryptStrategy.java │ └── resources │ ├── application.yml │ ├── generator │ └── generatorConfig.xml │ └── mapper │ ├── AlarmManageMapper.xml │ ├── UserManageMapper.xml │ └── UserMapper.xml ├── pom.xml ├── quartz-manage-api ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── tuacy │ └── microservice │ └── framework │ └── quartz │ └── manage │ ├── api │ └── IQuartzManageApi.java │ └── param │ ├── QuartzJobAddParam.java │ └── QuartzJobDeleteParam.java ├── quartz-manage ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── tuacy │ │ └── microservice │ │ └── framework │ │ └── quartz │ │ └── manage │ │ ├── FrameworkQuartzManageApplication.java │ │ ├── controller │ │ └── IQuartzManageController.java │ │ ├── exception │ │ └── QuartzException.java │ │ ├── job │ │ └── SendEmailJob.java │ │ ├── mapper │ │ └── UserMapper.java │ │ ├── model │ │ └── User.java │ │ └── service │ │ ├── IQuartzActionService.java │ │ └── impl │ │ └── QuartzActionServiceImpl.java │ └── resources │ ├── application.yml │ ├── bootstrap.yml │ ├── generator │ └── generatorConfig.xml │ └── mapper │ └── UserMapper.xml ├── url-decoration ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── tuacy │ │ └── starter │ │ └── url │ │ └── decoration │ │ ├── UrlDecorationAutoConfig.java │ │ ├── UrlDecorationService.java │ │ └── UrlDecorationServiceProperties.java │ └── resources │ └── META-INF │ └── spring.factories ├── user-manage-api ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── tuacy │ └── microservice │ └── framework │ └── user │ └── manage │ └── api │ ├── api │ ├── IStarterCustomControllerApi.java │ └── IUserControllerApi.java │ ├── request │ ├── UrlDecorationParam.java │ ├── UserParam.java │ └── readme.md │ └── response │ └── UserInfoEntity.java └── user-manage ├── pom.xml └── src └── main ├── java └── com │ └── tuacy │ └── microservice │ └── framework │ └── user │ └── manage │ ├── FrameworkUserManageApplication.java │ ├── annotation │ ├── ClassAnnotation.java │ └── LoggingAnnotation.java │ ├── aspect │ └── LoggingAspect.java │ ├── controller │ ├── StarterCustomController.java │ └── UserController.java │ ├── entity │ └── readme.md │ ├── mapper │ ├── UserManageMapper.java │ └── UserMapper.java │ ├── model │ └── User.java │ └── service │ ├── IStarterCustomService.java │ ├── IUserService.java │ └── impl │ ├── StarterCustomServiceImpl.java │ └── UserServiceImpl.java └── resources ├── application.yml ├── bootstrap.yml ├── generator └── generatorConfig.xml └── mapper ├── UserManageMapper.xml └── UserMapper.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | microservice-framework.iml 4 | 5 | framework-common/framework-common.iml 6 | 7 | user-manage/user-manage.iml 8 | 9 | user-manage-api/user-manage-api.iml 10 | 11 | quartz-manage/quartz-manage.iml 12 | 13 | quartz-manage-api/quartz-manage-api.iml 14 | 15 | actuator/actuator.iml 16 | 17 | url-decoration/url-decoration.iml 18 | 19 | mybatis-interceptor/mybatis-interceptor.iml 20 | -------------------------------------------------------------------------------- /actuator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | microservice-framework 7 | tuacy.microservice.framweork 8 | PACKT-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | actuator 13 | 14 | 15 | UTF-8 16 | UTF-8 17 | 1.8 18 | Finchley.RELEASE 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-actuator 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.cloud 32 | spring-cloud-dependencies 33 | ${spring-cloud.version} 34 | pom 35 | import 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | build-info 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /actuator/src/main/java/com/tuacy/actuator/ActuatorApplication.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.actuator; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 8 | public class ActuatorApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(ActuatorApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /actuator/src/main/java/com/tuacy/actuator/endpoint/DiskSpaceEndPoint.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.actuator.endpoint; 2 | 3 | import com.tuacy.actuator.utils.FileSizeUtil; 4 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint; 5 | import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import java.io.File; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * 自定义一个端点 id = diskspace 获取磁盘容量信息 14 | */ 15 | @Configuration 16 | @Endpoint(id = "diskspace") // @EndPoint中的id不能使用驼峰法,需要以-分割 17 | public class DiskSpaceEndPoint { 18 | 19 | /** 20 | * 获取自定义端点需要监测的数据 -- 磁盘容量信息 21 | * 22 | * @return Map 23 | */ 24 | @ReadOperation 25 | public Map diskSpaceInfo() { 26 | Map result = new HashMap<>(); 27 | // 获取磁盘容量信息 28 | File[] rootFiles = File.listRoots(); 29 | if (rootFiles != null && rootFiles.length != 0) { 30 | long total = 0; 31 | long free = 0; 32 | for (File file : rootFiles) { 33 | total += file.getTotalSpace(); // 总量 34 | free += file.getUsableSpace(); // 未用 35 | } 36 | long user = total - free; // 已用 37 | double userRate = total == 0 ? 0 : ((double) user / total);// 利用率 38 | result.put("diskspaceTotal", FileSizeUtil.getPrintSize(total)); 39 | result.put("diskspaceFree", FileSizeUtil.getPrintSize(free)); 40 | result.put("diskspaceUsage", String.valueOf(userRate * 100)); 41 | } 42 | return result; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /actuator/src/main/java/com/tuacy/actuator/indicator/AllDiskSpaceAbstractHealthIndicator.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.actuator.indicator; 2 | 3 | import com.tuacy.actuator.utils.FileSizeUtil; 4 | import org.springframework.boot.actuate.health.AbstractHealthIndicator; 5 | import org.springframework.boot.actuate.health.Health; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.io.File; 9 | 10 | /** 11 | * 通过自定义一个类继承AbstractHealthIndicator,往health端点添加数据 12 | * 13 | * allDiskSpace2 对应的信息会在 http://127.0.0.1:2224/actuator/health 里面显示出来 14 | */ 15 | @Component("allDiskSpace2") 16 | public class AllDiskSpaceAbstractHealthIndicator extends AbstractHealthIndicator { 17 | 18 | @Override 19 | protected void doHealthCheck(Health.Builder builder) throws Exception { 20 | 21 | File[] rootFiles = File.listRoots(); 22 | if (rootFiles != null && rootFiles.length != 0) { 23 | long total = 0, free = 0; 24 | for (File file : rootFiles) { 25 | total += file.getTotalSpace(); // 总量 26 | free += file.getUsableSpace(); // 未用 27 | } 28 | long user = total - free; // 已用 29 | double userRate = total == 0 ? 0 : ((double) user / total);// 利用率 30 | builder.up() 31 | .withDetail("diskspaceTotal", FileSizeUtil.getPrintSize(total)) 32 | .withDetail("diskspaceFree", FileSizeUtil.getPrintSize(free)) 33 | .withDetail("diskspaceUsage", String.valueOf(userRate * 100)) 34 | .build(); 35 | } else { 36 | builder.down().build(); 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /actuator/src/main/java/com/tuacy/actuator/indicator/AllDiskSpaceHealthIndicator.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.actuator.indicator; 2 | 3 | import com.tuacy.actuator.utils.FileSizeUtil; 4 | import org.springframework.boot.actuate.health.Health; 5 | import org.springframework.boot.actuate.health.HealthIndicator; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.io.File; 9 | 10 | /** 11 | * 通过自定义一个类实现HealthIndicator接口,往health端点添加数据 12 | * allDiskSpace0 对应的信息会在 http://127.0.0.1:2224/actuator/health 里面显示出来 13 | */ 14 | @Component("allDiskSpace0") 15 | public class AllDiskSpaceHealthIndicator implements HealthIndicator { 16 | 17 | @Override 18 | public Health health() { 19 | File[] rootFiles = File.listRoots(); 20 | if (rootFiles != null && rootFiles.length != 0) { 21 | long total = 0, free = 0; 22 | for (File file : rootFiles) { 23 | total += file.getTotalSpace(); // 总量 24 | free += file.getUsableSpace(); // 未用 25 | } 26 | long user = total - free; // 已用 27 | double userRate = total == 0 ? 0 : ((double) user / total);// 利用率 28 | return Health.up() 29 | .withDetail("diskspaceTotal", FileSizeUtil.getPrintSize(total)) 30 | .withDetail("diskspaceFree", FileSizeUtil.getPrintSize(free)) 31 | .withDetail("diskspaceUsage", String.valueOf(userRate * 100)) 32 | .build(); 33 | } else { 34 | return Health.down().build(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /actuator/src/main/java/com/tuacy/actuator/meter/DiskMetrics.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.actuator.meter; 2 | 3 | import io.micrometer.core.instrument.Gauge; 4 | import io.micrometer.core.instrument.MeterRegistry; 5 | import io.micrometer.core.instrument.binder.MeterBinder; 6 | import org.springframework.stereotype.Component; 7 | 8 | import javax.validation.constraints.NotNull; 9 | import java.io.File; 10 | 11 | @Component 12 | public class DiskMetrics implements MeterBinder { 13 | 14 | private File rootFilePath; 15 | 16 | public DiskMetrics() { 17 | this.rootFilePath = new File("."); 18 | } 19 | 20 | @Override 21 | public void bindTo(@NotNull MeterRegistry registry) { 22 | // 磁盘已用容量 23 | Gauge.builder("diskSpaceInfo.total", rootFilePath, File::getTotalSpace) 24 | .register(registry); 25 | // 磁盘剩余容量 26 | Gauge.builder("diskSpaceInfo.free", rootFilePath, File::getFreeSpace) 27 | .register(registry); 28 | // 磁盘使用率 29 | Gauge.builder("diskSpaceInfo.usage", rootFilePath, c -> { 30 | long totalDiskSpace = rootFilePath.getTotalSpace(); 31 | if (totalDiskSpace == 0) { 32 | return 0.0; 33 | } 34 | 35 | long usedDiskSpace = totalDiskSpace - rootFilePath.getFreeSpace(); 36 | return (double) usedDiskSpace / totalDiskSpace * 100; 37 | }) 38 | .register(registry); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /actuator/src/main/java/com/tuacy/actuator/utils/FileSizeUtil.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.actuator.utils; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class FileSizeUtil { 6 | 7 | /** 8 | * font size转换 9 | */ 10 | public static String getPrintSize(long size) { 11 | // 如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义 12 | double value = (double) size; 13 | if (value < 1024) { 14 | return String.valueOf(value) + " B"; 15 | } else { 16 | value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); 17 | } 18 | // 如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位 19 | // 因为还没有到达要使用另一个单位的时候 20 | // 接下去以此类推 21 | if (value < 1024) { 22 | return String.valueOf(value) + " KB"; 23 | } else { 24 | value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); 25 | } 26 | if (value < 1024) { 27 | return String.valueOf(value) + " MB"; 28 | } else { 29 | // 否则如果要以GB为单位的,先除于1024再作同样的处理 30 | value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); 31 | return String.valueOf(value) + " GB"; 32 | } 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /actuator/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # actuator监控 2 | management: 3 | # server: 4 | # # 设置监控服务端口,如果没写默认就是服务器的端口 5 | # port: 8081 6 | endpoints: 7 | # 设置端点是否可用 默认只有shutdown可用 8 | enabled-by-default: true 9 | web: 10 | # 设置是否暴露端点 默认只有health和info可见 11 | exposure: 12 | # 包括所有端点 13 | include: "*" # 注意需要添加引号 14 | # 排除端点 15 | exclude: shutdown 16 | endpoint: 17 | health: 18 | show-details: always 19 | #可以关闭指定的端点 20 | shutdown: 21 | enabled: false 22 | 23 | 24 | -------------------------------------------------------------------------------- /actuator/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2224 3 | 4 | spring: 5 | application: 6 | name: microservice-framework-user-manage 7 | cloud: 8 | config: # spring could config client 9 | discovery: 10 | enabled: true #开启配置服务发现 11 | service-id: microservice-center-config-server #配置服务实例名称 12 | label: master #配置文件所在分支 13 | 14 | -------------------------------------------------------------------------------- /framework-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | microservice-framework 7 | tuacy.microservice.framweork 8 | PACKT-SNAPSHOT 9 | 10 | jar 11 | 4.0.0 12 | 13 | framework-common 14 | 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-maven-plugin 21 | 22 | 23 | none 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/constant/ResponseResultType.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.constant; 2 | 3 | 4 | import com.tuacy.microservice.framework.common.utils.StringUtil; 5 | 6 | public enum ResponseResultType { 7 | 8 | SUCCESS(0, "SUCCESS"), //成功 9 | REQUEST_PARA_ERROR(1, "Parameter Error"), //参数不正确 10 | JSON_PARA_EXCEPTION(2, "Json format error"), //JSON 参数格式错误 11 | DB_OPERATE_ERROR(3, "Database operation failed"), //数据库操作失败 12 | USER_NO_AUTH(4, "No authority"), //没有权限 13 | SYS_SO_BUSY(5, "Server busy"), //服务器忙 14 | USER_NO_LOGIN(6, "User not login"), //用户未登录 15 | OTHER_ERROR(7, "Other error"), //其他错误(自定义提示内容) 16 | NEED_CONFIRM(8, "Need confirm"); // 当保存操作时需要进行再次确认 17 | 18 | private int value = 0; 19 | private String desc = null; 20 | private String myDesc = null; //用户自定义显示内容 21 | 22 | ResponseResultType(int value, String desc) { 23 | this.value = value; 24 | this.desc = desc; 25 | } 26 | 27 | /** 28 | * int到enum的转换函数 29 | * 30 | * @param value 31 | * @return 32 | */ 33 | public static ResponseResultType valueOf(int value) { 34 | switch (value) { 35 | case 0: 36 | return SUCCESS; 37 | case 1: 38 | return REQUEST_PARA_ERROR; 39 | case 2: 40 | return JSON_PARA_EXCEPTION; 41 | case 3: 42 | return DB_OPERATE_ERROR; 43 | case 4: 44 | return USER_NO_AUTH; 45 | case 5: 46 | return SYS_SO_BUSY; 47 | case 6: 48 | return USER_NO_LOGIN; 49 | case 7: 50 | return OTHER_ERROR; 51 | default: 52 | return null; 53 | } 54 | 55 | } 56 | 57 | public Integer getValue() { 58 | return value; 59 | } 60 | 61 | public String getDesc() { 62 | String ret = StringUtil.validEmpty(myDesc) ? myDesc : desc; 63 | this.myDesc = null; //清空自定义内容 64 | return ret; 65 | } 66 | 67 | public void setValue(int value) { 68 | this.value = value; 69 | } 70 | 71 | public void setDesc(String desc) { 72 | this.myDesc = desc; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/controller/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.controller; 2 | 3 | import com.tuacy.microservice.framework.common.entity.param.BaseParam; 4 | import com.tuacy.microservice.framework.common.utils.CommonUtil; 5 | import com.tuacy.microservice.framework.common.utils.HttpUtil; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | 10 | /** 11 | * http controller base 12 | */ 13 | public abstract class BaseController { 14 | 15 | protected F getAuthRequestObject(BaseParam baseParam, HttpServletRequest request) { 16 | if (baseParam != null) { 17 | String token = CommonUtil.getRequestAttribute(request, "access_token", HttpUtil.getRequestQueryString(request)); 18 | } 19 | return (F) baseParam; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/base/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.base; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface BaseEntity extends Serializable { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/base/BaseResponse.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.base; 2 | 3 | import com.tuacy.microservice.framework.common.constant.ResponseResultType; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class BaseResponse implements BaseEntity { 9 | 10 | private ResponseResultType resultType; 11 | private List data; 12 | 13 | public BaseResponse() { 14 | this.resultType = ResponseResultType.SUCCESS; 15 | } 16 | 17 | public BaseResponse(ResponseResultType resultType) { 18 | this.resultType = resultType; 19 | } 20 | 21 | public BaseResponse(ResponseResultType resultType, T data) { 22 | this.resultType = resultType; 23 | this.data = new ArrayList<>(); 24 | this.data.add(data); 25 | } 26 | 27 | /** 28 | * 自定义错误信息 29 | * 30 | * @param errMsg 31 | */ 32 | public void setCustomErrMsg(String errMsg) { 33 | this.resultType = ResponseResultType.OTHER_ERROR; 34 | this.resultType.setDesc(errMsg); 35 | } 36 | 37 | public ResponseResultType getResultType() { 38 | return resultType; 39 | } 40 | 41 | public void setResultType(ResponseResultType resultType) { 42 | this.resultType = resultType; 43 | } 44 | 45 | public List getData() { 46 | return data; 47 | } 48 | 49 | public void setData(List data) { 50 | this.data = data; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/param/BasePageParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.param; 2 | 3 | /** 4 | * 分页请求数据的param 5 | */ 6 | public class BasePageParam extends BaseParam { 7 | 8 | private int pageIndex; 9 | private int pageSize; 10 | 11 | public int getPageIndex() { 12 | return pageIndex; 13 | } 14 | 15 | public void setPageIndex(int pageIndex) { 16 | this.pageIndex = pageIndex; 17 | } 18 | 19 | public int getPageSize() { 20 | return pageSize; 21 | } 22 | 23 | public void setPageSize(int pageSize) { 24 | this.pageSize = pageSize; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/param/BaseParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.param; 2 | 3 | /** 4 | * 请求数据的param 5 | */ 6 | public class BaseParam { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/response/ResponseBaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.response; 2 | 3 | public abstract class ResponseBaseEntity { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/response/ResponseCodeEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.response; 2 | 3 | 4 | /** 5 | * http请求返回数据,status + code 6 | */ 7 | public class ResponseCodeEntity extends ResponseBaseEntity { 8 | 9 | private int status; 10 | private String msg; 11 | 12 | public int getStatus() { 13 | return status; 14 | } 15 | 16 | public void setStatus(int status) { 17 | this.status = status; 18 | } 19 | 20 | public String getMsg() { 21 | return msg; 22 | } 23 | 24 | public void setMsg(String msg) { 25 | this.msg = msg; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/response/ResponseDataEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.response; 2 | 3 | /** 4 | * http请求返回数据 data 5 | */ 6 | 7 | public class ResponseDataEntity extends ResponseCodeEntity { 8 | 9 | private T data; 10 | 11 | public T getData() { 12 | return data; 13 | } 14 | 15 | public void setData(T data) { 16 | this.data = data; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/response/ResponseListEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.response; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * http请求返回数据 list 7 | */ 8 | public class ResponseListEntity extends ResponseCodeEntity { 9 | 10 | private List data; 11 | 12 | public List getData() { 13 | return data; 14 | } 15 | 16 | public void setData(List data) { 17 | this.data = data; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/entity/response/ResponsePageEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.entity.response; 2 | 3 | /** 4 | * http请求返回数据 page info + list 5 | */ 6 | public class ResponsePageEntity extends ResponseListEntity { 7 | private int pageIndex; 8 | private int pageSize; 9 | private long pageCount; 10 | private long totalCount; 11 | 12 | public int getPageIndex() { 13 | return pageIndex; 14 | } 15 | 16 | public void setPageIndex(int pageIndex) { 17 | this.pageIndex = pageIndex; 18 | } 19 | 20 | public int getPageSize() { 21 | return pageSize; 22 | } 23 | 24 | public void setPageSize(int pageSize) { 25 | this.pageSize = pageSize; 26 | } 27 | 28 | public long getPageCount() { 29 | return pageCount; 30 | } 31 | 32 | public void setPageCount(long pageCount) { 33 | this.pageCount = pageCount; 34 | } 35 | 36 | public long getTotalCount() { 37 | return totalCount; 38 | } 39 | 40 | public void setTotalCount(long totalCount) { 41 | this.totalCount = totalCount; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/utils/CommonUtil.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.utils; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class CommonUtil { 11 | 12 | private static Logger logger = LoggerFactory.getLogger(CommonUtil.class.getName()); 13 | 14 | /** 15 | * 对象转换为List 16 | * 17 | * @param obj 18 | * @return 19 | */ 20 | public static List itemToList(Object obj) { 21 | List objList = new ArrayList(); 22 | objList.add(obj); 23 | return objList; 24 | } 25 | 26 | /** 27 | * 对象转换为List 28 | * 29 | * @param list 30 | * @return 31 | */ 32 | public static List listToObjectList(List list) { 33 | List objList = new ArrayList(); 34 | for (int i = 0; i < list.size(); i++) { 35 | objList.add(list.get(i)); 36 | } 37 | return objList; 38 | } 39 | 40 | /** 41 | * 获取请求URL参数,适用于GET和POST请求 42 | * 43 | * @param name 44 | * @param str 45 | * @return 46 | */ 47 | public static String getQueryStringValue(String name, String str) { 48 | if (StringUtil.validEmpty(str)) { 49 | String[] paramArr = str.split("&"); 50 | for (String param : paramArr) { 51 | if (StringUtil.validEmpty(param) && param.indexOf("=") > -1) { 52 | String[] valArr = param.split("="); 53 | if (name.equalsIgnoreCase(valArr[0])) { 54 | return valArr[1]; 55 | } 56 | } 57 | } 58 | } 59 | return null; 60 | } 61 | 62 | /** 63 | * 获取请求Token 64 | * 65 | * @return 66 | */ 67 | public static String getRequestAttribute(HttpServletRequest request, String code, String queryStr) { 68 | return request.getHeader(code) == null ? CommonUtil.getQueryStringValue(code, queryStr) : request.getHeader(code); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/utils/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.utils; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import java.io.IOException; 8 | 9 | /** 10 | * 通过post方式调用http请求接口的工具类 11 | */ 12 | public class HttpUtil { 13 | private final static Logger logger = LoggerFactory.getLogger(HttpUtil.class); 14 | 15 | /** 16 | * 获取request 中的 QueryString 17 | * 18 | * @param request 请求对象 19 | * @return 返回QueryString 字符串 20 | */ 21 | public static String getRequestQueryString(HttpServletRequest request) { 22 | String result = ""; 23 | try { 24 | request.setCharacterEncoding("UTF-8"); 25 | result = request.getQueryString(); 26 | } catch (IOException var4) { 27 | var4.printStackTrace(); 28 | } 29 | 30 | return result; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /framework-common/src/main/java/com/tuacy/microservice/framework/common/utils/StringUtil.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.common.utils; 2 | 3 | import java.math.BigInteger; 4 | import java.util.List; 5 | import java.util.logging.Logger; 6 | 7 | /** 8 | * @name: StringUtil 9 | * @author: tuacy. 10 | * @date: 2018/9/17. 11 | * @version: 1.0 12 | * @Description: 13 | */ 14 | public class StringUtil { 15 | 16 | private static Logger logger = Logger.getLogger(StringUtil.class.getName()); 17 | 18 | /** 19 | * 是否为空,如果为空或者为空字符串则返回不通过 false 20 | * 21 | * @param string 22 | * @return 23 | */ 24 | public static boolean validEmpty(String string) { 25 | return !(string == null || "".equals(string)); 26 | } 27 | 28 | /** 29 | * 字符串是否为空 30 | * 31 | * @param string 32 | * @return 33 | */ 34 | public static boolean isEmplty(String string) { 35 | return string == null || "".equals(string); 36 | } 37 | 38 | /** 39 | * 判断是有值,并肯不是全选 40 | * 41 | * @param string 42 | * @return 43 | */ 44 | public static boolean validEmptyOrAll(String string) { 45 | return !(string == null || "".equals(string) || "-1".equals(string)); 46 | } 47 | 48 | /** 49 | * 返回空字符串代替null 50 | * 51 | * @param string 52 | * @return 53 | */ 54 | public static String nullToEmpty(String string) { 55 | return string == null ? "" : string; 56 | } 57 | 58 | /** 59 | * 比较两个版本号,返回较高版本号 60 | * 61 | * @param version1 62 | * @param version2 63 | * @return 64 | */ 65 | public static String VersionCompare(String version1, String version2) { 66 | String highVersion = ""; 67 | if (validEmpty(version1) && validEmpty(version2)) { 68 | String[] verArr1 = version1.split("\\."); 69 | String[] verArr2 = version2.split("\\."); 70 | 71 | int length = verArr1.length > verArr2.length ? verArr1.length : verArr2.length; //取层级最高的版本号 72 | for (int i = 0; i < length; i++) { 73 | int val1 = verArr1.length > i ? Integer.parseInt(verArr1[i]) : 0; 74 | int val2 = verArr2.length > i ? Integer.parseInt(verArr2[i]) : 0; 75 | 76 | if (val1 != val2) { 77 | highVersion = val1 > val2 ? version1 : version2; 78 | break; 79 | } 80 | } 81 | 82 | // 完全相同,取第一个版本号 83 | if (highVersion == "") { 84 | highVersion = version1; 85 | } 86 | } 87 | return highVersion; 88 | } 89 | 90 | /** 91 | * @param list 92 | * @return 93 | */ 94 | public static String listToString(List list, char separator) { 95 | StringBuilder ret = new StringBuilder(); 96 | if (list != null && list.size() > 0) { 97 | for (Integer item : list) { 98 | ret.append(item.toString()).append(separator); 99 | } 100 | if (ret.length() > 0) { 101 | ret.deleteCharAt(ret.length() - 1); 102 | } 103 | } 104 | return ret.toString(); 105 | } 106 | 107 | 108 | /** 109 | * list转换为string 110 | * 111 | * @param list 112 | * @return 113 | */ 114 | public static String listToString(List list) { 115 | return listToString(list, ','); 116 | } 117 | 118 | /** 119 | * list转换为string 120 | * 121 | * @param list 122 | * @return 123 | */ 124 | public static String strListToString(List list) { 125 | StringBuilder ret = new StringBuilder(); 126 | if (list != null && list.size() > 0) { 127 | for (String item : list) { 128 | ret.append("'").append(item).append("',"); 129 | } 130 | if (ret.length() > 0) { 131 | ret.deleteCharAt(ret.length() - 1); 132 | } 133 | } 134 | return ret.toString(); 135 | } 136 | 137 | /** 138 | * 把字符串按分隔符转换为数组 139 | * 140 | * @param str 字符串 141 | * @param expr 分隔符 142 | * @return 143 | */ 144 | public static String[] stringSplitToArray(String str, String expr) { 145 | return str.split(expr); 146 | } 147 | 148 | /** 149 | * 字符数组转换为string 150 | * 151 | * @param arr 152 | * @return 153 | */ 154 | public static String strArrayToString(String[] arr, String separator) { 155 | StringBuilder ret = new StringBuilder(); 156 | if (arr != null && arr.length > 0) { 157 | for (String s : arr) { 158 | ret.append(s).append(separator); 159 | } 160 | if (ret.length() > 0) { 161 | ret.deleteCharAt(ret.length() - 1); 162 | } 163 | } 164 | return ret.toString(); 165 | } 166 | 167 | /** 168 | * Integer数组转换为string 169 | * 170 | * @param arr 171 | * @return 172 | */ 173 | public static String arrayToString(Integer[] arr) { 174 | return arrayToString(arr, ","); 175 | } 176 | 177 | /** 178 | * Integer数组转换为string 179 | * 180 | * @param arr 181 | * @return 182 | */ 183 | public static String arrayToString(Integer[] arr, String separator) { 184 | StringBuilder ret = new StringBuilder(); 185 | if (arr != null && arr.length > 0) { 186 | for (Integer i : arr) { 187 | ret.append(i).append(separator); 188 | } 189 | if (ret.length() > 0) { 190 | ret.deleteCharAt(ret.length() - 1); 191 | } 192 | } 193 | return ret.toString(); 194 | } 195 | 196 | /** 197 | * 计算百分比 198 | * 199 | * @param d1 200 | * @param b2 201 | * @return 202 | */ 203 | public static Double doubleDivision(Double d1, BigInteger b2) { 204 | double ret = 0d; 205 | if (!b2.equals(new BigInteger("0"))) { 206 | ret = d1 * 100 / b2.intValue(); 207 | } 208 | return Math.round(ret * 10) / 10.0; 209 | } 210 | 211 | /** 212 | * String 转 Int 213 | * 214 | * @param str 215 | * @return 216 | */ 217 | public static int StringParseInt(String str) { 218 | int result = 0; 219 | if (validEmpty(str)) { 220 | try { 221 | Double d = Double.parseDouble(str.trim()); 222 | result = d.intValue(); 223 | } catch (NumberFormatException ex) { 224 | ex.printStackTrace(); 225 | } 226 | } 227 | return result; 228 | } 229 | 230 | /** 231 | * String 转 Int 232 | * 233 | * @param str 234 | * @return 235 | */ 236 | public static long StringParseLong(String str) { 237 | long result = 0; 238 | if (validEmpty(str)) { 239 | try { 240 | Double d = Double.parseDouble(str.trim()); 241 | result = d.longValue(); 242 | } catch (NumberFormatException ex) { 243 | ex.printStackTrace(); 244 | } 245 | } 246 | return result; 247 | } 248 | 249 | /** 250 | * 判断ID是否在listStr之中 251 | * 252 | * @param listStr eg。4,3,2,1 253 | * @param id 254 | * @return 255 | */ 256 | public static boolean checkIdInList(String listStr, long id) { 257 | if (StringUtil.validEmpty(listStr) && id > 0) { 258 | String[] idArr = listStr.split(","); 259 | for (String s : idArr) { 260 | if (id == StringUtil.StringParseInt(s)) { 261 | return true; 262 | } 263 | } 264 | } 265 | return false; 266 | } 267 | 268 | /** 269 | * id 列表转换为 逗号分隔的字符串 270 | * 271 | * @param ids 272 | * @return 273 | */ 274 | public static String arrayToString(List ids) { 275 | return arrayToString(ids, ","); 276 | } 277 | 278 | /** 279 | * id 列表转换为 逗号分隔的字符串 280 | * 281 | * @param ids 282 | * @param separator 283 | * @return 284 | */ 285 | public static String arrayToString(List ids, String separator) { 286 | StringBuilder ret = new StringBuilder(); 287 | if (ids != null && ids.size() > 0) { 288 | for (Long i : ids) { 289 | ret.append(i).append(separator); 290 | } 291 | if (ret.length() > 0) { 292 | ret.deleteCharAt(ret.length() - 1); 293 | } 294 | } 295 | return ret.toString(); 296 | } 297 | 298 | /** 299 | * 检测最后一个元素是否与传入ID相同 300 | * 301 | * @param id 302 | * @param targetVal 303 | * @return 304 | */ 305 | public static boolean checkIsParentOnlyLastLevel(long id, String targetVal) { 306 | if (validEmpty(targetVal)) { 307 | String[] idArr = targetVal.split(":"); 308 | if (idArr.length > 2 && idArr[idArr.length - 2].equals(id)) { // 监测最后一个节点是否相等 309 | return true; 310 | } 311 | } 312 | return false; 313 | } 314 | 315 | 316 | /** 317 | * 两个Integer比较 318 | * 319 | * @param first 320 | * @param second 321 | * @return 322 | */ 323 | public static boolean equalWithInteger(Integer first, Integer second) { 324 | if (first != null && second != null) { 325 | return first.intValue() == second.intValue(); 326 | } 327 | return false; 328 | } 329 | 330 | /** 331 | * 两个Long比较 332 | * 333 | * @param first 334 | * @param second 335 | * @return 336 | */ 337 | public static boolean equalWithLong(Long first, Long second) { 338 | if (first != null && second != null) { 339 | return first.intValue() == second.intValue(); 340 | } 341 | return false; 342 | } 343 | 344 | } 345 | -------------------------------------------------------------------------------- /mybatis-interceptor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | microservice-framework 7 | tuacy.microservice.framweork 8 | PACKT-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | mybatis-interceptor 13 | 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-jdbc 20 | 21 | 22 | 23 | 24 | mysql 25 | mysql-connector-java 26 | 5.1.39 27 | 28 | 29 | 30 | 31 | com.alibaba 32 | druid 33 | 1.0.18 34 | 35 | 36 | 37 | 38 | org.mybatis.spring.boot 39 | mybatis-spring-boot-starter 40 | 1.3.2 41 | 42 | 43 | 44 | tuacy.microservice.framweork 45 | framework-common 46 | ${parent.version} 47 | 48 | 49 | 50 | org.apache.commons 51 | commons-lang3 52 | 3.3.2 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-maven-plugin 61 | 62 | 63 | none 64 | 65 | 66 | 67 | 68 | 69 | org.mybatis.generator 70 | mybatis-generator-maven-plugin 71 | 1.3.2 72 | 73 | 74 | mysql 75 | mysql-connector-java 76 | 5.1.35 77 | 78 | 79 | 80 | ${basedir}/src/main/resources/generator/generatorConfig.xml 81 | true 82 | true 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/MyBatisInterceptorApplication.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.transaction.annotation.EnableTransactionManagement; 7 | 8 | @SpringBootApplication 9 | //@EnableDiscoveryClient 10 | @EnableTransactionManagement 11 | @MapperScan(basePackages = "com.tuacy.mybatis.interceptor.mapper") 12 | //@EnableFeignClients 13 | //@EnableHystrix 14 | public class MyBatisInterceptorApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(MyBatisInterceptorApplication.class, args); 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/config/MybatisConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.config; 2 | 3 | import com.tuacy.mybatis.interceptor.interceptor.encryptresultfield.EncryptResultFieldInterceptor; 4 | import com.tuacy.mybatis.interceptor.interceptor.log.LogInterceptor; 5 | import com.tuacy.mybatis.interceptor.interceptor.page.PageInterceptor; 6 | import com.tuacy.mybatis.interceptor.interceptor.tableshard.TableShardInterceptor; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | import java.util.Properties; 11 | 12 | /** 13 | * mybatis配置 14 | */ 15 | @Configuration 16 | public class MybatisConfiguration { 17 | 18 | @Bean 19 | public PageInterceptor pageInterceptor() { 20 | PageInterceptor interceptor = new PageInterceptor(); 21 | Properties properties = new Properties(); 22 | properties.setProperty(PageInterceptor.PROPERTIES_KEY_DATABASE_TYPE, PageInterceptor.DATABASE_TYPE_MYSQL); 23 | properties.setProperty(PageInterceptor.PROPERTIES_KEY_PAGE_EXPRESSION_MATCHING, ".*Page.*"); 24 | properties.setProperty(PageInterceptor.PROPERTIES_KEY_COUNT_SUFFIX, "_COUNT"); 25 | interceptor.setProperties(properties); 26 | return interceptor; 27 | } 28 | 29 | @Bean 30 | public TableShardInterceptor tableSplitInterceptor() { 31 | return new TableShardInterceptor(); 32 | } 33 | 34 | @Bean 35 | public LogInterceptor logInterceptor() { 36 | LogInterceptor interceptor = new LogInterceptor(); 37 | Properties properties = new Properties(); 38 | // 设置是否显示数据库执行语句的执行时间 39 | properties.setProperty(LogInterceptor.PROPERTIES_KEY_ENABLE_EXECUTOR_TIME, LogInterceptor.ENABLE_EXECUTOR_TIME); 40 | interceptor.setProperties(properties); 41 | return interceptor; 42 | } 43 | 44 | @Bean 45 | public EncryptResultFieldInterceptor encryptResultFieldInterceptor() { 46 | return new EncryptResultFieldInterceptor(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/controller/AlarmManageController.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.controller; 2 | 3 | import com.tuacy.microservice.framework.common.controller.BaseController; 4 | import com.tuacy.microservice.framework.common.entity.response.ResponseListEntity; 5 | import com.tuacy.mybatis.interceptor.entity.param.AlarmInfoInsetParam; 6 | import com.tuacy.mybatis.interceptor.entity.vo.UserInfoVo; 7 | import com.tuacy.mybatis.interceptor.service.IAlarmManageService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RequestMethod; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | @RestController 15 | @RequestMapping("/alarm/") 16 | public class AlarmManageController extends BaseController { 17 | 18 | private IAlarmManageService alarmManageService; 19 | 20 | @Autowired 21 | public void setAlarmManageService(IAlarmManageService alarmManageService) { 22 | this.alarmManageService = alarmManageService; 23 | } 24 | 25 | 26 | @RequestMapping(value = "insert", method = RequestMethod.POST) 27 | public ResponseListEntity insertAlarm(@RequestBody AlarmInfoInsetParam param) { 28 | ResponseListEntity responseDataEntity = new ResponseListEntity<>(); 29 | alarmManageService.insertAlarm(param); 30 | return responseDataEntity; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/controller/UserManageController.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.controller; 2 | 3 | import com.tuacy.microservice.framework.common.controller.BaseController; 4 | import com.tuacy.microservice.framework.common.entity.response.ResponseListEntity; 5 | import com.tuacy.microservice.framework.common.entity.response.ResponsePageEntity; 6 | import com.tuacy.mybatis.interceptor.entity.param.GetAllUserParam; 7 | import com.tuacy.mybatis.interceptor.entity.param.GetUserPageParam; 8 | import com.tuacy.mybatis.interceptor.entity.vo.UserInfoVo; 9 | import com.tuacy.mybatis.interceptor.interceptor.page.PageView; 10 | import com.tuacy.mybatis.interceptor.service.IUserManageService; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.web.bind.annotation.RequestBody; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestMethod; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | @RestController 18 | @RequestMapping("/user/") 19 | public class UserManageController extends BaseController { 20 | 21 | private IUserManageService userManageService; 22 | 23 | @Autowired 24 | public void setUserManageService(IUserManageService userManageService) { 25 | this.userManageService = userManageService; 26 | } 27 | 28 | /** 29 | * 常规请求,不做分页 30 | */ 31 | @RequestMapping(value = "list", method = RequestMethod.POST) 32 | public ResponseListEntity getAllUser(@RequestBody GetAllUserParam param) { 33 | ResponseListEntity responseDataEntity = new ResponseListEntity<>(); 34 | try { 35 | responseDataEntity.setData(userManageService.getAllUserList()); 36 | } catch (Exception e) { 37 | responseDataEntity.setStatus(-1); 38 | } 39 | return responseDataEntity; 40 | } 41 | 42 | /** 43 | * 分页请求 44 | */ 45 | @RequestMapping(value = "listPage", method = RequestMethod.POST) 46 | public ResponsePageEntity getUserPage(@RequestBody GetUserPageParam param) { 47 | ResponsePageEntity responseDataEntity = new ResponsePageEntity<>(); 48 | try { 49 | PageView pageView = new PageView<>(param.getPageIndex(), param.getPageSize()); 50 | PageView pageViewResult = userManageService.getUserListPage(pageView); 51 | responseDataEntity.setPageCount(pageViewResult.getTotalPage()); 52 | responseDataEntity.setPageIndex(pageViewResult.getPageNo()); 53 | responseDataEntity.setPageSize(pageViewResult.getPageSize()); 54 | responseDataEntity.setTotalCount(pageViewResult.getTotalCount()); 55 | responseDataEntity.setData(pageViewResult.getLists()); 56 | } catch (Exception e) { 57 | responseDataEntity.setStatus(-1); 58 | } 59 | return responseDataEntity; 60 | } 61 | 62 | /** 63 | * 分页请求,并且子定义count查询 64 | */ 65 | @RequestMapping(value = "listPageManualCount", method = RequestMethod.POST) 66 | public ResponsePageEntity getUserPageManualCount(@RequestBody GetUserPageParam param) { 67 | ResponsePageEntity responseDataEntity = new ResponsePageEntity<>(); 68 | try { 69 | PageView pageView = new PageView<>(param.getPageIndex(), param.getPageSize()); 70 | PageView pageViewResult = userManageService.getUserListPageManualCount(pageView); 71 | responseDataEntity.setPageCount(pageViewResult.getTotalPage()); 72 | responseDataEntity.setPageIndex(pageViewResult.getPageNo()); 73 | responseDataEntity.setPageSize(pageViewResult.getPageSize()); 74 | responseDataEntity.setTotalCount(pageViewResult.getTotalCount()); 75 | responseDataEntity.setData(pageViewResult.getLists()); 76 | } catch (Exception e) { 77 | responseDataEntity.setStatus(-1); 78 | } 79 | return responseDataEntity; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/entity/param/AlarmInfoInsetParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.entity.param; 2 | 3 | public class AlarmInfoInsetParam { 4 | 5 | private String alarmEventName; 6 | private int alarmLevel; 7 | private String alarmOccurTime; 8 | 9 | public String getAlarmEventName() { 10 | return alarmEventName; 11 | } 12 | 13 | public void setAlarmEventName(String alarmEventName) { 14 | this.alarmEventName = alarmEventName; 15 | } 16 | 17 | public int getAlarmLevel() { 18 | return alarmLevel; 19 | } 20 | 21 | public void setAlarmLevel(int alarmLevel) { 22 | this.alarmLevel = alarmLevel; 23 | } 24 | 25 | public String getAlarmOccurTime() { 26 | return alarmOccurTime; 27 | } 28 | 29 | public void setAlarmOccurTime(String alarmOccurTime) { 30 | this.alarmOccurTime = alarmOccurTime; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/entity/param/GetAllUserParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.entity.param; 2 | 3 | public class GetAllUserParam { 4 | } 5 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/entity/param/GetUserPageParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.entity.param; 2 | 3 | import com.tuacy.microservice.framework.common.entity.param.BasePageParam; 4 | 5 | public class GetUserPageParam extends BasePageParam { 6 | } 7 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/entity/vo/AlarmInfoVo.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.entity.vo; 2 | 3 | public class AlarmInfoVo { 4 | 5 | private long alarmEventId; 6 | private String alarmEventName; 7 | private int alarmLevel; 8 | private String alarmOccurTime; 9 | 10 | public long getAlarmEventId() { 11 | return alarmEventId; 12 | } 13 | 14 | public void setAlarmEventId(long alarmEventId) { 15 | this.alarmEventId = alarmEventId; 16 | } 17 | 18 | public String getAlarmEventName() { 19 | return alarmEventName; 20 | } 21 | 22 | public void setAlarmEventName(String alarmEventName) { 23 | this.alarmEventName = alarmEventName; 24 | } 25 | 26 | public int getAlarmLevel() { 27 | return alarmLevel; 28 | } 29 | 30 | public void setAlarmLevel(int alarmLevel) { 31 | this.alarmLevel = alarmLevel; 32 | } 33 | 34 | public String getAlarmOccurTime() { 35 | return alarmOccurTime; 36 | } 37 | 38 | public void setAlarmOccurTime(String alarmOccurTime) { 39 | this.alarmOccurTime = alarmOccurTime; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/entity/vo/UserInfoVo.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.entity.vo; 2 | 3 | public class UserInfoVo { 4 | 5 | private String pkId; 6 | private String userName; 7 | private String password; 8 | private String phone; 9 | 10 | public String getPkId() { 11 | return pkId; 12 | } 13 | 14 | public void setPkId(String pkId) { 15 | this.pkId = pkId; 16 | } 17 | 18 | public String getUserName() { 19 | return userName; 20 | } 21 | 22 | public void setUserName(String userName) { 23 | this.userName = userName; 24 | } 25 | 26 | public String getPassword() { 27 | return password; 28 | } 29 | 30 | public void setPassword(String password) { 31 | this.password = password; 32 | } 33 | 34 | public String getPhone() { 35 | return phone; 36 | } 37 | 38 | public void setPhone(String phone) { 39 | this.phone = phone; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/encryptresultfield/EncryptResultFieldAnnotation.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.encryptresultfield; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * 通过注解来表明,我们需要对那个字段进行加密 7 | */ 8 | @Target({ElementType.METHOD}) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | @Inherited 11 | @Documented 12 | public @interface EncryptResultFieldAnnotation { 13 | 14 | /** 15 | * 加密策略 -- 和加密字段,一一对应 16 | */ 17 | Class[] encryptStrategy() default {}; 18 | 19 | /** 20 | * 加密字段对应的key 21 | */ 22 | String[] fieldKey() default {}; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/encryptresultfield/EncryptResultFieldInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.encryptresultfield; 2 | 3 | import org.apache.ibatis.executor.resultset.ResultSetHandler; 4 | import org.apache.ibatis.mapping.MappedStatement; 5 | import org.apache.ibatis.plugin.*; 6 | import org.apache.ibatis.reflection.DefaultReflectorFactory; 7 | import org.apache.ibatis.reflection.MetaObject; 8 | import org.apache.ibatis.reflection.ReflectorFactory; 9 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 10 | import org.apache.ibatis.reflection.factory.ObjectFactory; 11 | import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; 12 | import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; 13 | 14 | import java.lang.reflect.Method; 15 | import java.sql.Statement; 16 | import java.util.*; 17 | 18 | /** 19 | * 通过拦截器对返回结果中的某个字段进行加密处理 20 | */ 21 | @Intercepts({ 22 | @Signature( 23 | type = ResultSetHandler.class, 24 | method = "handleResultSets", 25 | args = {Statement.class} 26 | ) 27 | }) 28 | public class EncryptResultFieldInterceptor implements Interceptor { 29 | 30 | private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory(); 31 | private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory(); 32 | private static final ReflectorFactory REFLECTOR_FACTORY = new DefaultReflectorFactory(); 33 | 34 | @Override 35 | public Object intercept(Invocation invocation) throws Throwable { 36 | // 获取到返回结果 37 | ResultSetHandler resultSetHandler = (ResultSetHandler) invocation.getTarget(); 38 | MetaObject metaResultSetHandler = MetaObject.forObject(resultSetHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, REFLECTOR_FACTORY); 39 | MappedStatement mappedStatement = (MappedStatement) metaResultSetHandler.getValue("mappedStatement"); 40 | EncryptResultFieldAnnotation annotation = getEncryptResultFieldAnnotation(mappedStatement); 41 | Object returnValue = invocation.proceed(); 42 | if (annotation != null && returnValue != null) { 43 | String[] fieldKeyList = annotation.fieldKey(); 44 | Class[] strategyClassList = annotation.encryptStrategy(); 45 | if (strategyClassList.length != 0 && fieldKeyList.length == strategyClassList.length) { 46 | Map> strategyMap = null; 47 | for (int index = 0; index < fieldKeyList.length; index++) { 48 | if (strategyMap == null) { 49 | strategyMap = new HashMap<>(); 50 | } 51 | strategyMap.put(fieldKeyList[index], strategyClassList[index]); 52 | } 53 | // 对结果进行处理 54 | try { 55 | if (returnValue instanceof ArrayList) { 56 | List list = (ArrayList) returnValue; 57 | for (int index = 0; index < list.size(); index++) { 58 | Object returnItem = list.get(index); 59 | if (returnItem instanceof String) { 60 | List stringList = (List) list; 61 | IEncryptResultFieldStrategy encryptStrategy = strategyMap.get(fieldKeyList[0]).newInstance(); 62 | stringList.set(index, encryptStrategy.encrypt((String) returnItem)); 63 | } else { 64 | MetaObject metaReturnItem = MetaObject.forObject(returnItem, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, REFLECTOR_FACTORY); 65 | for (Map.Entry> entry : strategyMap.entrySet()) { 66 | String fieldKey = entry.getKey(); 67 | IEncryptResultFieldStrategy fieldEncryptStrategy = entry.getValue().newInstance(); 68 | Object fieldValue = metaReturnItem.getValue(fieldKey); 69 | if (fieldValue instanceof String) { 70 | metaReturnItem.setValue(fieldKey, fieldEncryptStrategy.encrypt((String) fieldValue)); 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } catch (Exception e) { 77 | // ignore 78 | } 79 | 80 | } 81 | } 82 | return returnValue; 83 | 84 | } 85 | 86 | @Override 87 | public Object plugin(Object target) { 88 | return Plugin.wrap(target, this); 89 | } 90 | 91 | @Override 92 | public void setProperties(Properties properties) { 93 | 94 | } 95 | 96 | /** 97 | * 获取方法上的EncryptResultFieldAnnotation注解 98 | * 99 | * @param mappedStatement MappedStatement 100 | * @return EncryptResultFieldAnnotation注解 101 | */ 102 | private EncryptResultFieldAnnotation getEncryptResultFieldAnnotation(MappedStatement mappedStatement) { 103 | EncryptResultFieldAnnotation encryptResultFieldAnnotation = null; 104 | try { 105 | String id = mappedStatement.getId(); 106 | String className = id.substring(0, id.lastIndexOf(".")); 107 | String methodName = id.substring(id.lastIndexOf(".") + 1); 108 | final Method[] method = Class.forName(className).getMethods(); 109 | for (Method me : method) { 110 | if (me.getName().equals(methodName) && me.isAnnotationPresent(EncryptResultFieldAnnotation.class)) { 111 | encryptResultFieldAnnotation = me.getAnnotation(EncryptResultFieldAnnotation.class); 112 | break; 113 | } 114 | } 115 | } catch (Exception ex) { 116 | ex.printStackTrace(); 117 | } 118 | return encryptResultFieldAnnotation; 119 | } 120 | 121 | 122 | } 123 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/encryptresultfield/IEncryptResultFieldStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.encryptresultfield; 2 | 3 | /** 4 | * 加密策略 5 | */ 6 | public interface IEncryptResultFieldStrategy { 7 | 8 | /** 9 | * 返回加密之后的字符 10 | * 11 | * @param source 原始字符 12 | * @return 加密之后的字符 13 | */ 14 | String encrypt(String source); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/log/LogInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.log; 2 | 3 | import org.apache.ibatis.executor.Executor; 4 | import org.apache.ibatis.mapping.BoundSql; 5 | import org.apache.ibatis.mapping.MappedStatement; 6 | import org.apache.ibatis.mapping.ParameterMapping; 7 | import org.apache.ibatis.plugin.*; 8 | import org.apache.ibatis.reflection.MetaObject; 9 | import org.apache.ibatis.session.Configuration; 10 | import org.apache.ibatis.session.ResultHandler; 11 | import org.apache.ibatis.session.RowBounds; 12 | import org.apache.ibatis.type.TypeHandlerRegistry; 13 | 14 | import java.text.DateFormat; 15 | import java.util.Date; 16 | import java.util.List; 17 | import java.util.Locale; 18 | import java.util.Properties; 19 | 20 | 21 | /** 22 | * 1. 打印mysql完整的执行语句 23 | * 2. 打印mysql语句执行时间 24 | * 这里我们拦截Executor里面的query和update方法 25 | */ 26 | @Intercepts({ 27 | @Signature( 28 | method = "query", 29 | type = Executor.class, 30 | args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class} 31 | ), 32 | @Signature( 33 | type = Executor.class, 34 | method = "update", 35 | args = {MappedStatement.class, Object.class} 36 | ) 37 | }) 38 | public class LogInterceptor implements Interceptor { 39 | 40 | /** 41 | * 是否显示语句的执行时间 42 | */ 43 | public static final String PROPERTIES_KEY_ENABLE_EXECUTOR_TIME = "enableExecutorTIme"; 44 | public static final String ENABLE_EXECUTOR_TIME = "0"; // 显示 45 | 46 | private boolean enableExecutorTime = false; 47 | 48 | @Override 49 | public Object intercept(Invocation invocation) throws Throwable { 50 | // 获取执行方法的MappedStatement参数,不管是Executor的query方法还是update方法,第一个参数都是MappedStatement 51 | MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; 52 | Object parameter = null; 53 | if (invocation.getArgs().length > 1) { 54 | parameter = invocation.getArgs()[1]; 55 | } 56 | String sqlId = mappedStatement.getId(); 57 | BoundSql boundSql = mappedStatement.getBoundSql(parameter); 58 | Configuration configuration = mappedStatement.getConfiguration(); 59 | long sqlStartTime = System.currentTimeMillis(); 60 | Object re = invocation.proceed(); 61 | long sqlEndTime = System.currentTimeMillis(); 62 | // 打印mysql执行语句 63 | String sql = getSql(configuration, boundSql, sqlId); 64 | System.out.println(sql); 65 | // 打印mysql执行时间 66 | if (enableExecutorTime) { 67 | String sqlTimeLog = sqlId + " 方法对应sql执行时间:" + (sqlEndTime - sqlStartTime) + " ms"; 68 | System.out.println(sqlTimeLog); 69 | } 70 | return re; 71 | } 72 | 73 | /** 74 | * 通过该方法决定要返回的对象是目标对象还是对应的代理 75 | * 不要想的太复杂,一般就两种情况: 76 | *

77 | * 1. return target; 直接返回目标对象,相当于当前Interceptor没起作用,不会调用上面的intercept()方法 78 | * 2. return Plugin.wrap(target, this); 返回代理对象,会调用上面的intercept()方法 79 | * 80 | * @param target 目标对象 81 | * @return 目标对象或者代理对象 82 | */ 83 | @Override 84 | public Object plugin(Object target) { 85 | return Plugin.wrap(target, this); 86 | } 87 | 88 | /** 89 | * 用于获取在Configuration初始化当前的Interceptor时时候设置的一些参数 90 | * 91 | * @param properties Properties参数 92 | */ 93 | @Override 94 | public void setProperties(Properties properties) { 95 | if (properties != null) { 96 | String executorTImeValue = properties.getProperty(PROPERTIES_KEY_ENABLE_EXECUTOR_TIME); 97 | if (executorTImeValue != null) { 98 | enableExecutorTime = executorTImeValue.equals(ENABLE_EXECUTOR_TIME); 99 | } 100 | } 101 | } 102 | 103 | private static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) { 104 | return sqlId + " 方法对应sql执行语句:" + assembleSql(configuration, boundSql); 105 | } 106 | 107 | /** 108 | * 转义正则特殊字符 ($()*+.[]?\^{} 109 | * \\需要第一个替换,否则replace方法替换时会有逻辑bug 110 | */ 111 | private static String makeQueryStringAllRegExp(String str) { 112 | if (str != null && !str.equals("")) { 113 | return str.replace("\\", "\\\\").replace("*", "\\*") 114 | .replace("+", "\\+").replace("|", "\\|") 115 | .replace("{", "\\{").replace("}", "\\}") 116 | .replace("(", "\\(").replace(")", "\\)") 117 | .replace("^", "\\^").replace("$", "\\$") 118 | .replace("[", "\\[").replace("]", "\\]") 119 | .replace("?", "\\?").replace(",", "\\,") 120 | .replace(".", "\\.").replace("&", "\\&"); 121 | } 122 | return str; 123 | } 124 | 125 | /** 126 | * 获取参数对应的string值 127 | * 128 | * @param obj 参数对应的值 129 | * @return string 130 | */ 131 | private static String getParameterValue(Object obj) { 132 | String value; 133 | if (obj instanceof String) { 134 | value = "'" + obj.toString() + "'"; 135 | } else if (obj instanceof Date) { 136 | DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); 137 | value = "'" + formatter.format(new Date()) + "'"; 138 | } else { 139 | if (obj != null) { 140 | value = obj.toString(); 141 | } else { 142 | value = ""; 143 | } 144 | 145 | } 146 | // 对特殊字符进行转义,方便之后处理替换 147 | return value != null ? makeQueryStringAllRegExp(value) : value; 148 | } 149 | 150 | /** 151 | * 组装完整的sql语句 -- 把对应的参数都代入到sql语句里面 152 | * 153 | * @param configuration Configuration 154 | * @param boundSql BoundSql 155 | * @return sql完整语句 156 | */ 157 | private static String assembleSql(Configuration configuration, BoundSql boundSql) { 158 | // 获取mapper里面方法上的参数 159 | Object sqlParameter = boundSql.getParameterObject(); 160 | // sql语句里面需要的参数 -- 真实需要用到的参数 因为sqlParameter里面的每个参数不一定都会用到 161 | List parameterMappings = boundSql.getParameterMappings(); 162 | // sql原始语句(?还没有替换成我们具体的参数) 163 | String sql = boundSql.getSql().replaceAll("[\\s]+", " "); 164 | if (parameterMappings.size() > 0 && sqlParameter != null) { 165 | // sql语句里面的?替换成真实的参数 166 | TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); 167 | if (typeHandlerRegistry.hasTypeHandler(sqlParameter.getClass())) { 168 | sql = sql.replaceFirst("\\?", getParameterValue(sqlParameter)); 169 | } else { 170 | MetaObject metaObject = configuration.newMetaObject(sqlParameter); 171 | for (ParameterMapping parameterMapping : parameterMappings) { 172 | // 一个一个把对应的值替换进去 按顺序把?替换成对应的值 173 | String propertyName = parameterMapping.getProperty(); 174 | if (metaObject.hasGetter(propertyName)) { 175 | Object obj = metaObject.getValue(propertyName); 176 | sql = sql.replaceFirst("\\?", getParameterValue(obj)); 177 | } else if (boundSql.hasAdditionalParameter(propertyName)) { 178 | Object obj = boundSql.getAdditionalParameter(propertyName); 179 | sql = sql.replaceFirst("\\?", getParameterValue(obj)); 180 | } 181 | } 182 | } 183 | } 184 | return sql; 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/page/PageView.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.page; 2 | 3 | import java.util.List; 4 | 5 | public class PageView { 6 | 7 | /** 8 | * 默认每一页的数据大小 9 | */ 10 | private static final int PAGE_SIZE = 10; 11 | 12 | /** 13 | * 当前需要查询的页码 ,从0开始 14 | */ 15 | private int pageNo; 16 | /** 17 | * 每页显示几条记录 18 | */ 19 | private int pageSize; 20 | /** 21 | * 总记录数 22 | */ 23 | private long totalCount; 24 | /** 25 | * 是否需要进行count查询,因为在有些情况,不需要每次都进行count查询 26 | */ 27 | private boolean doCount; 28 | /** 29 | * 总页数 30 | */ 31 | private long totalPage; 32 | 33 | /** 34 | * 最终查询出来的数据 35 | */ 36 | private List lists; 37 | 38 | public PageView() { 39 | this(0); 40 | } 41 | 42 | /** 43 | * 使用构造函数,,强制必需输入 当前页 44 | * 45 | * @param pageNo  当前页 46 | */ 47 | public PageView(int pageNo) { 48 | this(pageNo, PAGE_SIZE, true); 49 | } 50 | 51 | /** 52 | * @param pageSize 每一页的大小 53 | * @param pageNo 当前查询第几页,从1开始 54 | */ 55 | public PageView(int pageNo, int pageSize) { 56 | this(pageNo, pageSize, true); 57 | } 58 | 59 | /** 60 | * @param pageSize 每一页的大小 61 | * @param pageNo 当前查询第几页,从1开始 62 | * @param doCount 是否需要进行count查询 63 | */ 64 | public PageView(int pageNo, int pageSize, boolean doCount) { 65 | this.pageNo = pageNo; 66 | this.pageSize = pageSize; 67 | this.doCount = doCount; 68 | } 69 | 70 | /** 71 | * 查询结果方法 把 记录数 结果集合 放入到 PageView对象 72 | * 73 | * @param rowCount 总记录数 74 | * @param records 结果集合 75 | */ 76 | 77 | public void setQueryResult(long rowCount, List records) { 78 | setLists(records); 79 | } 80 | 81 | 82 | public List getLists() { 83 | return lists; 84 | } 85 | 86 | public void setLists(List lists) { 87 | this.lists = lists; 88 | } 89 | 90 | public int getPageNo() { 91 | return pageNo; 92 | } 93 | 94 | public void setPageNo(int pageNo) { 95 | this.pageNo = pageNo; 96 | } 97 | 98 | public int getPageSize() { 99 | return pageSize; 100 | } 101 | 102 | public void setPageSize(int pageSize) { 103 | this.pageSize = pageSize; 104 | } 105 | 106 | public long getTotalCount() { 107 | return totalCount; 108 | } 109 | 110 | public void setTotalCount(long totalCount) { 111 | this.totalCount = totalCount; 112 | setTotalPage(this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1); 113 | } 114 | 115 | public long getTotalPage() { 116 | return totalPage; 117 | } 118 | 119 | public void setTotalPage(long totalPage) { 120 | this.totalPage = totalPage; 121 | } 122 | 123 | public boolean isDoCount() { 124 | return doCount; 125 | } 126 | 127 | public void setDoCount(boolean doCount) { 128 | this.doCount = doCount; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/tableshard/DayTableShardStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.tableshard; 2 | 3 | import org.apache.ibatis.mapping.BoundSql; 4 | import org.apache.ibatis.reflection.MetaObject; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | 9 | /** 10 | * 按日分表,根据当前时间 11 | */ 12 | public class DayTableShardStrategy implements ITableShardStrategy { 13 | 14 | /** 15 | * 分表策略 16 | * 17 | * @param metaStatementHandler MetaObject包装的RoutingStatementHandler对象 18 | * @param tableName 原始表名 19 | * @param shardParamKey 可以在mapper文件的方法里面传递一些参数key过来,在分表策略里面通过key获取到对应的值 20 | * @return 包装之后的sql语句 21 | */ 22 | @Override 23 | public String tableShard(MetaObject metaStatementHandler, String tableName, String[] shardParamKey) { 24 | BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); 25 | String originSql = boundSql.getSql(); 26 | // 当前时间 27 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 28 | String newTableName = tableName + "_" + sdf.format(new Date()); 29 | return originSql.replaceAll(tableName, newTableName); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/tableshard/ITableShardStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.tableshard; 2 | 3 | import org.apache.ibatis.reflection.MetaObject; 4 | 5 | /** 6 | * 分表策略 7 | */ 8 | public interface ITableShardStrategy { 9 | 10 | /** 11 | * 分表策略 12 | * 13 | * @param metaStatementHandler MetaObject包装的RoutingStatementHandler对象 14 | * @param tableName 原始表名 15 | * @param shardParamKey 可以在mapper文件的方法里面传递一些参数key过来,在分表策略里面通过key获取到对应的值 16 | * @return 包装之后的sql语句 17 | */ 18 | String tableShard(MetaObject metaStatementHandler, String tableName, String[] shardParamKey) throws Exception; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/tableshard/MonthTableShardStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.tableshard; 2 | 3 | import org.apache.ibatis.mapping.BoundSql; 4 | import org.apache.ibatis.reflection.MetaObject; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | 9 | /** 10 | * 按月分表,根据当前时间 11 | */ 12 | public class MonthTableShardStrategy implements ITableShardStrategy { 13 | 14 | @Override 15 | public String tableShard(MetaObject metaStatementHandler, String tableName, String[] shardParamKey) { 16 | 17 | BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); 18 | String originSql = boundSql.getSql(); 19 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); 20 | String newTableName = tableName + "_" + sdf.format(new Date()); 21 | return originSql.replaceAll(tableName, newTableName); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/tableshard/TableShardAnnotation.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.tableshard; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * 分表注解 7 | */ 8 | @Target({ElementType.METHOD}) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | @Inherited 11 | @Documented 12 | public @interface TableShardAnnotation { 13 | /** 14 | * 待分表的表名 15 | */ 16 | String tableName(); 17 | 18 | /** 19 | * 分表策略 20 | */ 21 | Class shadeStrategy(); 22 | 23 | /** 24 | * 分表条件key, 通过key去参数列表里取对应的值,作为分表条件处理 25 | */ 26 | String[] shardParamKey() default {}; 27 | } 28 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/tableshard/TableShardInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.tableshard; 2 | 3 | import org.apache.ibatis.executor.statement.RoutingStatementHandler; 4 | import org.apache.ibatis.executor.statement.StatementHandler; 5 | import org.apache.ibatis.mapping.BoundSql; 6 | import org.apache.ibatis.mapping.MappedStatement; 7 | import org.apache.ibatis.plugin.*; 8 | import org.apache.ibatis.reflection.DefaultReflectorFactory; 9 | import org.apache.ibatis.reflection.MetaObject; 10 | import org.apache.ibatis.reflection.ReflectorFactory; 11 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 12 | import org.apache.ibatis.reflection.factory.ObjectFactory; 13 | import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; 14 | import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; 15 | import org.springframework.util.StringUtils; 16 | 17 | import java.lang.reflect.Method; 18 | import java.sql.Connection; 19 | import java.util.Properties; 20 | 21 | /** 22 | * mybatis分表拦截器 -- 水平切分 23 | */ 24 | @Intercepts({ 25 | @Signature( 26 | type = StatementHandler.class, 27 | method = "prepare", 28 | args = {Connection.class, Integer.class} 29 | ) 30 | }) 31 | public class TableShardInterceptor implements Interceptor { 32 | 33 | private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory(); 34 | private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory(); 35 | private static final ReflectorFactory REFLECTOR_FACTORY = new DefaultReflectorFactory(); 36 | 37 | @Override 38 | public Object intercept(Invocation invocation) throws Throwable { 39 | if (invocation.getTarget() instanceof RoutingStatementHandler) { 40 | try { 41 | RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget(); 42 | // MetaObject是mybatis里面提供的一个工具类,类似反射的效果 43 | MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, REFLECTOR_FACTORY); 44 | BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");//获取sql语句 45 | String originSql = boundSql.getSql(); 46 | if (!StringUtils.isEmpty(originSql)) { 47 | MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement"); 48 | // 判断方法上是否添加了 TableShardAnnotation 注解,因为只有添加了TableShard注解的方法我们才会去做分表处理 49 | TableShardAnnotation tableShardAnnotation = getTableShardAnnotation(mappedStatement); 50 | if (tableShardAnnotation != null) { 51 | String tableName = tableShardAnnotation.tableName(); 52 | Class strategyClazz = tableShardAnnotation.shadeStrategy(); 53 | String[] shardParamKey = tableShardAnnotation.shardParamKey(); 54 | ITableShardStrategy tableStrategy = (ITableShardStrategy) strategyClazz.newInstance(); 55 | String newSql = tableStrategy.tableShard(metaStatementHandler, tableName, shardParamKey); 56 | // 把新语句设置回去 57 | metaStatementHandler.setValue("delegate.boundSql.sql", newSql); 58 | } 59 | 60 | } 61 | } catch (Exception e) { 62 | // ignore 任何一个地方有异常都去执行原始操作 -- invocation.proceed() 63 | } 64 | 65 | } 66 | return invocation.proceed(); 67 | } 68 | 69 | @Override 70 | public Object plugin(Object target) { 71 | // 当目标类是StatementHandler类型时,才包装目标类,否者直接返回目标本身,减少目标被代理的次数 72 | return (target instanceof RoutingStatementHandler) ? Plugin.wrap(target, this) : target; 73 | } 74 | 75 | @Override 76 | public void setProperties(Properties properties) { 77 | 78 | } 79 | 80 | /** 81 | * 获取方法上的TableShardAnnotation注解 82 | * 83 | * @param mappedStatement MappedStatement 84 | * @return TableShardAnnotation注解 85 | */ 86 | private TableShardAnnotation getTableShardAnnotation(MappedStatement mappedStatement) { 87 | TableShardAnnotation tableShardAnnotation = null; 88 | try { 89 | String id = mappedStatement.getId(); 90 | String className = id.substring(0, id.lastIndexOf(".")); 91 | String methodName = id.substring(id.lastIndexOf(".") + 1); 92 | final Method[] method = Class.forName(className).getMethods(); 93 | for (Method me : method) { 94 | if (me.getName().equals(methodName) && me.isAnnotationPresent(TableShardAnnotation.class)) { 95 | tableShardAnnotation = me.getAnnotation(TableShardAnnotation.class); 96 | break; 97 | } 98 | } 99 | } catch (Exception ex) { 100 | ex.printStackTrace(); 101 | } 102 | return tableShardAnnotation; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/tableshard/YearTableShardStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.tableshard; 2 | 3 | import org.apache.ibatis.mapping.BoundSql; 4 | import org.apache.ibatis.reflection.MetaObject; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | 9 | /** 10 | * 按年分表,根据当前时间 11 | */ 12 | public class YearTableShardStrategy implements ITableShardStrategy { 13 | 14 | @Override 15 | public String tableShard(MetaObject metaStatementHandler, String tableName, String[] shardParamKey) { 16 | 17 | BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); 18 | String originSql = boundSql.getSql(); 19 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); 20 | String newTableName = tableName + "_" + sdf.format(new Date()); 21 | return originSql.replaceAll(tableName, newTableName); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/interceptor/utils/ReflectHelper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.interceptor.utils; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | /** 6 | * 反射工具 7 | */ 8 | public class ReflectHelper { 9 | 10 | /** 11 | * 获取obj对象fieldName的Field 12 | * 13 | * @param obj 对象 14 | * @param fieldName 字段名称 15 | * @return 字段 16 | */ 17 | private static Field getFieldByFieldName(Object obj, String fieldName) { 18 | if (obj == null || fieldName == null) { 19 | return null; 20 | } 21 | // 遍历父类 22 | for (Class superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { 23 | try { 24 | // 获得某个类的所有声明的字段,即包括public、private和protected,但是不包括父类的申明字段 25 | return superClass.getDeclaredField(fieldName); 26 | } catch (Exception e) { 27 | // 当前对象没有该字段,继续去父类里面查找 28 | } 29 | } 30 | return null; 31 | } 32 | 33 | /** 34 | * 获取obj对象fieldName的属性值 35 | * 36 | * @param obj 对象 37 | * @param fieldName 字段名称 38 | * @return obj对象fieldName的属性值 39 | */ 40 | public static Object getValueByFieldName(Object obj, String fieldName) { 41 | Object value = null; 42 | try { 43 | Field field = getFieldByFieldName(obj, fieldName); 44 | if (field != null) { 45 | // 获取字段对应的值 46 | if (field.isAccessible()) { 47 | value = field.get(obj); 48 | } else { 49 | field.setAccessible(true); 50 | value = field.get(obj); 51 | field.setAccessible(false); 52 | } 53 | } 54 | } catch (Exception e) { 55 | return null; 56 | } 57 | return value; 58 | } 59 | 60 | /** 61 | * 设置obj对象fieldName的属性值 62 | * 63 | * @param obj 对象 64 | * @param fieldName 属性名称 65 | * @param value 属性值 66 | */ 67 | public static boolean setValueByFieldName(Object obj, String fieldName, Object value) { 68 | try { 69 | //java.lang.Class.getDeclaredField()方法用法实例教程 - 方法返回一个Field对象,它反映此Class对象所表示的类或接口的指定已声明字段。 70 | //此方法返回这个类中的指定字段的Field对象 71 | Field field = obj.getClass().getDeclaredField(fieldName); 72 | /** 73 | * public void setAccessible(boolean flag) 74 | * throws SecurityException将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。 75 | * 首先,如果存在安全管理器,则在 ReflectPermission("suppressAccessChecks") 权限下调用 checkPermission 方法。 76 | * 如果 flag 为 true,并且不能更改此对象的可访问性(例如,如果此元素对象是 Class 类的 Constructor 对象),则会引发 SecurityException。 77 | * 如果此对象是 java.lang.Class 类的 Constructor 对象,并且 flag 为 true,则会引发 SecurityException。 78 | * 参数: 79 | * flag - accessible 标志的新值 80 | * 抛出: 81 | * SecurityException - 如果请求被拒绝。 82 | */ 83 | if (field.isAccessible()) {//获取此对象的 accessible 标志的值。 84 | field.set(obj, value);//将指定对象变量上此 Field 对象表示的字段设置为指定的新值 85 | } else { 86 | field.setAccessible(true); 87 | field.set(obj, value); 88 | field.setAccessible(false); 89 | } 90 | return true; 91 | } catch (Exception e) { 92 | e.printStackTrace(); 93 | } 94 | return false; 95 | } 96 | 97 | /** 98 | * 去obj对象里面找某种类型对应的值 99 | * 比如去obj对象里面查找 String类型对应的值 100 | * 101 | * @param obj 对象 102 | * @param fieldType 属性列席 103 | * @return 属性对应值 104 | */ 105 | @SuppressWarnings("unchecked") 106 | public static T getValueByFieldType(Object obj, Class fieldType) { 107 | Object value = null; 108 | for (Class superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { 109 | try { 110 | Field[] fields = superClass.getDeclaredFields(); 111 | for (Field f : fields) { 112 | // 遍历每个属性,判断属性类型是否对应 113 | if (f.getType() == fieldType) { 114 | if (f.isAccessible()) { 115 | value = f.get(obj); 116 | break; 117 | } else { 118 | f.setAccessible(true); 119 | value = f.get(obj); 120 | f.setAccessible(false); 121 | break; 122 | } 123 | } 124 | } 125 | if (value != null) { 126 | // 找到 127 | break; 128 | } 129 | } catch (Exception e) { 130 | // 当前对象没有该字段,继续去父类里面查找 131 | } 132 | } 133 | return (T) value; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/mapper/AlarmManageMapper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.mapper; 2 | 3 | import com.tuacy.mybatis.interceptor.entity.param.AlarmInfoInsetParam; 4 | import com.tuacy.mybatis.interceptor.interceptor.tableshard.TableShardAnnotation; 5 | import com.tuacy.mybatis.interceptor.strategy.AlarmTableShardStrategy; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.springframework.stereotype.Repository; 8 | 9 | /** 10 | * 告警管理mapper 11 | */ 12 | @Repository 13 | public interface AlarmManageMapper { 14 | 15 | @TableShardAnnotation(tableName = "tablealarmevent", shadeStrategy = AlarmTableShardStrategy.class, shardParamKey = "occurTime") 16 | long insertAlarm(@Param("param") AlarmInfoInsetParam param, @Param("occurTime") String occurTIme); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/mapper/UserManageMapper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.mapper; 2 | 3 | import com.tuacy.mybatis.interceptor.entity.vo.UserInfoVo; 4 | import com.tuacy.mybatis.interceptor.interceptor.encryptresultfield.EncryptResultFieldAnnotation; 5 | import com.tuacy.mybatis.interceptor.interceptor.page.PageView; 6 | import com.tuacy.mybatis.interceptor.strategy.PasswordEncryptStrategy; 7 | import org.apache.ibatis.annotations.Param; 8 | import org.springframework.stereotype.Repository; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * 用户管理mapper 14 | */ 15 | @Repository 16 | public interface UserManageMapper { 17 | 18 | @EncryptResultFieldAnnotation(fieldKey = "password", encryptStrategy = PasswordEncryptStrategy.class) 19 | List getAllUserList(); 20 | 21 | List getAllUserListPage(@Param("pageView") PageView pageView); 22 | 23 | List getAllUserListPageManualCount(@Param("pageView") PageView pageView); 24 | 25 | /** 26 | * 和getAllUserListPageManualCount查询对应,自定义一个count查询语句 27 | */ 28 | Long getAllUserListPageManualCount_COUNT(@Param("pageView") PageView pageView); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.mapper; 2 | 3 | import com.tuacy.mybatis.interceptor.model.User; 4 | 5 | public interface UserMapper { 6 | /** 7 | * This method was generated by MyBatis Generator. 8 | * This method corresponds to the database table user 9 | * 10 | * @mbggenerated 11 | */ 12 | int deleteByPrimaryKey(Short pkid); 13 | 14 | /** 15 | * This method was generated by MyBatis Generator. 16 | * This method corresponds to the database table user 17 | * 18 | * @mbggenerated 19 | */ 20 | int insert(User record); 21 | 22 | /** 23 | * This method was generated by MyBatis Generator. 24 | * This method corresponds to the database table user 25 | * 26 | * @mbggenerated 27 | */ 28 | int insertSelective(User record); 29 | 30 | /** 31 | * This method was generated by MyBatis Generator. 32 | * This method corresponds to the database table user 33 | * 34 | * @mbggenerated 35 | */ 36 | User selectByPrimaryKey(Short pkid); 37 | 38 | /** 39 | * This method was generated by MyBatis Generator. 40 | * This method corresponds to the database table user 41 | * 42 | * @mbggenerated 43 | */ 44 | int updateByPrimaryKeySelective(User record); 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method corresponds to the database table user 49 | * 50 | * @mbggenerated 51 | */ 52 | int updateByPrimaryKey(User record); 53 | } -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/model/User.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | /** 7 | * This field was generated by MyBatis Generator. 8 | * This field corresponds to the database column user.pkid 9 | * 10 | * @mbggenerated 11 | */ 12 | private Short pkid; 13 | 14 | /** 15 | * This field was generated by MyBatis Generator. 16 | * This field corresponds to the database column user.name 17 | * 18 | * @mbggenerated 19 | */ 20 | private String name; 21 | 22 | /** 23 | * This field was generated by MyBatis Generator. 24 | * This field corresponds to the database column user.phone 25 | * 26 | * @mbggenerated 27 | */ 28 | private String phone; 29 | 30 | /** 31 | * This field was generated by MyBatis Generator. 32 | * This field corresponds to the database column user.password 33 | * 34 | * @mbggenerated 35 | */ 36 | private String password; 37 | 38 | /** 39 | * This field was generated by MyBatis Generator. 40 | * This field corresponds to the database table user 41 | * 42 | * @mbggenerated 43 | */ 44 | private static final long serialVersionUID = 1L; 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method returns the value of the database column user.pkid 49 | * 50 | * @return the value of user.pkid 51 | * 52 | * @mbggenerated 53 | */ 54 | public Short getPkid() { 55 | return pkid; 56 | } 57 | 58 | /** 59 | * This method was generated by MyBatis Generator. 60 | * This method sets the value of the database column user.pkid 61 | * 62 | * @param pkid the value for user.pkid 63 | * 64 | * @mbggenerated 65 | */ 66 | public void setPkid(Short pkid) { 67 | this.pkid = pkid; 68 | } 69 | 70 | /** 71 | * This method was generated by MyBatis Generator. 72 | * This method returns the value of the database column user.name 73 | * 74 | * @return the value of user.name 75 | * 76 | * @mbggenerated 77 | */ 78 | public String getName() { 79 | return name; 80 | } 81 | 82 | /** 83 | * This method was generated by MyBatis Generator. 84 | * This method sets the value of the database column user.name 85 | * 86 | * @param name the value for user.name 87 | * 88 | * @mbggenerated 89 | */ 90 | public void setName(String name) { 91 | this.name = name == null ? null : name.trim(); 92 | } 93 | 94 | /** 95 | * This method was generated by MyBatis Generator. 96 | * This method returns the value of the database column user.phone 97 | * 98 | * @return the value of user.phone 99 | * 100 | * @mbggenerated 101 | */ 102 | public String getPhone() { 103 | return phone; 104 | } 105 | 106 | /** 107 | * This method was generated by MyBatis Generator. 108 | * This method sets the value of the database column user.phone 109 | * 110 | * @param phone the value for user.phone 111 | * 112 | * @mbggenerated 113 | */ 114 | public void setPhone(String phone) { 115 | this.phone = phone == null ? null : phone.trim(); 116 | } 117 | 118 | /** 119 | * This method was generated by MyBatis Generator. 120 | * This method returns the value of the database column user.password 121 | * 122 | * @return the value of user.password 123 | * 124 | * @mbggenerated 125 | */ 126 | public String getPassword() { 127 | return password; 128 | } 129 | 130 | /** 131 | * This method was generated by MyBatis Generator. 132 | * This method sets the value of the database column user.password 133 | * 134 | * @param password the value for user.password 135 | * 136 | * @mbggenerated 137 | */ 138 | public void setPassword(String password) { 139 | this.password = password == null ? null : password.trim(); 140 | } 141 | 142 | /** 143 | * This method was generated by MyBatis Generator. 144 | * This method corresponds to the database table user 145 | * 146 | * @mbggenerated 147 | */ 148 | @Override 149 | public boolean equals(Object that) { 150 | if (this == that) { 151 | return true; 152 | } 153 | if (that == null) { 154 | return false; 155 | } 156 | if (getClass() != that.getClass()) { 157 | return false; 158 | } 159 | User other = (User) that; 160 | return (this.getPkid() == null ? other.getPkid() == null : this.getPkid().equals(other.getPkid())) 161 | && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) 162 | && (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone())) 163 | && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())); 164 | } 165 | 166 | /** 167 | * This method was generated by MyBatis Generator. 168 | * This method corresponds to the database table user 169 | * 170 | * @mbggenerated 171 | */ 172 | @Override 173 | public int hashCode() { 174 | final int prime = 31; 175 | int result = 1; 176 | result = prime * result + ((getPkid() == null) ? 0 : getPkid().hashCode()); 177 | result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); 178 | result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode()); 179 | result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode()); 180 | return result; 181 | } 182 | 183 | /** 184 | * This method was generated by MyBatis Generator. 185 | * This method corresponds to the database table user 186 | * 187 | * @mbggenerated 188 | */ 189 | @Override 190 | public String toString() { 191 | StringBuilder sb = new StringBuilder(); 192 | sb.append(getClass().getSimpleName()); 193 | sb.append(" ["); 194 | sb.append("Hash = ").append(hashCode()); 195 | sb.append(", pkid=").append(pkid); 196 | sb.append(", name=").append(name); 197 | sb.append(", phone=").append(phone); 198 | sb.append(", password=").append(password); 199 | sb.append(", serialVersionUID=").append(serialVersionUID); 200 | sb.append("]"); 201 | return sb.toString(); 202 | } 203 | } -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/service/IAlarmManageService.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.service; 2 | 3 | import com.tuacy.mybatis.interceptor.entity.param.AlarmInfoInsetParam; 4 | 5 | public interface IAlarmManageService { 6 | 7 | /** 8 | * 插入一条告警 9 | * @param param 告警信息 10 | */ 11 | void insertAlarm(AlarmInfoInsetParam param); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/service/IUserManageService.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.service; 2 | 3 | import com.tuacy.mybatis.interceptor.entity.vo.UserInfoVo; 4 | import com.tuacy.mybatis.interceptor.interceptor.page.PageView; 5 | 6 | import java.util.List; 7 | 8 | public interface IUserManageService { 9 | 10 | /** 11 | * 获取所有的用户列表信息 12 | */ 13 | List getAllUserList(); 14 | 15 | 16 | /** 17 | * 分页获取所有的用户列表信息 18 | */ 19 | PageView getUserListPage(PageView pageView); 20 | 21 | 22 | /** 23 | * 分页获取所有的用户列表信息 -- 自定义count查询 24 | */ 25 | PageView getUserListPageManualCount(PageView pageView); 26 | } 27 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/service/impl/AlarmManageServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.service.impl; 2 | 3 | import com.tuacy.mybatis.interceptor.entity.param.AlarmInfoInsetParam; 4 | import com.tuacy.mybatis.interceptor.mapper.AlarmManageMapper; 5 | import com.tuacy.mybatis.interceptor.service.IAlarmManageService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | @Service(value = "alarm-manage") 11 | public class AlarmManageServiceImpl implements IAlarmManageService { 12 | 13 | private AlarmManageMapper alarmManageMapper; 14 | 15 | @Autowired 16 | public void setAlarmManageMapper(AlarmManageMapper alarmManageMapper) { 17 | this.alarmManageMapper = alarmManageMapper; 18 | } 19 | 20 | /** 21 | * 插入一条告警 22 | * @param param 告警信息 23 | */ 24 | @Transactional(rollbackFor = Exception.class) 25 | @Override 26 | public void insertAlarm(AlarmInfoInsetParam param) { 27 | alarmManageMapper.insertAlarm(param, param.getAlarmOccurTime()); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/service/impl/UserManageServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.service.impl; 2 | 3 | import com.tuacy.mybatis.interceptor.entity.vo.UserInfoVo; 4 | import com.tuacy.mybatis.interceptor.interceptor.page.PageView; 5 | import com.tuacy.mybatis.interceptor.mapper.UserManageMapper; 6 | import com.tuacy.mybatis.interceptor.service.IUserManageService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import java.util.List; 12 | 13 | @Service(value = "user-manage") 14 | public class UserManageServiceImpl implements IUserManageService { 15 | 16 | private UserManageMapper userManageMapper; 17 | 18 | @Autowired 19 | public void setUserManageMapper(UserManageMapper userManageMapper) { 20 | this.userManageMapper = userManageMapper; 21 | } 22 | 23 | @Transactional(rollbackFor = Exception.class, readOnly = true) 24 | @Override 25 | public List getAllUserList() { 26 | return userManageMapper.getAllUserList(); 27 | } 28 | 29 | /** 30 | * 分页获取所有的用户列表信息 31 | */ 32 | @Transactional(rollbackFor = Exception.class, readOnly = true) 33 | @Override 34 | public PageView getUserListPage(PageView pageView) { 35 | pageView.setLists(userManageMapper.getAllUserListPage(pageView)); 36 | return pageView; 37 | } 38 | 39 | 40 | /** 41 | * 分页获取所有的用户列表信息 -- 自定义count查询 42 | */ 43 | @Transactional(rollbackFor = Exception.class, readOnly = true) 44 | @Override 45 | public PageView getUserListPageManualCount(PageView pageView) { 46 | pageView.setLists(userManageMapper.getAllUserListPageManualCount(pageView)); 47 | return pageView; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/strategy/AlarmTableShardStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.strategy; 2 | 3 | import com.tuacy.mybatis.interceptor.interceptor.tableshard.ITableShardStrategy; 4 | import org.apache.ibatis.mapping.BoundSql; 5 | import org.apache.ibatis.reflection.DefaultReflectorFactory; 6 | import org.apache.ibatis.reflection.MetaObject; 7 | import org.apache.ibatis.reflection.ReflectorFactory; 8 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 9 | import org.apache.ibatis.reflection.factory.ObjectFactory; 10 | import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; 11 | import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; 12 | 13 | import java.text.SimpleDateFormat; 14 | import java.util.Date; 15 | import java.util.Map; 16 | import java.util.Set; 17 | 18 | /** 19 | * 告警表分表策略 -- 根据告警发生时间来分表,天 20 | * shardParamKey 对应时间格式 yyyy-MM-dd HH:mm:ss 21 | */ 22 | public class AlarmTableShardStrategy implements ITableShardStrategy { 23 | 24 | private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory(); 25 | private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory(); 26 | private static final ReflectorFactory REFLECTOR_FACTORY = new DefaultReflectorFactory(); 27 | 28 | /** 29 | * 分表策略 30 | * 31 | * @param metaStatementHandler MetaObject包装的RoutingStatementHandler对象 32 | * @param tableName 原始表名 33 | * @param shardParamKey 可以在mapper文件的方法里面传递一些参数key过来,在分表策略里面通过key获取到对应的值 34 | * @return 包装之后的sql语句 35 | */ 36 | @SuppressWarnings("unchecked") 37 | @Override 38 | public String tableShard(MetaObject metaStatementHandler, String tableName, String[] shardParamKey) throws Exception { 39 | BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); 40 | String originSql = boundSql.getSql(); 41 | if (shardParamKey == null || shardParamKey.length == 0) { 42 | return originSql; 43 | } 44 | String alarmOccurTimeParamKey = shardParamKey[0]; 45 | String alarmOccurTime = null; 46 | Object parameterObject = metaStatementHandler.getValue("delegate.boundSql.parameterObject");//获取参数 47 | if (parameterObject instanceof String) { 48 | // 参数是一个String,那我们就认为这个String就是用来分表的参数了 49 | alarmOccurTime = (String) parameterObject; 50 | } else if (parameterObject instanceof Map) { 51 | // 参数是一个Map 52 | Map map = (Map) parameterObject; 53 | Set set = map.keySet(); 54 | for (String key : set) { 55 | if (key.equals(alarmOccurTimeParamKey)) { 56 | alarmOccurTime = map.get(alarmOccurTimeParamKey).toString(); 57 | break; 58 | } 59 | } 60 | } else { 61 | // 参数为某个对象 62 | MetaObject metaParamObject = MetaObject.forObject(parameterObject, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, REFLECTOR_FACTORY); 63 | alarmOccurTime = (String) metaParamObject.getValue(alarmOccurTimeParamKey); 64 | } 65 | // 确定表名字 66 | if (alarmOccurTime != null) { 67 | SimpleDateFormat parseSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 68 | Date occurDate = parseSdf.parse(alarmOccurTime); 69 | SimpleDateFormat formatSdf = new SimpleDateFormat("yyyyMMdd"); 70 | String shardTableName = tableName + "_" + formatSdf.format(occurDate); 71 | return originSql.replaceAll(tableName, shardTableName); 72 | } 73 | return originSql; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/java/com/tuacy/mybatis/interceptor/strategy/PasswordEncryptStrategy.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.mybatis.interceptor.strategy; 2 | 3 | import com.tuacy.mybatis.interceptor.interceptor.encryptresultfield.IEncryptResultFieldStrategy; 4 | import org.springframework.util.DigestUtils; 5 | 6 | public class PasswordEncryptStrategy implements IEncryptResultFieldStrategy { 7 | @Override 8 | public String encrypt(String source) { 9 | if (source == null) { 10 | return null; 11 | } 12 | return new String(DigestUtils.md5Digest(source.getBytes())); 13 | } 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2223 3 | spring: 4 | application: 5 | name: mybatis-interceptor 6 | datasource: 7 | url: jdbc:mysql://localhost:3306/tuacydev?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false 8 | username: root 9 | password: 123456 10 | driver-class-name: com.mysql.jdbc.Driver 11 | # 使用druid数据源 12 | type: com.alibaba.druid.pool.DruidDataSource 13 | #eureka: 14 | # client: 15 | # serviceUrl: 16 | # defaultZone: http://localhost:8761/eureka/ 17 | # 18 | # instance: 19 | # instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} 20 | # prefer-ip-address: true 21 | # leaseRenewalIntervalInSeconds: 5 22 | # leaseExpirationDurationInSeconds: 5 23 | 24 | mybatis: 25 | typeAliasesPackage: com.tuacy.mybatis.interceptor.model 26 | mapperLocations: classpath:mapper/*.xml 27 | # configuration: 28 | # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 29 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/resources/generator/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 43 | 44 | 45 | 46 | 47 | 48 | 52 |
53 |
54 |
55 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/resources/mapper/AlarmManageMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | insert into tablealarmevent ( 9 | alarmeventid, 10 | alarmeventname, 11 | alarmlevel, 12 | alarmoccurtime 13 | ) values ( 14 | uuid_short(), 15 | #{param.alarmEventName}, 16 | #{param.alarmLevel}, 17 | #{param.alarmOccurTime} 18 | ) 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/resources/mapper/UserManageMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 26 | 27 | 37 | 38 | 39 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /mybatis-interceptor/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 19 | pkid, name, phone, password 20 | 21 | 31 | 32 | 36 | delete from user 37 | where pkid = #{pkid,jdbcType=SMALLINT} 38 | 39 | 40 | 44 | insert into user (pkid, name, phone, 45 | password) 46 | values (#{pkid,jdbcType=SMALLINT}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, 47 | #{password,jdbcType=VARCHAR}) 48 | 49 | 50 | 54 | insert into user 55 | 56 | 57 | pkid, 58 | 59 | 60 | name, 61 | 62 | 63 | phone, 64 | 65 | 66 | password, 67 | 68 | 69 | 70 | 71 | #{pkid,jdbcType=SMALLINT}, 72 | 73 | 74 | #{name,jdbcType=VARCHAR}, 75 | 76 | 77 | #{phone,jdbcType=VARCHAR}, 78 | 79 | 80 | #{password,jdbcType=VARCHAR}, 81 | 82 | 83 | 84 | 85 | 89 | update user 90 | 91 | 92 | name = #{name,jdbcType=VARCHAR}, 93 | 94 | 95 | phone = #{phone,jdbcType=VARCHAR}, 96 | 97 | 98 | password = #{password,jdbcType=VARCHAR}, 99 | 100 | 101 | where pkid = #{pkid,jdbcType=SMALLINT} 102 | 103 | 104 | 108 | update user 109 | set name = #{name,jdbcType=VARCHAR}, 110 | phone = #{phone,jdbcType=VARCHAR}, 111 | password = #{password,jdbcType=VARCHAR} 112 | where pkid = #{pkid,jdbcType=SMALLINT} 113 | 114 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | tuacy.microservice.framweork 8 | microservice-framework 9 | pom 10 | PACKT-SNAPSHOT 11 | 12 | user-manage 13 | framework-common 14 | user-manage-api 15 | quartz-manage 16 | quartz-manage-api 17 | actuator 18 | url-decoration 19 | mybatis-interceptor 20 | 21 | 22 | microservice-framwork 23 | microservice framework project for Spring Boot 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-parent 30 | 2.0.3.RELEASE 31 | 32 | 33 | 34 | UTF-8 35 | UTF-8 36 | 1.8 37 | Finchley.RELEASE 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-dependencies 46 | ${spring-cloud.version} 47 | pom 48 | import 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-starter 59 | 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-starter-test 64 | test 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-starter-web 70 | 71 | 72 | 73 | org.springframework.cloud 74 | spring-cloud-starter-netflix-hystrix 75 | 76 | 77 | 78 | org.springframework.cloud 79 | spring-cloud-starter-netflix-eureka-client 80 | 81 | 82 | 83 | org.springframework.cloud 84 | spring-cloud-starter-openfeign 85 | 86 | 87 | 88 | org.springframework.cloud 89 | spring-cloud-config-client 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | src/main/java 99 | 100 | **/*.xml 101 | **/*.properties 102 | 103 | false 104 | 105 | 106 | src/main/resources 107 | 108 | **/*.yml 109 | **/*.xml 110 | **/*.properties 111 | 112 | false 113 | 114 | 115 | 116 | 117 | 118 | org.springframework.boot 119 | spring-boot-maven-plugin 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | alimaven 129 | http://maven.aliyun.com/nexus/content/repositories/central/ 130 | 131 | true 132 | 133 | 134 | true 135 | 136 | 137 | 138 | 139 | 140 | spring-libs-release 141 | http://repo.spring.io/libs-release-local 142 | 143 | 144 | 145 | 146 | 147 | 148 | spring-libs-release 149 | http://repo.spring.io/libs-release-local 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /quartz-manage-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | microservice-framework 7 | tuacy.microservice.framweork 8 | PACKT-SNAPSHOT 9 | 10 | jar 11 | 4.0.0 12 | 13 | quartz-manage-api 14 | 15 | 16 | 17 | tuacy.microservice.framweork 18 | framework-common 19 | ${parent.version} 20 | 21 | 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-maven-plugin 28 | 29 | 30 | none 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /quartz-manage-api/src/main/java/com/tuacy/microservice/framework/quartz/manage/api/IQuartzManageApi.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.api; 2 | 3 | import com.tuacy.microservice.framework.common.entity.response.ResponseDataEntity; 4 | import com.tuacy.microservice.framework.quartz.manage.param.QuartzJobAddParam; 5 | import com.tuacy.microservice.framework.quartz.manage.param.QuartzJobDeleteParam; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | 10 | @RequestMapping("/quartz/") 11 | public interface IQuartzManageApi { 12 | 13 | 14 | /** 15 | * 添加一个job任务 16 | */ 17 | @RequestMapping(value = "add", method = RequestMethod.POST) 18 | ResponseDataEntity addQuartzJob(@RequestBody QuartzJobAddParam param); 19 | 20 | /** 21 | * 添加一个job任务 22 | */ 23 | @RequestMapping(value = "delete", method = RequestMethod.POST) 24 | ResponseDataEntity deleteQuartzJob(@RequestBody QuartzJobDeleteParam param); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /quartz-manage-api/src/main/java/com/tuacy/microservice/framework/quartz/manage/param/QuartzJobAddParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.param; 2 | 3 | public class QuartzJobAddParam { 4 | private String keyId; 5 | 6 | public String getKeyId() { 7 | return keyId; 8 | } 9 | 10 | public void setKeyId(String keyId) { 11 | this.keyId = keyId; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /quartz-manage-api/src/main/java/com/tuacy/microservice/framework/quartz/manage/param/QuartzJobDeleteParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.param; 2 | 3 | public class QuartzJobDeleteParam { 4 | private String keyId; 5 | 6 | public String getKeyId() { 7 | return keyId; 8 | } 9 | 10 | public void setKeyId(String keyId) { 11 | this.keyId = keyId; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /quartz-manage/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | microservice-framework 7 | tuacy.microservice.framweork 8 | PACKT-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | quartz-manage 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-hateoas 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-sleuth 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-jdbc 36 | 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-quartz 42 | 43 | 44 | 45 | 46 | mysql 47 | mysql-connector-java 48 | 5.1.39 49 | 50 | 51 | 52 | 53 | com.alibaba 54 | druid 55 | 1.0.18 56 | 57 | 58 | 59 | 60 | org.mybatis.spring.boot 61 | mybatis-spring-boot-starter 62 | 1.3.2 63 | 64 | 65 | 66 | 67 | com.github.pagehelper 68 | pagehelper-spring-boot-starter 69 | 70 | 1.2.3 71 | 72 | 73 | 74 | tuacy.microservice.framweork 75 | framework-common 76 | ${parent.version} 77 | 78 | 79 | 80 | tuacy.microservice.framweork 81 | quartz-manage-api 82 | ${parent.version} 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | org.springframework.boot 92 | spring-boot-maven-plugin 93 | 94 | 95 | none 96 | 97 | 98 | 99 | 100 | 101 | org.mybatis.generator 102 | mybatis-generator-maven-plugin 103 | 1.3.2 104 | 105 | 106 | mysql 107 | mysql-connector-java 108 | 5.1.35 109 | 110 | 111 | 112 | ${basedir}/src/main/resources/generator/generatorConfig.xml 113 | true 114 | true 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/FrameworkQuartzManageApplication.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 8 | import org.springframework.cloud.openfeign.EnableFeignClients; 9 | import org.springframework.transaction.annotation.EnableTransactionManagement; 10 | 11 | @SpringBootApplication 12 | @EnableDiscoveryClient 13 | @EnableTransactionManagement 14 | @MapperScan(basePackages = "com.tuacy.microservice.framework.quartz.manage.mapper") 15 | @EnableFeignClients 16 | @EnableHystrix 17 | public class FrameworkQuartzManageApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(FrameworkQuartzManageApplication.class, args); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/controller/IQuartzManageController.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.controller; 2 | 3 | import com.tuacy.microservice.framework.common.controller.BaseController; 4 | import com.tuacy.microservice.framework.common.entity.response.ResponseDataEntity; 5 | import com.tuacy.microservice.framework.quartz.manage.api.IQuartzManageApi; 6 | import com.tuacy.microservice.framework.quartz.manage.exception.QuartzException; 7 | import com.tuacy.microservice.framework.quartz.manage.job.SendEmailJob; 8 | import com.tuacy.microservice.framework.quartz.manage.param.QuartzJobAddParam; 9 | import com.tuacy.microservice.framework.quartz.manage.param.QuartzJobDeleteParam; 10 | import com.tuacy.microservice.framework.quartz.manage.service.IQuartzActionService; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.web.bind.annotation.RequestBody; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | @RestController 16 | public class IQuartzManageController extends BaseController implements IQuartzManageApi { 17 | 18 | private static final String GROUP = "email_group"; 19 | 20 | private IQuartzActionService quartzActionService; 21 | 22 | @Autowired 23 | public void setQuartzActionService(IQuartzActionService quartzActionService) { 24 | this.quartzActionService = quartzActionService; 25 | } 26 | 27 | /** 28 | * 添加一个job任务 29 | */ 30 | @Override 31 | public ResponseDataEntity addQuartzJob(@RequestBody QuartzJobAddParam param) { 32 | ResponseDataEntity responseDataEntity = new ResponseDataEntity<>(); 33 | if (param.getKeyId() != null && !param.getKeyId().isEmpty()) { 34 | // 模拟每隔5s执行一次 35 | try { 36 | quartzActionService.addJob(SendEmailJob.class, param.getKeyId(), GROUP, "0/5 * * * * ? "); 37 | } catch (QuartzException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | return responseDataEntity; 42 | } 43 | 44 | /** 45 | * 添加一个job任务 46 | */ 47 | @Override 48 | public ResponseDataEntity deleteQuartzJob(@RequestBody QuartzJobDeleteParam param) { 49 | ResponseDataEntity responseDataEntity = new ResponseDataEntity<>(); 50 | if (param.getKeyId() != null && !param.getKeyId().isEmpty()) { 51 | // 模拟每隔5s执行一次 52 | try { 53 | quartzActionService.deleteJob(param.getKeyId(), GROUP); 54 | } catch (QuartzException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | return responseDataEntity; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/exception/QuartzException.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.exception; 2 | 3 | public class QuartzException extends Exception { 4 | 5 | public QuartzException() { 6 | } 7 | 8 | public QuartzException(String message) { 9 | super(message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/job/SendEmailJob.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.job; 2 | 3 | import com.tuacy.microservice.framework.quartz.manage.service.impl.QuartzActionServiceImpl; 4 | import org.quartz.JobDataMap; 5 | import org.quartz.JobExecutionContext; 6 | import org.springframework.scheduling.quartz.QuartzJobBean; 7 | 8 | /** 9 | * 定时发送邮件任务 -- 只是一个模拟任务,大家根据实际情况编写 10 | */ 11 | public class SendEmailJob extends QuartzJobBean { 12 | @Override 13 | protected void executeInternal(JobExecutionContext jobExecutionContext) { 14 | 15 | JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap(); 16 | if (jobDataMap != null && !jobDataMap.isEmpty()) { 17 | System.out.println("******************************"); 18 | System.out.println("******************************"); 19 | System.out.println("******************************"); 20 | System.out.println("job name = " + jobDataMap.get(QuartzActionServiceImpl.TASK_ID_KEY)); 21 | System.out.println("开始发送邮件了"); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.mapper; 2 | 3 | import com.tuacy.microservice.framework.quartz.manage.model.User; 4 | 5 | public interface UserMapper { 6 | /** 7 | * This method was generated by MyBatis Generator. 8 | * This method corresponds to the database table user 9 | * 10 | * @mbggenerated 11 | */ 12 | int deleteByPrimaryKey(Short pkid); 13 | 14 | /** 15 | * This method was generated by MyBatis Generator. 16 | * This method corresponds to the database table user 17 | * 18 | * @mbggenerated 19 | */ 20 | int insert(User record); 21 | 22 | /** 23 | * This method was generated by MyBatis Generator. 24 | * This method corresponds to the database table user 25 | * 26 | * @mbggenerated 27 | */ 28 | int insertSelective(User record); 29 | 30 | /** 31 | * This method was generated by MyBatis Generator. 32 | * This method corresponds to the database table user 33 | * 34 | * @mbggenerated 35 | */ 36 | User selectByPrimaryKey(Short pkid); 37 | 38 | /** 39 | * This method was generated by MyBatis Generator. 40 | * This method corresponds to the database table user 41 | * 42 | * @mbggenerated 43 | */ 44 | int updateByPrimaryKeySelective(User record); 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method corresponds to the database table user 49 | * 50 | * @mbggenerated 51 | */ 52 | int updateByPrimaryKey(User record); 53 | } -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/model/User.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | /** 7 | * This field was generated by MyBatis Generator. 8 | * This field corresponds to the database column user.pkid 9 | * 10 | * @mbggenerated 11 | */ 12 | private Short pkid; 13 | 14 | /** 15 | * This field was generated by MyBatis Generator. 16 | * This field corresponds to the database column user.name 17 | * 18 | * @mbggenerated 19 | */ 20 | private String name; 21 | 22 | /** 23 | * This field was generated by MyBatis Generator. 24 | * This field corresponds to the database column user.phone 25 | * 26 | * @mbggenerated 27 | */ 28 | private String phone; 29 | 30 | /** 31 | * This field was generated by MyBatis Generator. 32 | * This field corresponds to the database column user.password 33 | * 34 | * @mbggenerated 35 | */ 36 | private String password; 37 | 38 | /** 39 | * This field was generated by MyBatis Generator. 40 | * This field corresponds to the database table user 41 | * 42 | * @mbggenerated 43 | */ 44 | private static final long serialVersionUID = 1L; 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method returns the value of the database column user.pkid 49 | * 50 | * @return the value of user.pkid 51 | * 52 | * @mbggenerated 53 | */ 54 | public Short getPkid() { 55 | return pkid; 56 | } 57 | 58 | /** 59 | * This method was generated by MyBatis Generator. 60 | * This method sets the value of the database column user.pkid 61 | * 62 | * @param pkid the value for user.pkid 63 | * 64 | * @mbggenerated 65 | */ 66 | public void setPkid(Short pkid) { 67 | this.pkid = pkid; 68 | } 69 | 70 | /** 71 | * This method was generated by MyBatis Generator. 72 | * This method returns the value of the database column user.name 73 | * 74 | * @return the value of user.name 75 | * 76 | * @mbggenerated 77 | */ 78 | public String getName() { 79 | return name; 80 | } 81 | 82 | /** 83 | * This method was generated by MyBatis Generator. 84 | * This method sets the value of the database column user.name 85 | * 86 | * @param name the value for user.name 87 | * 88 | * @mbggenerated 89 | */ 90 | public void setName(String name) { 91 | this.name = name == null ? null : name.trim(); 92 | } 93 | 94 | /** 95 | * This method was generated by MyBatis Generator. 96 | * This method returns the value of the database column user.phone 97 | * 98 | * @return the value of user.phone 99 | * 100 | * @mbggenerated 101 | */ 102 | public String getPhone() { 103 | return phone; 104 | } 105 | 106 | /** 107 | * This method was generated by MyBatis Generator. 108 | * This method sets the value of the database column user.phone 109 | * 110 | * @param phone the value for user.phone 111 | * 112 | * @mbggenerated 113 | */ 114 | public void setPhone(String phone) { 115 | this.phone = phone == null ? null : phone.trim(); 116 | } 117 | 118 | /** 119 | * This method was generated by MyBatis Generator. 120 | * This method returns the value of the database column user.password 121 | * 122 | * @return the value of user.password 123 | * 124 | * @mbggenerated 125 | */ 126 | public String getPassword() { 127 | return password; 128 | } 129 | 130 | /** 131 | * This method was generated by MyBatis Generator. 132 | * This method sets the value of the database column user.password 133 | * 134 | * @param password the value for user.password 135 | * 136 | * @mbggenerated 137 | */ 138 | public void setPassword(String password) { 139 | this.password = password == null ? null : password.trim(); 140 | } 141 | 142 | /** 143 | * This method was generated by MyBatis Generator. 144 | * This method corresponds to the database table user 145 | * 146 | * @mbggenerated 147 | */ 148 | @Override 149 | public boolean equals(Object that) { 150 | if (this == that) { 151 | return true; 152 | } 153 | if (that == null) { 154 | return false; 155 | } 156 | if (getClass() != that.getClass()) { 157 | return false; 158 | } 159 | User other = (User) that; 160 | return (this.getPkid() == null ? other.getPkid() == null : this.getPkid().equals(other.getPkid())) 161 | && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) 162 | && (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone())) 163 | && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())); 164 | } 165 | 166 | /** 167 | * This method was generated by MyBatis Generator. 168 | * This method corresponds to the database table user 169 | * 170 | * @mbggenerated 171 | */ 172 | @Override 173 | public int hashCode() { 174 | final int prime = 31; 175 | int result = 1; 176 | result = prime * result + ((getPkid() == null) ? 0 : getPkid().hashCode()); 177 | result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); 178 | result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode()); 179 | result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode()); 180 | return result; 181 | } 182 | 183 | /** 184 | * This method was generated by MyBatis Generator. 185 | * This method corresponds to the database table user 186 | * 187 | * @mbggenerated 188 | */ 189 | @Override 190 | public String toString() { 191 | StringBuilder sb = new StringBuilder(); 192 | sb.append(getClass().getSimpleName()); 193 | sb.append(" ["); 194 | sb.append("Hash = ").append(hashCode()); 195 | sb.append(", pkid=").append(pkid); 196 | sb.append(", name=").append(name); 197 | sb.append(", phone=").append(phone); 198 | sb.append(", password=").append(password); 199 | sb.append(", serialVersionUID=").append(serialVersionUID); 200 | sb.append("]"); 201 | return sb.toString(); 202 | } 203 | } -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/service/IQuartzActionService.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.service; 2 | 3 | import com.tuacy.microservice.framework.quartz.manage.exception.QuartzException; 4 | import org.springframework.scheduling.quartz.QuartzJobBean; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | public interface IQuartzActionService { 10 | 11 | /** 12 | * 增加一个job 13 | * 14 | * @param jobClass 任务实现类 15 | * @param jobName 任务名称 16 | * @param jobGroupName 任务组名 17 | * @param jobTime 这是每隔多少秒为一次任务 18 | * @param jobTimes 运行的次数 (<0:表示不限次数) 19 | */ 20 | void addJob(Class jobClass, 21 | String jobName, 22 | String jobGroupName, 23 | int jobTime, 24 | int jobTimes) throws QuartzException; 25 | 26 | /** 27 | * 增加一个job 28 | * 29 | * @param jobClass 任务实现类 30 | * @param jobName 任务名称 31 | * @param jobGroupName 任务组名 32 | * @param jobTime 这是每隔多少秒为一次任务 33 | * @param jobTimes 运行的次数 (<0:表示不限次数) 34 | * @param start 添加任务之后直接设置pause,resume状态 35 | */ 36 | void addJob(Class jobClass, 37 | String jobName, 38 | String jobGroupName, 39 | int jobTime, 40 | int jobTimes, 41 | boolean start) throws QuartzException; 42 | 43 | /** 44 | * 增加一个job 45 | * 46 | * @param jobClass 任务实现类 47 | * @param jobName 任务名称 48 | * @param jobGroupName 任务组名 49 | * @param jobTime 时间表达式 (如:0/5 * * * * ? ) 50 | */ 51 | void addJob(Class jobClass, 52 | String jobName, 53 | String jobGroupName, 54 | String jobTime) throws QuartzException; 55 | 56 | /** 57 | * 增加一个job 58 | * 59 | * @param jobClass 任务实现类 60 | * @param jobName 任务名称 61 | * @param jobGroupName 任务组名 62 | * @param jobTime 时间表达式 (如:0/5 * * * * ? ) 63 | * @param start 是否启用 true: 添加任务之后,job run,false: 添加任务之后,job pause 64 | */ 65 | void addJob(Class jobClass, 66 | String jobName, 67 | String jobGroupName, 68 | String jobTime, 69 | boolean start) throws QuartzException; 70 | 71 | /** 72 | * 修改 一个job的 时间表达式 73 | * 74 | * @param jobName 任务名称 75 | * @param jobGroupName 任务组 76 | * @param jobTime 任务时间表达式 77 | */ 78 | void updateJob(String jobName, String jobGroupName, String jobTime) throws QuartzException; 79 | 80 | /** 81 | * 修改 一个job的 时间表达式 82 | * 83 | * @param jobName 任务名称 84 | * @param jobGroupName 任务组 85 | * @param jobTime 任务时间表达式 86 | * @param start 修改任务之后直接设置pause,resume状态 87 | */ 88 | void updateJob(String jobName, 89 | String jobGroupName, 90 | String jobTime, 91 | boolean start) throws QuartzException; 92 | 93 | /** 94 | * 删除任务一个job 95 | * 96 | * @param jobName 任务名称 97 | * @param jobGroupName 任务组名 98 | */ 99 | void deleteJob(String jobName, String jobGroupName) throws QuartzException; 100 | 101 | /** 102 | * 暂停一个job 103 | * 104 | * @param jobName 任务名称 105 | * @param jobGroupName 任务组名 106 | */ 107 | void pauseJob(String jobName, String jobGroupName) throws QuartzException; 108 | 109 | /** 110 | * 恢复一个job 111 | * 112 | * @param jobName 任务名称 113 | * @param jobGroupName 任务组名 114 | */ 115 | void resumeJob(String jobName, String jobGroupName) throws QuartzException; 116 | 117 | /** 118 | * 立即执行一个job 119 | * 120 | * @param jobName 任务名称 121 | * @param jobGroupName 任务组名 122 | */ 123 | void runAJobNow(String jobName, String jobGroupName) throws QuartzException; 124 | 125 | /** 126 | * 获取所有计划中的任务列表 127 | * 128 | * @return map 列表 129 | */ 130 | List> queryAllJob() throws QuartzException; 131 | 132 | /** 133 | * 获取所有正在运行的job 134 | * 135 | * @return list 列表 136 | */ 137 | 138 | List> queryRunJob() throws QuartzException; 139 | } 140 | -------------------------------------------------------------------------------- /quartz-manage/src/main/java/com/tuacy/microservice/framework/quartz/manage/service/impl/QuartzActionServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.quartz.manage.service.impl; 2 | 3 | import com.tuacy.microservice.framework.quartz.manage.exception.QuartzException; 4 | import com.tuacy.microservice.framework.quartz.manage.service.IQuartzActionService; 5 | import org.quartz.*; 6 | import org.quartz.impl.matchers.GroupMatcher; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.scheduling.quartz.QuartzJobBean; 9 | import org.springframework.scheduling.quartz.SchedulerFactoryBean; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.*; 13 | 14 | /** 15 | * @name: SafetyReportServiceImpl 16 | * @author: tuacy. 17 | * @date: 2019/3/6. 18 | * @version: 1.0 19 | * @Description: 告警 -- 定时报平安任务 20 | *

21 | *

22 | *

23 | * Quartz大致可分为三个主要的核心: 24 | *     1、调度器Scheduler:是一个计划调度器容器,容器里面可以盛放众多的JobDetail和Trigger,当容器启动后,里面的每个JobDetail都会根据Trigger按部就班自动去执行. 25 | *     2、任务Job:要执行的具体内容。JobDetail:具体的可执行的调度程序,包含了这个任务调度的方案和策略。 26 | *     3、触发器Trigger:调度参数的配置,什么时候去执行调度。 27 | * 原理: 28 | * 可以这么理解它的原理:调度器就相当于一个容器,装载着任务和触发器。任务和触发器又是绑定在一起的,然而一个任务可以对应多个触发器,但一个触发器却只能对应一个任务。 29 | * 当JobDetail和Trigger在scheduler容器上注册后,形成了装配好的任务作业(JobDetail和Trigger所组成的一对儿),就可以伴随容器启动而调度执行了。 30 | *

31 | *

32 | * corn表达式格式为七个域,如:秒 分 时 日 月 周 年 33 | *

34 | * 字段    允许值        允许的特殊字符 35 | *

36 | * 秒     0-59         , - * / 37 | * 分     0-59         , - * / 38 | * 小时    0-23         , - * / 39 | * 日期    1-31         , - * ? / L W C 40 | * 月份    1-12 或者 JAN-DEC  , - * / 41 | * 星期    1-7 或者 SUN-SAT  , - * ? / L C # 42 | * 年(可选) 留空, 1970-2099   , - * / 43 | *

44 | * 1、所有域均可用“,”,“-”,“*”,“/ 45 | * 【1】,    x,y表示x和y 46 | * 【2】-    x-y表示x到y 47 | * 【3】*    表示每TIME 48 | * 【4】/    x/y表示从x起,每隔y 49 | * 2、“日”域另有“?”,“L”,“W”,“C” 50 | * 【1】?    表示不指定“日”域的值。规则是指定“周”域,则“日”域必须为“?”。反之,指定“日”域,则“周”域必须为“?”。如0 0 12 ? * MON 或 0 0 12 1 * ? 51 | * 【2】L    2种写法。一,表示月末几天,如2L, 表示月末的2天。二,表示月末倒数第几天,如L-3,表示月末倒数第3天。 52 | * 【3】W    表示临近某日的工作日,如15W,表示最接近15号的工作日,可能是15号(刚好是工作日)、15号前(刚好周六)或15号后(刚好周日)。 53 | * 【4】C    表示和Calendar计划关联的值,如1C表示,1日或1日后包括Carlendar的第一天。 54 | * 【5】LW    L和W的组合,只能出现在"日"域中。表示某月最后一个工作日,不一定是周五(详细见结尾PS)。 55 | * 3、“周”域另有“?”,“L”,“#”,“C” 56 | * 【1】?    表示不指定“周”域。规则是指定“日”域值,则“周”域值必须为“?”。反之,指定“周”域值,则“日”域值必须为“?”。如0 0 12 1 * ? 或 0 0 12 ? * MON 57 | * 【2】L    表示某月的最后一个周几,如1L, 表示某月的最后一个周日(1表示周日),7L,表示某月的最后一个周六(7表示周六)。 58 | * 【3】#    只能出现在"周"域中,表示第几个周几,x#y,y表示第几个,x表示周的值,如6#2,表示第2个周五(6表示周五)。 59 | * 【4】C    表示和Calendar计划关联的值,如1C表示,周日或周日后包括Carlendar的第一天 60 | *

61 | *

62 | * 1、cron表达式在线生成工具http://www.pppet.net/ 63 | *

64 | * 2、cron表达式生成器http://www.pdtools.net/tools/becron.jsp 65 | *

66 | * quartz数据库相关建表路径:org.quartz.impl.jdbcjobstore 67 | */ 68 | @Service 69 | public class QuartzActionServiceImpl implements IQuartzActionService { 70 | 71 | 72 | public static final String TASK_ID_KEY = "task_id"; 73 | 74 | private SchedulerFactoryBean schedulerFactoryBean; 75 | 76 | @Autowired 77 | public void setSchedulerFactoryBean(SchedulerFactoryBean schedulerFactoryBean) { 78 | this.schedulerFactoryBean = schedulerFactoryBean; 79 | } 80 | 81 | /** 82 | * 增加一个job 83 | * 84 | * @param jobClass 任务实现类 85 | * @param jobName 任务名称 86 | * @param jobGroupName 任务组名 87 | * @param jobTime 这是每隔多少秒为一次任务 88 | * @param jobTimes 运行的次数 (<0:表示不限次数) 89 | */ 90 | @Override 91 | public void addJob(Class jobClass, 92 | String jobName, 93 | String jobGroupName, 94 | int jobTime, 95 | int jobTimes) throws QuartzException { 96 | this.addJob(jobClass, jobName, jobGroupName, jobTime, jobTimes, true); 97 | } 98 | 99 | /** 100 | * 增加一个job 101 | * 102 | * @param jobClass 任务实现类 103 | * @param jobName 任务名称 104 | * @param jobGroupName 任务组名 105 | * @param jobTime 这是每隔多少秒为一次任务 106 | * @param jobTimes 运行的次数 (<0:表示不限次数) 107 | * @param start 添加任务之后直接设置pause,resume状态 108 | */ 109 | @Override 110 | public void addJob(Class jobClass, 111 | String jobName, 112 | String jobGroupName, 113 | int jobTime, 114 | int jobTimes, 115 | boolean start) throws QuartzException { 116 | try { 117 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 118 | JobDetail jobDetail = JobBuilder 119 | .newJob(jobClass) 120 | .withIdentity(jobName, jobGroupName)// 任务名称和组构成任务key 121 | .build(); 122 | // job 信息 123 | JobDataMap map = jobDetail.getJobDataMap(); 124 | map.put(TASK_ID_KEY, jobName); 125 | // 使用simpleTrigger规则 126 | Trigger trigger; 127 | if (jobTimes < 0) { 128 | trigger = TriggerBuilder 129 | .newTrigger() 130 | .withIdentity(jobName, jobGroupName) 131 | .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1).withIntervalInSeconds(jobTime)) 132 | .startNow().build(); 133 | } else { 134 | trigger = TriggerBuilder 135 | .newTrigger() 136 | .withIdentity(jobName, jobGroupName) 137 | .withSchedule(SimpleScheduleBuilder 138 | .repeatSecondlyForever(1) 139 | .withIntervalInSeconds(jobTime) 140 | .withRepeatCount(jobTimes)) 141 | .startNow() 142 | .build(); 143 | } 144 | scheduler.scheduleJob(jobDetail, trigger); 145 | if (!start) { 146 | // 暂停 147 | JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); 148 | scheduler.pauseJob(jobKey); 149 | } 150 | } catch (SchedulerException e) { 151 | e.printStackTrace(); 152 | throw new QuartzException("添加告警定时任务失败!"); 153 | } 154 | } 155 | 156 | /** 157 | * 增加一个job 158 | * 159 | * @param jobClass 任务实现类 160 | * @param jobName 任务名称 161 | * @param jobGroupName 任务组名 162 | * @param jobTime 时间表达式 (如:0/5 * * * * ? ) 163 | */ 164 | @Override 165 | public void addJob(Class jobClass, 166 | String jobName, 167 | String jobGroupName, 168 | String jobTime) throws QuartzException { 169 | this.addJob(jobClass, jobName, jobGroupName, jobTime, true); 170 | } 171 | 172 | /** 173 | * 增加一个job 174 | * 175 | * @param jobClass 任务实现类 176 | * @param jobName 任务名称 177 | * @param jobGroupName 任务组名 178 | * @param jobTime 时间表达式 (如:0/5 * * * * ? ) 179 | * @param start 是否启用 true: 添加任务之后,job run,false: 添加任务之后,job pause 180 | */ 181 | @Override 182 | public void addJob(Class jobClass, 183 | String jobName, 184 | String jobGroupName, 185 | String jobTime, 186 | boolean start) throws QuartzException { 187 | try { 188 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 189 | JobDetail jobDetail = JobBuilder 190 | .newJob(jobClass) 191 | .withIdentity(jobName, jobGroupName) 192 | .build(); 193 | // 参数使用 194 | JobDataMap map = jobDetail.getJobDataMap(); 195 | map.put(TASK_ID_KEY, jobName); 196 | Trigger trigger = TriggerBuilder.newTrigger() 197 | .withIdentity(jobName, jobGroupName)// 触发器名,触发器组 198 | .withSchedule(CronScheduleBuilder.cronSchedule(jobTime)) // // 触发器时间设定 199 | .startNow() 200 | .build(); 201 | // 把作业和触发器注册到任务调度中 202 | scheduler.scheduleJob(jobDetail, trigger); 203 | if (!start) { 204 | // 暂停 205 | JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); 206 | scheduler.pauseJob(jobKey); 207 | } 208 | } catch (Exception e) { 209 | e.printStackTrace(); 210 | throw new QuartzException("告警定时任务运行失败"); 211 | } 212 | } 213 | 214 | /** 215 | * 修改 一个job的 时间表达式 216 | * 217 | * @param jobName 任务名称 218 | * @param jobGroupName 任务组 219 | * @param jobTime 任务时间表达式 220 | */ 221 | @Override 222 | public void updateJob(String jobName, String jobGroupName, String jobTime) throws QuartzException { 223 | this.updateJob(jobName, jobGroupName, jobTime, true); 224 | } 225 | 226 | /** 227 | * 修改 一个job的 时间表达式 228 | * 229 | * @param jobName 任务名称 230 | * @param jobGroupName 任务组 231 | * @param jobTime 任务时间表达式 232 | * @param start 修改任务之后直接设置pause,resume状态 233 | */ 234 | @Override 235 | public void updateJob(String jobName, 236 | String jobGroupName, 237 | String jobTime, 238 | boolean start) throws QuartzException { 239 | try { 240 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 241 | TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName); 242 | CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); 243 | trigger = trigger 244 | .getTriggerBuilder() 245 | .withIdentity(triggerKey) 246 | .withSchedule(CronScheduleBuilder.cronSchedule(jobTime)) 247 | .build(); 248 | // 重启触发器 249 | scheduler.rescheduleJob(triggerKey, trigger); 250 | if (!start) { 251 | // 暂停 252 | JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); 253 | scheduler.pauseJob(jobKey); 254 | } 255 | } catch (SchedulerException e) { 256 | e.printStackTrace(); 257 | throw new QuartzException("告警定时任务更新失败"); 258 | } 259 | } 260 | 261 | /** 262 | * 删除任务一个job 263 | * 264 | * @param jobName 任务名称 265 | * @param jobGroupName 任务组名 266 | */ 267 | @Override 268 | public void deleteJob(String jobName, String jobGroupName) throws QuartzException { 269 | try { 270 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 271 | scheduler.deleteJob(new JobKey(jobName, jobGroupName)); 272 | } catch (Exception e) { 273 | e.printStackTrace(); 274 | throw new QuartzException("告警定时任务删除失败"); 275 | } 276 | } 277 | 278 | /** 279 | * 暂停一个job 280 | * 281 | * @param jobName 任务名称 282 | * @param jobGroupName 任务组名 283 | */ 284 | @Override 285 | public void pauseJob(String jobName, String jobGroupName) throws QuartzException { 286 | try { 287 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 288 | JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); 289 | scheduler.pauseJob(jobKey); 290 | } catch (SchedulerException e) { 291 | e.printStackTrace(); 292 | throw new QuartzException("告警定时任务暂停失败"); 293 | } 294 | } 295 | 296 | /** 297 | * 恢复一个job 298 | * 299 | * @param jobName 任务名称 300 | * @param jobGroupName 任务组名 301 | */ 302 | @Override 303 | public void resumeJob(String jobName, String jobGroupName) throws QuartzException { 304 | try { 305 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 306 | JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); 307 | scheduler.resumeJob(jobKey); 308 | } catch (SchedulerException e) { 309 | e.printStackTrace(); 310 | throw new QuartzException("告警定时任务恢复失败"); 311 | } 312 | } 313 | 314 | /** 315 | * 立即执行一个job 316 | * 317 | * @param jobName 任务名称 318 | * @param jobGroupName 任务组名 319 | */ 320 | @Override 321 | public void runAJobNow(String jobName, String jobGroupName) throws QuartzException { 322 | try { 323 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 324 | JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); 325 | scheduler.triggerJob(jobKey); 326 | } catch (SchedulerException e) { 327 | e.printStackTrace(); 328 | throw new QuartzException("告警定时任务运行失败"); 329 | } 330 | } 331 | 332 | /** 333 | * 获取所有计划中的任务列表 334 | * 335 | * @return map 列表 336 | */ 337 | @Override 338 | public List> queryAllJob() throws QuartzException { 339 | List> jobList = new ArrayList<>(); 340 | try { 341 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 342 | GroupMatcher matcher = GroupMatcher.anyJobGroup(); 343 | Set jobKeys = scheduler.getJobKeys(matcher); 344 | for (JobKey jobKey : jobKeys) { 345 | List triggers = scheduler.getTriggersOfJob(jobKey); 346 | for (Trigger trigger : triggers) { 347 | Map map = new HashMap<>(); 348 | map.put("jobName", jobKey.getName()); 349 | map.put("jobGroupName", jobKey.getGroup()); 350 | map.put("description", "触发器:" + trigger.getKey()); 351 | Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); 352 | map.put("jobStatus", triggerState.name()); 353 | if (trigger instanceof CronTrigger) { 354 | CronTrigger cronTrigger = (CronTrigger) trigger; 355 | String cronExpression = cronTrigger.getCronExpression(); 356 | map.put("jobTime", cronExpression); 357 | } 358 | jobList.add(map); 359 | } 360 | } 361 | } catch (SchedulerException e) { 362 | e.printStackTrace(); 363 | throw new QuartzException("查询告警定时任务列表失败"); 364 | } 365 | return jobList; 366 | } 367 | 368 | /** 369 | * 获取所有正在运行的job 370 | * 371 | * @return list 列表 372 | */ 373 | @Override 374 | public List> queryRunJob() throws QuartzException { 375 | List> jobList; 376 | try { 377 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 378 | List executingJobs = scheduler.getCurrentlyExecutingJobs(); 379 | jobList = new ArrayList<>(executingJobs.size()); 380 | for (JobExecutionContext executingJob : executingJobs) { 381 | Map map = new HashMap<>(); 382 | JobDetail jobDetail = executingJob.getJobDetail(); 383 | JobKey jobKey = jobDetail.getKey(); 384 | Trigger trigger = executingJob.getTrigger(); 385 | map.put("jobName", jobKey.getName()); 386 | map.put("jobGroupName", jobKey.getGroup()); 387 | map.put("description", "触发器:" + trigger.getKey()); 388 | Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); 389 | map.put("jobStatus", triggerState.name()); 390 | if (trigger instanceof CronTrigger) { 391 | CronTrigger cronTrigger = (CronTrigger) trigger; 392 | String cronExpression = cronTrigger.getCronExpression(); 393 | map.put("jobTime", cronExpression); 394 | } 395 | jobList.add(map); 396 | } 397 | } catch (SchedulerException e) { 398 | e.printStackTrace(); 399 | throw new QuartzException("查询告警定时任务运行任务列表失败"); 400 | } 401 | return jobList; 402 | } 403 | } 404 | -------------------------------------------------------------------------------- /quartz-manage/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #eureka: 2 | # client: 3 | # serviceUrl: 4 | # defaultZone: http://localhost:8761/eureka/ 5 | # 6 | # instance: 7 | # instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} 8 | # prefer-ip-address: true 9 | # leaseRenewalIntervalInSeconds: 5 10 | # leaseExpirationDurationInSeconds: 5 11 | 12 | mybatis: 13 | typeAliasesPackage: com.tuacy.microservice.framework.user.manage.model 14 | mapperLocations: classpath:mapper/*.xml 15 | configuration: 16 | log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 17 | 18 | # 分页配置 19 | pagehelper: 20 | helper-dialect: mysql 21 | reasonable: true 22 | support-methods-arguments: true 23 | params: count=countSql 24 | 25 | #hystrix: 26 | # threadpool: 27 | # default: 28 | # coreSize: 200 ##并发执行的最大线程数,默认10 29 | # maxQueueSize: 200 ##BlockingQueue的最大队列数 30 | # queueSizeRejectionThreshold: 200 ##即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝 31 | # command: 32 | # default: 33 | # execution: 34 | # timeout: 35 | # enabled: false 36 | # circuitBreaker: 37 | # errorThresholdPercentage: 50 # 50% 38 | # sleepWindowInMilliseconds: 100000 # 100s 39 | # customCommandKey: 40 | # fallback: 41 | # enabled: false 42 | # circuitBreaker: 43 | # errorThresholdPercentage: 75 # 75% 44 | # sleepWindowInMilliseconds: 15000 # 15s 45 | -------------------------------------------------------------------------------- /quartz-manage/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2223 3 | 4 | spring: 5 | application: 6 | name: microservice-framework-quartz-manage 7 | quartz: 8 | #相关属性配置 9 | properties: 10 | org: 11 | quartz: 12 | scheduler: 13 | instanceName: clusteredScheduler 14 | instanceId: AUTO 15 | jobStore: 16 | class: org.quartz.impl.jdbcjobstore.JobStoreTX 17 | driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate 18 | tablePrefix: QRTZ_ 19 | isClustered: true 20 | clusterCheckinInterval: 10000 21 | useProperties: false 22 | threadPool: 23 | class: org.quartz.simpl.SimpleThreadPool 24 | threadCount: 10 25 | threadPriority: 5 26 | threadsInheritContextClassLoaderOfInitializingThread: true 27 | #数据库方式 28 | job-store-type: jdbc 29 | datasource: 30 | url: jdbc:mysql://localhost:3306/tuacydev?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false 31 | username: root 32 | password: 123456 33 | driver-class-name: com.mysql.jdbc.Driver 34 | # 使用druid数据源 35 | type: com.alibaba.druid.pool.DruidDataSource 36 | cloud: 37 | config: # spring could config client 38 | discovery: 39 | enabled: true #开启配置服务发现 40 | service-id: microservice-center-config-server #配置服务实例名称 41 | label: master #配置文件所在分支 42 | -------------------------------------------------------------------------------- /quartz-manage/src/main/resources/generator/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 43 | 44 | 45 | 46 | 47 | 48 | 52 |
53 |
54 |
55 | -------------------------------------------------------------------------------- /quartz-manage/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 19 | pkid, name, phone, password 20 | 21 | 31 | 32 | 36 | delete from user 37 | where pkid = #{pkid,jdbcType=SMALLINT} 38 | 39 | 40 | 44 | insert into user (pkid, name, phone, 45 | password) 46 | values (#{pkid,jdbcType=SMALLINT}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, 47 | #{password,jdbcType=VARCHAR}) 48 | 49 | 50 | 54 | insert into user 55 | 56 | 57 | pkid, 58 | 59 | 60 | name, 61 | 62 | 63 | phone, 64 | 65 | 66 | password, 67 | 68 | 69 | 70 | 71 | #{pkid,jdbcType=SMALLINT}, 72 | 73 | 74 | #{name,jdbcType=VARCHAR}, 75 | 76 | 77 | #{phone,jdbcType=VARCHAR}, 78 | 79 | 80 | #{password,jdbcType=VARCHAR}, 81 | 82 | 83 | 84 | 85 | 89 | update user 90 | 91 | 92 | name = #{name,jdbcType=VARCHAR}, 93 | 94 | 95 | phone = #{phone,jdbcType=VARCHAR}, 96 | 97 | 98 | password = #{password,jdbcType=VARCHAR}, 99 | 100 | 101 | where pkid = #{pkid,jdbcType=SMALLINT} 102 | 103 | 104 | 108 | update user 109 | set name = #{name,jdbcType=VARCHAR}, 110 | phone = #{phone,jdbcType=VARCHAR}, 111 | password = #{password,jdbcType=VARCHAR} 112 | where pkid = #{pkid,jdbcType=SMALLINT} 113 | 114 | -------------------------------------------------------------------------------- /url-decoration/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.tuacy.starter 8 | url-decoration 9 | 1.0.0 10 | jar 11 | 12 | 13 | 14 | UTF-8 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-autoconfigure 23 | 2.0.3.RELEASE 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /url-decoration/src/main/java/com/tuacy/starter/url/decoration/UrlDecorationAutoConfig.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.starter.url.decoration; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 7 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | /** 12 | * 实现自动化配置 13 | */ 14 | @Configuration//开启配置 15 | @EnableConfigurationProperties(UrlDecorationServiceProperties.class) //开启使用映射实体对象 16 | @ConditionalOnClass(UrlDecorationService.class) //存在UrlDecorationService时初始化该配置类 17 | @ConditionalOnProperty //存在对应配置信息时初始化该配置类 18 | ( 19 | prefix = "url",//存在配置前缀hello 20 | value = "enabled",//开启 21 | matchIfMissing = true//缺失检查 22 | ) 23 | public class UrlDecorationAutoConfig { 24 | 25 | 26 | private UrlDecorationServiceProperties properties; 27 | 28 | @Autowired 29 | public void setProperties(UrlDecorationServiceProperties properties) { 30 | this.properties = properties; 31 | } 32 | 33 | /** 34 | * 根据条件判断不存在UrlDecorationService时初始化新bean到SpringIoc 35 | */ 36 | @Bean //创建UrlDecorationService实体bean 37 | @ConditionalOnMissingBean(UrlDecorationService.class) //缺失UrlDecorationService实体bean时,初始化UrlDecorationService并添加到SpringIoc 38 | UrlDecorationService urlDecorationService() { 39 | return new UrlDecorationService(properties); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /url-decoration/src/main/java/com/tuacy/starter/url/decoration/UrlDecorationService.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.starter.url.decoration; 2 | 3 | /** 4 | * 自定义业务实现 5 | */ 6 | public class UrlDecorationService { 7 | 8 | /** 9 | * http or https 10 | */ 11 | private String protocol; 12 | /** 13 | * 地址 127.0.0.1 14 | */ 15 | private String host; 16 | /** 17 | * 端口 8000 18 | */ 19 | private int port; 20 | 21 | public UrlDecorationService(UrlDecorationServiceProperties properties) { 22 | this.protocol = properties.getProtocol(); 23 | this.host = properties.getHost(); 24 | this.port = properties.getPort(); 25 | } 26 | 27 | public String url(String paramter) { 28 | return protocol + "//" + host + ":" + port + "/" + paramter; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /url-decoration/src/main/java/com/tuacy/starter/url/decoration/UrlDecorationServiceProperties.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.starter.url.decoration; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * 配置文件实体映射体 7 | */ 8 | @ConfigurationProperties("url") 9 | public class UrlDecorationServiceProperties { 10 | 11 | /** 12 | * http or https 13 | */ 14 | private String protocol = "http"; 15 | /** 16 | * 地址 127.0.0.1 17 | */ 18 | private String host = "127.0.0.1"; 19 | /** 20 | * 端口 8000 21 | */ 22 | private int port = 8080; 23 | 24 | public String getProtocol() { 25 | return protocol; 26 | } 27 | 28 | public void setProtocol(String protocol) { 29 | this.protocol = protocol; 30 | } 31 | 32 | public String getHost() { 33 | return host; 34 | } 35 | 36 | public void setHost(String host) { 37 | this.host = host; 38 | } 39 | 40 | public int getPort() { 41 | return port; 42 | } 43 | 44 | public void setPort(int port) { 45 | this.port = port; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /url-decoration/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # 配置自定义Starter的自动化配置 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tuacy.starter.url.decoration.UrlDecorationAutoConfig 3 | -------------------------------------------------------------------------------- /user-manage-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | microservice-framework 7 | tuacy.microservice.framweork 8 | PACKT-SNAPSHOT 9 | 10 | 4.0.0 11 | jar 12 | 13 | user-manage-api 14 | 15 | 16 | 17 | tuacy.microservice.framweork 18 | framework-common 19 | ${parent.version} 20 | 21 | 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-maven-plugin 28 | 29 | 30 | none 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/api/IStarterCustomControllerApi.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.api.api; 2 | 3 | import com.tuacy.microservice.framework.common.entity.response.ResponseDataEntity; 4 | import com.tuacy.microservice.framework.user.manage.api.request.UrlDecorationParam; 5 | import org.springframework.web.bind.annotation.RequestBody; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | @RequestMapping("/starter/") 10 | public interface IStarterCustomControllerApi { 11 | 12 | @RequestMapping(value = "urlDecoration", method = RequestMethod.POST) 13 | ResponseDataEntity urlDecoration(@RequestBody UrlDecorationParam param); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/api/IUserControllerApi.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.api.api; 2 | 3 | import com.tuacy.microservice.framework.common.entity.response.ResponseDataEntity; 4 | import com.tuacy.microservice.framework.user.manage.api.request.UserParam; 5 | import com.tuacy.microservice.framework.user.manage.api.response.UserInfoEntity; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | 10 | @RequestMapping("/user/") 11 | public interface IUserControllerApi { 12 | 13 | @RequestMapping(value = "get", method = RequestMethod.POST) 14 | ResponseDataEntity getUser(@RequestBody UserParam param); 15 | } 16 | -------------------------------------------------------------------------------- /user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/request/UrlDecorationParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.api.request; 2 | 3 | public class UrlDecorationParam { 4 | 5 | private String param; 6 | 7 | public String getParam() { 8 | return param; 9 | } 10 | 11 | public void setParam(String param) { 12 | this.param = param; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/request/UserParam.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.api.request; 2 | 3 | public class UserParam { 4 | 5 | private String userName; 6 | 7 | public String getUserName() { 8 | return userName; 9 | } 10 | 11 | public void setUserName(String userName) { 12 | this.userName = userName; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "UserParam{" + 18 | "userName='" + userName + '\'' + 19 | '}'; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/request/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuacy/microservice-framework/57a151ef4e032daa136250b946b31cc130435dbb/user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/request/readme.md -------------------------------------------------------------------------------- /user-manage-api/src/main/java/com/tuacy/microservice/framework/user/manage/api/response/UserInfoEntity.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.api.response; 2 | 3 | public class UserInfoEntity { 4 | 5 | private String name; 6 | private String phone; 7 | private String password; 8 | 9 | public String getName() { 10 | return name; 11 | } 12 | 13 | public void setName(String name) { 14 | this.name = name; 15 | } 16 | 17 | public String getPhone() { 18 | return phone; 19 | } 20 | 21 | public void setPhone(String phone) { 22 | this.phone = phone; 23 | } 24 | 25 | public String getPassword() { 26 | return password; 27 | } 28 | 29 | public void setPassword(String password) { 30 | this.password = password; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /user-manage/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | user-manage 8 | PACKT-SNAPSHOT 9 | jar 10 | 11 | user-manage 12 | user manage for Spring Cloud 13 | 14 | 15 | microservice-framework 16 | tuacy.microservice.framweork 17 | PACKT-SNAPSHOT 18 | 19 | 4.0.0 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-hateoas 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-actuator 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-sleuth 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-jdbc 43 | 44 | 45 | 46 | 47 | mysql 48 | mysql-connector-java 49 | 5.1.39 50 | 51 | 52 | 53 | 54 | com.alibaba 55 | druid 56 | 1.0.18 57 | 58 | 59 | 60 | 61 | org.mybatis.spring.boot 62 | mybatis-spring-boot-starter 63 | 1.3.2 64 | 65 | 66 | 67 | 68 | com.github.pagehelper 69 | pagehelper-spring-boot-starter 70 | 71 | 1.2.3 72 | 73 | 74 | 75 | tuacy.microservice.framweork 76 | framework-common 77 | ${parent.version} 78 | 79 | 80 | 81 | tuacy.microservice.framweork 82 | user-manage-api 83 | ${parent.version} 84 | 85 | 86 | 87 | com.tuacy.starter 88 | url-decoration 89 | 1.0.0 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | org.springframework.boot 98 | spring-boot-maven-plugin 99 | 100 | 101 | none 102 | 103 | 104 | 105 | 106 | 107 | org.mybatis.generator 108 | mybatis-generator-maven-plugin 109 | 1.3.2 110 | 111 | 112 | mysql 113 | mysql-connector-java 114 | 5.1.35 115 | 116 | 117 | 118 | ${basedir}/src/main/resources/generator/generatorConfig.xml 119 | true 120 | true 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/FrameworkUserManageApplication.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 8 | import org.springframework.cloud.openfeign.EnableFeignClients; 9 | import org.springframework.transaction.annotation.EnableTransactionManagement; 10 | 11 | @SpringBootApplication 12 | @EnableDiscoveryClient 13 | @EnableTransactionManagement 14 | @MapperScan(basePackages = "com.tuacy.microservice.framework.user.manage.mapper") 15 | @EnableFeignClients 16 | @EnableHystrix 17 | public class FrameworkUserManageApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(FrameworkUserManageApplication.class, args); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/annotation/ClassAnnotation.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.annotation; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * 添加在类上的注解 7 | */ 8 | @Retention(RetentionPolicy.RUNTIME) 9 | @Target(ElementType.TYPE) 10 | @Documented 11 | public @interface ClassAnnotation { 12 | public String value() default ""; 13 | } 14 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/annotation/LoggingAnnotation.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.annotation; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * 添加在方法上的注解 7 | * 编写自定义注解。实现对方法所实现的功能进行描述 8 | */ 9 | @Retention(RetentionPolicy.RUNTIME) 10 | @Target(ElementType.METHOD) 11 | @Documented 12 | public @interface LoggingAnnotation { 13 | } 14 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/aspect/LoggingAspect.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.aspect; 2 | 3 | import com.tuacy.microservice.framework.user.manage.api.request.UserParam; 4 | import com.tuacy.microservice.framework.user.manage.controller.UserController; 5 | import org.aspectj.lang.JoinPoint; 6 | import org.aspectj.lang.ProceedingJoinPoint; 7 | import org.aspectj.lang.annotation.*; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * 日志AOP 切面类 12 | */ 13 | 14 | @Aspect 15 | @Component("logAspect") 16 | public class LoggingAspect { 17 | 18 | /** 19 | * 匹配com.tuacy.microservice.framework.user.manage.controller包下面直接子类所有方法。 20 | */ 21 | @Pointcut("execution(* com.tuacy.microservice.framework.user.manage.controller.*.*(..))") 22 | public void operateLog() { 23 | } 24 | 25 | /** 26 | * 匹配com.tuacy.microservice.framework.user.manage.controller包下直接子类所有方法。 27 | */ 28 | // @Pointcut("within(com.tuacy.microservice.framework.user.manage.controller.*)") 29 | // public void operateLog() { 30 | // } 31 | 32 | /** 33 | * 匹配所有添加了LogginAnnotation注解的方法 34 | */ 35 | // @Pointcut("@within(com.tuacy.microservice.framework.user.manage.annotation.LoggingAnnotation)") 36 | // public void operateLog() { 37 | // } 38 | 39 | /** 40 | * 匹配所有添加了LogginAnnotation注解的方法(TODO: 测试发现只有后置通知) 41 | */ 42 | // @Pointcut("within(@com.tuacy.microservice.framework.user.manage.annotation.LoggingAnnotation *)") 43 | // public void operateLog() { 44 | // } 45 | 46 | /** 47 | * 匹配所有实现了IUserControllerApi接口类的所有方法 48 | */ 49 | // @Pointcut("this(com.tuacy.microservice.framework.user.manage.api.api.IUserControllerApi)") 50 | // public void operateLog() { 51 | // } 52 | 53 | /** 54 | * 匹配UserController类里面所有的方法 55 | */ 56 | // @Pointcut("this(com.tuacy.microservice.framework.user.manage.controller.UserController)") 57 | // public void operateLog() { 58 | // } 59 | 60 | /** 61 | * 匹配所有实现了IUserControllerApi接口类的所有方法 62 | */ 63 | // @Pointcut("target(com.tuacy.microservice.framework.user.manage.api.api.IUserControllerApi)") 64 | // public void operateLog() { 65 | // } 66 | 67 | /** 68 | * 匹配所有类添加了ClassAnnotation注解的类的所有方法 69 | */ 70 | // @Pointcut("@within(com.tuacy.microservice.framework.user.manage.annotation.ClassAnnotation)") 71 | // public void operateLog() { 72 | // } 73 | 74 | /** 75 | * 匹配所有类添加了ClassAnnotation注解的类的所有方法 -- TODO: 进过测试直接就编译不过 76 | */ 77 | // @Pointcut("@target(com.tuacy.microservice.framework.user.manage.annotation.ClassAnnotation)") 78 | // public void operateLog() { 79 | // } 80 | 81 | /** 82 | * 匹配所有添加了LoggingAnnotation注解的方法 83 | */ 84 | // @Pointcut("@annotation(com.tuacy.microservice.framework.user.manage.annotation.LoggingAnnotation)") 85 | // public void operateLog() { 86 | // } 87 | 88 | /** 89 | * 前置通知:目标方法执行之前执行以下方法体的内容 90 | */ 91 | @Before(value = "operateLog() && args(param)") 92 | public void beforeMethod(JoinPoint jp, UserParam param) { 93 | System.out.println("aaaaaaaaaa"); 94 | System.out.println(param.toString()); 95 | String methodName = jp.getSignature().getName(); 96 | System.out.println("【前置通知】the method 【" + methodName + "】"); 97 | } 98 | 99 | /** 100 | * 返回通知:目标方法正常执行完毕时执行以下代码 101 | */ 102 | @AfterReturning(value = "operateLog()", returning = "result") 103 | public void afterReturningMethod(JoinPoint jp, Object result) { 104 | String methodName = jp.getSignature().getName(); 105 | System.out.println("【返回通知】the method 【" + methodName + "】 ends with 【" + result + "】"); 106 | } 107 | 108 | /** 109 | * 后置通知:目标方法执行之后执行以下方法体的内容,不管是否发生异常。 110 | */ 111 | @After(value = "operateLog()") 112 | public void afterMethod(JoinPoint jp) { 113 | System.out.println("【后置通知】this is a afterMethod advice..."); 114 | } 115 | 116 | /** 117 | * 异常通知:目标方法发生异常的时候执行以下代码 118 | */ 119 | @AfterThrowing(value = "operateLog()", throwing = "e") 120 | public void afterThorwingMethod(JoinPoint jp, NullPointerException e) { 121 | String methodName = jp.getSignature().getName(); 122 | System.out.println("【异常通知】the method 【" + methodName + "】 occurs exception: " + e); 123 | } 124 | 125 | /** 126 | * 环绕通知:目标方法执行前后分别执行一些代码,发生异常的时候执行另外一些代码 127 | */ 128 | // @Around(value = "operateLog()") 129 | // public Object aroundMethod(ProceedingJoinPoint jp) { 130 | // String methodName = jp.getSignature().getName(); 131 | // Object result = null; 132 | // try { 133 | // System.out.println("【环绕通知中的--->前置通知】:the method 【" + methodName + "】"); 134 | // //执行目标方法 135 | // result = jp.proceed(); 136 | // System.out.println("【环绕通知中的--->返回通知】:the method 【" + methodName + "】 ends with " + result); 137 | // } catch (Throwable e) { 138 | // System.out.println("【环绕通知中的--->异常通知】:the method 【" + methodName + "】 occurs exception " + e); 139 | // } 140 | // 141 | // System.out.println("【环绕通知中的--->后置通知】:-----------------end.----------------------"); 142 | // return result; 143 | // } 144 | 145 | 146 | } 147 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/controller/StarterCustomController.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.controller; 2 | 3 | import com.tuacy.microservice.framework.common.constant.ResponseResultType; 4 | import com.tuacy.microservice.framework.common.controller.BaseController; 5 | import com.tuacy.microservice.framework.common.entity.response.ResponseDataEntity; 6 | import com.tuacy.microservice.framework.user.manage.api.api.IStarterCustomControllerApi; 7 | import com.tuacy.microservice.framework.user.manage.api.request.UrlDecorationParam; 8 | import com.tuacy.microservice.framework.user.manage.service.IStarterCustomService; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.web.bind.annotation.RequestBody; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | @RestController 14 | public class StarterCustomController extends BaseController implements IStarterCustomControllerApi { 15 | 16 | private IStarterCustomService starterCustomService; 17 | 18 | @Autowired 19 | public void setStarterCustomService(IStarterCustomService starterCustomService) { 20 | this.starterCustomService = starterCustomService; 21 | } 22 | 23 | @Override 24 | public ResponseDataEntity urlDecoration(@RequestBody UrlDecorationParam param) { 25 | ResponseDataEntity responseDataEntity = new ResponseDataEntity<>(); 26 | try { 27 | responseDataEntity.setMsg(ResponseResultType.SUCCESS.getDesc()); 28 | responseDataEntity.setStatus(ResponseResultType.SUCCESS.getValue()); 29 | responseDataEntity.setData(starterCustomService.urlDecoration(param.getParam())); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | 34 | return responseDataEntity; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.controller; 2 | 3 | import com.tuacy.microservice.framework.common.constant.ResponseResultType; 4 | import com.tuacy.microservice.framework.common.controller.BaseController; 5 | import com.tuacy.microservice.framework.common.entity.response.ResponseDataEntity; 6 | import com.tuacy.microservice.framework.user.manage.annotation.ClassAnnotation; 7 | import com.tuacy.microservice.framework.user.manage.annotation.LoggingAnnotation; 8 | import com.tuacy.microservice.framework.user.manage.api.api.IUserControllerApi; 9 | import com.tuacy.microservice.framework.user.manage.api.request.UserParam; 10 | import com.tuacy.microservice.framework.user.manage.api.response.UserInfoEntity; 11 | import com.tuacy.microservice.framework.user.manage.service.IUserService; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | 18 | @ClassAnnotation() 19 | @RestController 20 | @RequestMapping("/user/") 21 | public class UserController extends BaseController implements IUserControllerApi { 22 | 23 | private IUserService userService; 24 | 25 | @Autowired 26 | public void setUserService(IUserService userService) { 27 | this.userService = userService; 28 | } 29 | 30 | @LoggingAnnotation() 31 | public ResponseDataEntity getUser(@RequestBody UserParam param) { 32 | ResponseDataEntity responseDataEntity = new ResponseDataEntity<>(); 33 | try { 34 | responseDataEntity.setMsg(ResponseResultType.SUCCESS.getDesc()); 35 | responseDataEntity.setStatus(ResponseResultType.SUCCESS.getValue()); 36 | responseDataEntity.setData(userService.getUserInfo()); 37 | } catch (Exception e) { 38 | e.printStackTrace(); 39 | } 40 | 41 | return responseDataEntity; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/entity/readme.md: -------------------------------------------------------------------------------- 1 | # 实体类 -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/mapper/UserManageMapper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.mapper; 2 | 3 | import com.tuacy.microservice.framework.user.manage.api.response.UserInfoEntity; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | import java.util.List; 7 | 8 | public interface UserManageMapper { 9 | 10 | List selectName(); 11 | 12 | long saveUserInfo(@Param("userInfo") UserInfoEntity userInfo); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.mapper; 2 | 3 | import com.tuacy.microservice.framework.user.manage.model.User; 4 | 5 | public interface UserMapper { 6 | /** 7 | * This method was generated by MyBatis Generator. 8 | * This method corresponds to the database table user 9 | * 10 | * @mbggenerated 11 | */ 12 | int deleteByPrimaryKey(Short pkid); 13 | 14 | /** 15 | * This method was generated by MyBatis Generator. 16 | * This method corresponds to the database table user 17 | * 18 | * @mbggenerated 19 | */ 20 | int insert(User record); 21 | 22 | /** 23 | * This method was generated by MyBatis Generator. 24 | * This method corresponds to the database table user 25 | * 26 | * @mbggenerated 27 | */ 28 | int insertSelective(User record); 29 | 30 | /** 31 | * This method was generated by MyBatis Generator. 32 | * This method corresponds to the database table user 33 | * 34 | * @mbggenerated 35 | */ 36 | User selectByPrimaryKey(Short pkid); 37 | 38 | /** 39 | * This method was generated by MyBatis Generator. 40 | * This method corresponds to the database table user 41 | * 42 | * @mbggenerated 43 | */ 44 | int updateByPrimaryKeySelective(User record); 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method corresponds to the database table user 49 | * 50 | * @mbggenerated 51 | */ 52 | int updateByPrimaryKey(User record); 53 | } -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/model/User.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | /** 7 | * This field was generated by MyBatis Generator. 8 | * This field corresponds to the database column user.pkid 9 | * 10 | * @mbggenerated 11 | */ 12 | private Short pkid; 13 | 14 | /** 15 | * This field was generated by MyBatis Generator. 16 | * This field corresponds to the database column user.name 17 | * 18 | * @mbggenerated 19 | */ 20 | private String name; 21 | 22 | /** 23 | * This field was generated by MyBatis Generator. 24 | * This field corresponds to the database column user.phone 25 | * 26 | * @mbggenerated 27 | */ 28 | private String phone; 29 | 30 | /** 31 | * This field was generated by MyBatis Generator. 32 | * This field corresponds to the database column user.password 33 | * 34 | * @mbggenerated 35 | */ 36 | private String password; 37 | 38 | /** 39 | * This field was generated by MyBatis Generator. 40 | * This field corresponds to the database table user 41 | * 42 | * @mbggenerated 43 | */ 44 | private static final long serialVersionUID = 1L; 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method returns the value of the database column user.pkid 49 | * 50 | * @return the value of user.pkid 51 | * 52 | * @mbggenerated 53 | */ 54 | public Short getPkid() { 55 | return pkid; 56 | } 57 | 58 | /** 59 | * This method was generated by MyBatis Generator. 60 | * This method sets the value of the database column user.pkid 61 | * 62 | * @param pkid the value for user.pkid 63 | * 64 | * @mbggenerated 65 | */ 66 | public void setPkid(Short pkid) { 67 | this.pkid = pkid; 68 | } 69 | 70 | /** 71 | * This method was generated by MyBatis Generator. 72 | * This method returns the value of the database column user.name 73 | * 74 | * @return the value of user.name 75 | * 76 | * @mbggenerated 77 | */ 78 | public String getName() { 79 | return name; 80 | } 81 | 82 | /** 83 | * This method was generated by MyBatis Generator. 84 | * This method sets the value of the database column user.name 85 | * 86 | * @param name the value for user.name 87 | * 88 | * @mbggenerated 89 | */ 90 | public void setName(String name) { 91 | this.name = name == null ? null : name.trim(); 92 | } 93 | 94 | /** 95 | * This method was generated by MyBatis Generator. 96 | * This method returns the value of the database column user.phone 97 | * 98 | * @return the value of user.phone 99 | * 100 | * @mbggenerated 101 | */ 102 | public String getPhone() { 103 | return phone; 104 | } 105 | 106 | /** 107 | * This method was generated by MyBatis Generator. 108 | * This method sets the value of the database column user.phone 109 | * 110 | * @param phone the value for user.phone 111 | * 112 | * @mbggenerated 113 | */ 114 | public void setPhone(String phone) { 115 | this.phone = phone == null ? null : phone.trim(); 116 | } 117 | 118 | /** 119 | * This method was generated by MyBatis Generator. 120 | * This method returns the value of the database column user.password 121 | * 122 | * @return the value of user.password 123 | * 124 | * @mbggenerated 125 | */ 126 | public String getPassword() { 127 | return password; 128 | } 129 | 130 | /** 131 | * This method was generated by MyBatis Generator. 132 | * This method sets the value of the database column user.password 133 | * 134 | * @param password the value for user.password 135 | * 136 | * @mbggenerated 137 | */ 138 | public void setPassword(String password) { 139 | this.password = password == null ? null : password.trim(); 140 | } 141 | 142 | /** 143 | * This method was generated by MyBatis Generator. 144 | * This method corresponds to the database table user 145 | * 146 | * @mbggenerated 147 | */ 148 | @Override 149 | public boolean equals(Object that) { 150 | if (this == that) { 151 | return true; 152 | } 153 | if (that == null) { 154 | return false; 155 | } 156 | if (getClass() != that.getClass()) { 157 | return false; 158 | } 159 | User other = (User) that; 160 | return (this.getPkid() == null ? other.getPkid() == null : this.getPkid().equals(other.getPkid())) 161 | && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) 162 | && (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone())) 163 | && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())); 164 | } 165 | 166 | /** 167 | * This method was generated by MyBatis Generator. 168 | * This method corresponds to the database table user 169 | * 170 | * @mbggenerated 171 | */ 172 | @Override 173 | public int hashCode() { 174 | final int prime = 31; 175 | int result = 1; 176 | result = prime * result + ((getPkid() == null) ? 0 : getPkid().hashCode()); 177 | result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); 178 | result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode()); 179 | result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode()); 180 | return result; 181 | } 182 | 183 | /** 184 | * This method was generated by MyBatis Generator. 185 | * This method corresponds to the database table user 186 | * 187 | * @mbggenerated 188 | */ 189 | @Override 190 | public String toString() { 191 | StringBuilder sb = new StringBuilder(); 192 | sb.append(getClass().getSimpleName()); 193 | sb.append(" ["); 194 | sb.append("Hash = ").append(hashCode()); 195 | sb.append(", pkid=").append(pkid); 196 | sb.append(", name=").append(name); 197 | sb.append(", phone=").append(phone); 198 | sb.append(", password=").append(password); 199 | sb.append(", serialVersionUID=").append(serialVersionUID); 200 | sb.append("]"); 201 | return sb.toString(); 202 | } 203 | } -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/service/IStarterCustomService.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.service; 2 | 3 | public interface IStarterCustomService { 4 | 5 | String urlDecoration(String param); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/service/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.service; 2 | 3 | import com.tuacy.microservice.framework.user.manage.api.response.UserInfoEntity; 4 | 5 | public interface IUserService { 6 | 7 | /** 8 | * 获取用户信息 9 | * 10 | * @return 用户信息实体类 11 | */ 12 | UserInfoEntity getUserInfo(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/service/impl/StarterCustomServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.service.impl; 2 | 3 | import com.tuacy.microservice.framework.user.manage.service.IStarterCustomService; 4 | import com.tuacy.starter.url.decoration.UrlDecorationService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service(value = "starter-custom") 9 | public class StarterCustomServiceImpl implements IStarterCustomService { 10 | 11 | private UrlDecorationService urlDecorationService; 12 | 13 | @Autowired 14 | public void setUrlDecorationService(UrlDecorationService urlDecorationService) { 15 | this.urlDecorationService = urlDecorationService; 16 | } 17 | 18 | @Override 19 | public String urlDecoration(String param) { 20 | return urlDecorationService.url(param); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /user-manage/src/main/java/com/tuacy/microservice/framework/user/manage/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.tuacy.microservice.framework.user.manage.service.impl; 2 | 3 | import com.tuacy.microservice.framework.user.manage.api.response.UserInfoEntity; 4 | import com.tuacy.microservice.framework.user.manage.mapper.UserManageMapper; 5 | import com.tuacy.microservice.framework.user.manage.service.IUserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | @Service(value = "user-service") 11 | public class UserServiceImpl implements IUserService { 12 | 13 | 14 | private UserManageMapper userManageMapper; 15 | 16 | 17 | @Autowired 18 | public void setUserManageMapper(UserManageMapper userManageMapper) { 19 | this.userManageMapper = userManageMapper; 20 | } 21 | 22 | @Transactional(rollbackFor = Exception.class) 23 | @Override 24 | public UserInfoEntity getUserInfo() { 25 | UserInfoEntity userInfoEntity = new UserInfoEntity(); 26 | userInfoEntity.setName("tuacy"); 27 | userInfoEntity.setPhone("1868888"); 28 | userInfoEntity.setPassword("tuacy"); 29 | userManageMapper.saveUserInfo(userInfoEntity); 30 | return userInfoEntity; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /user-manage/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #eureka: 2 | # client: 3 | # serviceUrl: 4 | # defaultZone: http://localhost:8761/eureka/ 5 | # 6 | # instance: 7 | # instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} 8 | # prefer-ip-address: true 9 | # leaseRenewalIntervalInSeconds: 5 10 | # leaseExpirationDurationInSeconds: 5 11 | 12 | mybatis: 13 | typeAliasesPackage: com.tuacy.microservice.framework.user.manage.model 14 | mapperLocations: classpath:mapper/*.xml 15 | configuration: 16 | log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 17 | 18 | # 分页配置 19 | pagehelper: 20 | helper-dialect: mysql 21 | reasonable: true 22 | support-methods-arguments: true 23 | params: count=countSql 24 | 25 | #hystrix: 26 | # threadpool: 27 | # default: 28 | # coreSize: 200 ##并发执行的最大线程数,默认10 29 | # maxQueueSize: 200 ##BlockingQueue的最大队列数 30 | # queueSizeRejectionThreshold: 200 ##即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝 31 | # command: 32 | # default: 33 | # execution: 34 | # timeout: 35 | # enabled: false 36 | # circuitBreaker: 37 | # errorThresholdPercentage: 50 # 50% 38 | # sleepWindowInMilliseconds: 100000 # 100s 39 | # customCommandKey: 40 | # fallback: 41 | # enabled: false 42 | # circuitBreaker: 43 | # errorThresholdPercentage: 75 # 75% 44 | # sleepWindowInMilliseconds: 15000 # 15s 45 | -------------------------------------------------------------------------------- /user-manage/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2222 3 | url: # 我们自定义的starter 4 | protocol: http 5 | host: 192.168.0.1 6 | port: 8080 7 | spring: 8 | application: 9 | name: microservice-framework-user-manage 10 | datasource: 11 | url: jdbc:mysql://localhost:3306/tuacydev?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false 12 | username: root 13 | password: 123456 14 | driver-class-name: com.mysql.jdbc.Driver 15 | # 使用druid数据源 16 | type: com.alibaba.druid.pool.DruidDataSource 17 | cloud: 18 | config: # spring could config client 19 | discovery: 20 | enabled: true #开启配置服务发现 21 | service-id: microservice-center-config-server #配置服务实例名称 22 | label: master #配置文件所在分支 23 | 24 | 25 | -------------------------------------------------------------------------------- /user-manage/src/main/resources/generator/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 43 | 44 | 45 | 46 | 47 | 48 | 52 |
53 | 54 | 55 | 56 | 57 | 58 |
59 |
60 | -------------------------------------------------------------------------------- /user-manage/src/main/resources/mapper/UserManageMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | 13 | 14 | insert into user (name, phone, password) values (#{userInfo.name}, #{userInfo.phone}, #{userInfo.password}) 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /user-manage/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 19 | pkid, name, phone, password 20 | 21 | 31 | 32 | 36 | delete from user 37 | where pkid = #{pkid,jdbcType=SMALLINT} 38 | 39 | 40 | 44 | insert into user (pkid, name, phone, 45 | password) 46 | values (#{pkid,jdbcType=SMALLINT}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, 47 | #{password,jdbcType=VARCHAR}) 48 | 49 | 50 | 54 | insert into user 55 | 56 | 57 | pkid, 58 | 59 | 60 | name, 61 | 62 | 63 | phone, 64 | 65 | 66 | password, 67 | 68 | 69 | 70 | 71 | #{pkid,jdbcType=SMALLINT}, 72 | 73 | 74 | #{name,jdbcType=VARCHAR}, 75 | 76 | 77 | #{phone,jdbcType=VARCHAR}, 78 | 79 | 80 | #{password,jdbcType=VARCHAR}, 81 | 82 | 83 | 84 | 85 | 89 | update user 90 | 91 | 92 | name = #{name,jdbcType=VARCHAR}, 93 | 94 | 95 | phone = #{phone,jdbcType=VARCHAR}, 96 | 97 | 98 | password = #{password,jdbcType=VARCHAR}, 99 | 100 | 101 | where pkid = #{pkid,jdbcType=SMALLINT} 102 | 103 | 104 | 108 | update user 109 | set name = #{name,jdbcType=VARCHAR}, 110 | phone = #{phone,jdbcType=VARCHAR}, 111 | password = #{password,jdbcType=VARCHAR} 112 | where pkid = #{pkid,jdbcType=SMALLINT} 113 | 114 | --------------------------------------------------------------------------------