调整
This commit is contained in:
parent
46753e4f02
commit
1fe64d55ce
@ -1,76 +0,0 @@
|
||||
<!-- Easy-FLV: Java RTSP/RTMP to FLV Converter -->
|
||||
# 📺 Easy-FLV: Java 实现的 RTSP/RTMP 到 FLV 转换器
|
||||
|
||||
|
||||
## 🌟 关于 Easy-FLV
|
||||
Easy-FLV 是一个用 Java 实现的库,它能够将 RTSP 或 RTMP 视频流转换为 FLV 格式,以便在浏览器中播放。它为实时视频监控、直播和视频流处理提供了一个高效、稳定且易于集成的解决方案。
|
||||
|
||||
### 为什么选择 Easy-FLV?
|
||||
- **高效转换**:快速将视频流转换为 FLV 格式,无需复杂配置。
|
||||
- **易于集成**:作为 Spring Boot Starter 使用,轻松集成到任何 Java 项目。
|
||||
- **现代浏览器支持**:支持所有主流浏览器,无需额外插件。
|
||||
- **实时流处理**:适用于实时视频流的转换,如安防监控和直播。
|
||||
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 添加 Maven 依赖
|
||||
在你的 Spring Boot 项目中,添加以下 Maven 依赖:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-flv</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 实现接口
|
||||
创建一个服务类来实现 `IOpenFLVService` 接口,并提供流地址:
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class RtspDataService implements IOpenFLVService {
|
||||
|
||||
@Override
|
||||
public String getUrl(Integer channel) {
|
||||
// 根据 channel 获取 RTSP 视频流地址
|
||||
return "rtsp://xx.xx.xx.xx/openUrl/16HV8mA";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 配置 YAML
|
||||
在 `application.yml` 中配置 Easy-FLV:
|
||||
|
||||
```yaml
|
||||
easy:
|
||||
flv:
|
||||
host: http://localhost:8200
|
||||
```
|
||||
|
||||
### 使用接口
|
||||
通过以下接口获取转换后的流地址,并在浏览器中播放:
|
||||
|
||||
- 转换地址:`GET http://ip:port/get/flv/hls/stream_{channel}.flv`
|
||||
- 播放地址:`GET http://ip:port/flv/hls/stream_{channel}.flv`
|
||||
|
||||
### 直接使用
|
||||
如果不使用接口,可以直接编码流地址并转换:
|
||||
|
||||
```java
|
||||
public static void main(String[] args) throws UnsupportedEncodingException {
|
||||
String url = "rtsp://XXXXXXXX";
|
||||
String encodedUrl = java.net.URLEncoder.encode(url, "UTF-8");
|
||||
System.out.println("Encoded Stream URL: " + encodedUrl);
|
||||
}
|
||||
```
|
||||
|
||||
- 转换地址:`GET http://ip:port/get/flv/hls/stream?url=编码后的地址`
|
||||
- 播放地址:`GET http://ip:port/flv/hls/stream?url=编码后的地址`
|
||||
|
||||
## 🛠️ 贡献
|
||||
我们欢迎任何形式的贡献,包括但不限于报告 bug、提交修复、添加新功能、改进文档等。
|
||||
|
||||
## 📄 许可证
|
||||
Easy-FLV 根据 [Apache License 2.0](LICENSE) 发布。
|
@ -1,229 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-flv</artifactId>
|
||||
<version>1.1</version>
|
||||
<name>视频模块</name>
|
||||
<description>视频流转换 rtsp/rtmp 转flv</description>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name> The Apache Software License, Version 2.0 </name>
|
||||
<url> http://www.apache.org/licenses/LICENSE-2.0.txt </url>
|
||||
<distribution> repo </distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
<scm>
|
||||
<url>https://github.com/javpower/easy-flv</url>
|
||||
<connection>scm:git@github.com/javpower/easy-flv.git</connection>
|
||||
<developerConnection>scm:git@github.com/javpower/easy-flv.git</developerConnection>
|
||||
</scm>
|
||||
<developers>
|
||||
<developer>
|
||||
<name>gc.x</name>
|
||||
<email>javpower@163.com</email>
|
||||
<organization> https://github.com/javpower</organization>
|
||||
<timezone>+8</timezone>
|
||||
</developer>
|
||||
</developers>
|
||||
<properties>
|
||||
<project.version>0.0.1-SNAPSHOT</project.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-boot.version>2.7.13</spring-boot.version>
|
||||
<mica-auto.vaersion>2.3.2</mica-auto.vaersion>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-logging</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lmax</groupId>
|
||||
<artifactId>disruptor</artifactId>
|
||||
<version>3.4.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.31</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>javacv-platform</artifactId>
|
||||
<version>1.5.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Additional dependencies required to use CUDA and cuDNN -->
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>opencv-platform-gpu</artifactId>
|
||||
<version>4.7.0-1.5.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Optional GPL builds with (almost) everything enabled -->
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>ffmpeg-platform-gpl</artifactId>
|
||||
<version>6.0-1.5.9</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.dreamlu</groupId>
|
||||
<artifactId>mica-auto</artifactId>
|
||||
<version>${mica-auto.vaersion}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<url> https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>com.gc.easy.EasyHttpApplication</mainClass>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<version>1.6.7</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<serverId>ossrh</serverId>
|
||||
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
|
||||
<autoReleaseAfterClose>true</autoReleaseAfterClose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId> org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<configuration>
|
||||
<show>private</show>
|
||||
<nohelp>true</nohelp>
|
||||
<charset>UTF-8</charset>
|
||||
<encoding>UTF-8</encoding>
|
||||
<docencoding>UTF-8</docencoding>
|
||||
<additionalparam>-Xdoclint:none</additionalparam>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,15 +0,0 @@
|
||||
package com.gc.easy.flv.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ConfigurationProperties(prefix = "easy.flv")
|
||||
@Data
|
||||
@Component
|
||||
public class FlvConfig {
|
||||
private String host;
|
||||
private Integer wight=1920;
|
||||
private Integer height=1080;
|
||||
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package com.gc.easy.flv.controller;
|
||||
|
||||
import com.gc.easy.flv.service.IFLVService;
|
||||
import com.gc.easy.flv.service.IOpenFLVService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.annotation.rest.AnonymousGetMapping;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* FLV流转换
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
@Slf4j
|
||||
@RequestMapping("api/front/flv")
|
||||
@Api(tags = "微信:拉流播放")
|
||||
public class FLVController {
|
||||
|
||||
@Autowired
|
||||
private IFLVService service;
|
||||
|
||||
@Autowired(required = false)
|
||||
private IOpenFLVService openFLVService;
|
||||
|
||||
@Log("拉流播放")
|
||||
@ApiOperation(value = "打开视频流")
|
||||
@AnonymousGetMapping(value = "/get/stream")
|
||||
public void open(String url, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
|
||||
if (!StringUtils.isEmpty(url)) {
|
||||
String decodedUrl = java.net.URLDecoder.decode(url, "UTF-8");
|
||||
service.open(decodedUrl, response, request, openFLVService);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package com.gc.easy.flv.controller;
|
||||
|
||||
import com.gc.easy.flv.config.FlvConfig;
|
||||
import me.zhengjie.annotation.rest.AnonymousGetMapping;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* FLV流转换
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
@Controller
|
||||
public class FLVPlayController {
|
||||
@Autowired
|
||||
private FlvConfig flvConfig;
|
||||
|
||||
@AnonymousGetMapping(value = "/api/front/flv/hls/stream")
|
||||
public String getAppHtml1(String url, Model model) throws UnsupportedEncodingException {
|
||||
String decodedUrl = java.net.URLDecoder.decode(url, "UTF-8");
|
||||
String videoPath="/api/front/flv/get/stream?url="+decodedUrl;
|
||||
model.addAttribute("videoPath", videoPath);
|
||||
model.addAttribute("wight", flvConfig.getWight());
|
||||
model.addAttribute("height", flvConfig.getHeight());
|
||||
return "video";
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package com.gc.easy.flv.factories;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Converter {
|
||||
|
||||
/**
|
||||
* 获取该转换的key
|
||||
*/
|
||||
public String getKey();
|
||||
|
||||
/**
|
||||
* 获取该转换的url
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getUrl();
|
||||
|
||||
/**
|
||||
* 添加一个流输出
|
||||
*
|
||||
* @param entity
|
||||
*/
|
||||
public void addOutputStreamEntity(String key, AsyncContext entity) throws IOException;
|
||||
|
||||
/**
|
||||
* 退出转换
|
||||
*/
|
||||
public void exit();
|
||||
|
||||
/**
|
||||
* 启动
|
||||
*/
|
||||
public void start();
|
||||
|
||||
}
|
@ -1,232 +0,0 @@
|
||||
package com.gc.easy.flv.factories;
|
||||
|
||||
import com.alibaba.fastjson.util.IOUtils;
|
||||
import com.gc.easy.flv.service.IOpenFLVService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bytedeco.ffmpeg.avcodec.AVPacket;
|
||||
import org.bytedeco.ffmpeg.global.avcodec;
|
||||
import org.bytedeco.javacv.FFmpegFrameGrabber;
|
||||
import org.bytedeco.javacv.FFmpegFrameRecorder;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* javacv转包装<br/>
|
||||
* 无须转码,更低的资源消耗,更低的延迟<br/>
|
||||
* 确保流来源视频H264格式,音频AAC格式
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
@Slf4j
|
||||
public class ConverterFactories extends Thread implements Converter {
|
||||
public volatile boolean runing = true;
|
||||
/**
|
||||
* 读流器
|
||||
*/
|
||||
private FFmpegFrameGrabber grabber;
|
||||
/**
|
||||
* 转码器
|
||||
*/
|
||||
private FFmpegFrameRecorder recorder;
|
||||
/**
|
||||
* 转FLV格式的头信息<br/>
|
||||
* 如果有第二个客户端播放首先要返回头信息
|
||||
*/
|
||||
private byte[] headers;
|
||||
/**
|
||||
* 保存转换好的流
|
||||
*/
|
||||
private ByteArrayOutputStream stream;
|
||||
/**
|
||||
* 流地址,h264,aac
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 流输出
|
||||
*/
|
||||
private List<AsyncContext> outEntitys;
|
||||
|
||||
/**
|
||||
* key用于表示这个转换器
|
||||
*/
|
||||
private String key;
|
||||
|
||||
/**
|
||||
* 转换队列
|
||||
*/
|
||||
private Map<String, Converter> factories;
|
||||
|
||||
private IOpenFLVService openFLVService;
|
||||
|
||||
|
||||
public ConverterFactories(String url, String key, Map<String, Converter> factories, List<AsyncContext> outEntitys, IOpenFLVService openFLVService) {
|
||||
this.url = url;
|
||||
this.key = key;
|
||||
this.factories = factories;
|
||||
this.outEntitys = outEntitys;
|
||||
this.openFLVService = openFLVService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
boolean isCloseGrabberAndResponse = true;
|
||||
try {
|
||||
grabber = new FFmpegFrameGrabber(url);
|
||||
if ("rtsp".equals(url.substring(0, 4))) {
|
||||
grabber.setOption("rtsp_transport", "tcp");
|
||||
grabber.setOption("timeout", "5000000");
|
||||
}
|
||||
grabber.start();
|
||||
if (avcodec.AV_CODEC_ID_H264 == grabber.getVideoCodec()
|
||||
&& (grabber.getAudioChannels() == 0 || avcodec.AV_CODEC_ID_AAC == grabber.getAudioCodec()) && (openFLVService == null || !openFLVService.openPreview())) {
|
||||
log.info("this url:{} converterFactories start", url);
|
||||
// 来源视频H264格式,音频AAC格式
|
||||
// 无须转码,更低的资源消耗,更低的延迟
|
||||
stream = new ByteArrayOutputStream();
|
||||
recorder = new FFmpegFrameRecorder(stream, grabber.getImageWidth(), grabber.getImageHeight(),
|
||||
grabber.getAudioChannels());
|
||||
recorder.setInterleaved(true);
|
||||
recorder.setVideoOption("preset", "ultrafast");
|
||||
recorder.setVideoOption("tune", "zerolatency");
|
||||
recorder.setVideoOption("crf", "25");
|
||||
recorder.setFrameRate(grabber.getFrameRate());
|
||||
recorder.setSampleRate(grabber.getSampleRate());
|
||||
if (grabber.getAudioChannels() > 0) {
|
||||
recorder.setAudioChannels(grabber.getAudioChannels());
|
||||
recorder.setAudioBitrate(grabber.getAudioBitrate());
|
||||
recorder.setAudioCodec(grabber.getAudioCodec());
|
||||
}
|
||||
recorder.setFormat("flv");
|
||||
recorder.setVideoBitrate(grabber.getVideoBitrate());
|
||||
recorder.setVideoCodec(grabber.getVideoCodec());
|
||||
recorder.start(grabber.getFormatContext());
|
||||
if (headers == null) {
|
||||
headers = stream.toByteArray();
|
||||
stream.reset();
|
||||
writeResponse(headers);
|
||||
}
|
||||
int nullNumber = 0;
|
||||
while (runing) {
|
||||
AVPacket k = grabber.grabPacket();
|
||||
if (k != null) {
|
||||
try {
|
||||
recorder.recordPacket(k);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (stream.size() > 0) {
|
||||
byte[] b = stream.toByteArray();
|
||||
stream.reset();
|
||||
writeResponse(b);
|
||||
if (outEntitys.isEmpty()) {
|
||||
log.info("没有输出退出");
|
||||
break;
|
||||
}
|
||||
}
|
||||
avcodec.av_packet_unref(k);
|
||||
} else {
|
||||
nullNumber++;
|
||||
if (nullNumber > 200) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(5);
|
||||
}
|
||||
} else {
|
||||
isCloseGrabberAndResponse = false;
|
||||
// 需要转码为视频H264格式,音频AAC格式
|
||||
ConverterTranFactories c = new ConverterTranFactories(url, key, factories, outEntitys, grabber, openFLVService);
|
||||
factories.put(key, c);
|
||||
c.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
closeConverter(isCloseGrabberAndResponse);
|
||||
completeResponse(isCloseGrabberAndResponse);
|
||||
log.info("this url:{} converterFactories exit", url);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出FLV视频流
|
||||
*
|
||||
* @param b
|
||||
*/
|
||||
public void writeResponse(byte[] b) {
|
||||
Iterator<AsyncContext> it = outEntitys.iterator();
|
||||
while (it.hasNext()) {
|
||||
AsyncContext o = it.next();
|
||||
try {
|
||||
o.getResponse().getOutputStream().write(b);
|
||||
} catch (Exception e) {
|
||||
log.info("移除一个输出");
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出转换
|
||||
*/
|
||||
public void closeConverter(boolean isCloseGrabberAndResponse) {
|
||||
if (isCloseGrabberAndResponse) {
|
||||
IOUtils.close(grabber);
|
||||
factories.remove(this.key);
|
||||
}
|
||||
IOUtils.close(recorder);
|
||||
IOUtils.close(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭异步响应
|
||||
*
|
||||
* @param isCloseGrabberAndResponse
|
||||
*/
|
||||
public void completeResponse(boolean isCloseGrabberAndResponse) {
|
||||
if (isCloseGrabberAndResponse) {
|
||||
Iterator<AsyncContext> it = outEntitys.iterator();
|
||||
while (it.hasNext()) {
|
||||
AsyncContext o = it.next();
|
||||
o.complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOutputStreamEntity(String key, AsyncContext entity) throws IOException {
|
||||
if (headers == null) {
|
||||
outEntitys.add(entity);
|
||||
} else {
|
||||
entity.getResponse().getOutputStream().write(headers);
|
||||
entity.getResponse().getOutputStream().flush();
|
||||
outEntitys.add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit() {
|
||||
this.runing = false;
|
||||
try {
|
||||
this.join();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,230 +0,0 @@
|
||||
package com.gc.easy.flv.factories;
|
||||
|
||||
import com.alibaba.fastjson.util.IOUtils;
|
||||
import com.gc.easy.flv.service.IOpenFLVService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bytedeco.ffmpeg.global.avcodec;
|
||||
import org.bytedeco.javacv.FFmpegFrameGrabber;
|
||||
import org.bytedeco.javacv.FFmpegFrameRecorder;
|
||||
import org.bytedeco.javacv.Frame;
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
import org.bytedeco.opencv.opencv_core.IplImage;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* javacv转码<br/>
|
||||
* 流来源不是视频H264格式,音频AAC格式 转码为视频H264格式,音频AAC格式
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
@Slf4j
|
||||
public class ConverterTranFactories extends Thread implements Converter {
|
||||
public volatile boolean runing = true;
|
||||
/**
|
||||
* 读流器
|
||||
*/
|
||||
private FFmpegFrameGrabber grabber;
|
||||
/**
|
||||
* 转码器
|
||||
*/
|
||||
private FFmpegFrameRecorder recorder;
|
||||
/**
|
||||
* 转FLV格式的头信息<br/>
|
||||
* 如果有第二个客户端播放首先要返回头信息
|
||||
*/
|
||||
private byte[] headers;
|
||||
/**
|
||||
* 保存转换好的流
|
||||
*/
|
||||
private ByteArrayOutputStream stream;
|
||||
/**
|
||||
* 流地址,h264,aac
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 流输出
|
||||
*/
|
||||
private List<AsyncContext> outEntitys;
|
||||
|
||||
/**
|
||||
* key用于表示这个转换器
|
||||
*/
|
||||
private String key;
|
||||
|
||||
/**
|
||||
* 转换队列
|
||||
*/
|
||||
private Map<String, Converter> factories;
|
||||
|
||||
private OpenCVFrameConverter.ToIplImage converter;
|
||||
private IOpenFLVService openFLVService;
|
||||
|
||||
|
||||
public ConverterTranFactories(String url, String key, Map<String, Converter> factories,
|
||||
List<AsyncContext> outEntitys, FFmpegFrameGrabber grabber, IOpenFLVService openFLVService) {
|
||||
this.url = url;
|
||||
this.key = key;
|
||||
this.factories = factories;
|
||||
this.outEntitys = outEntitys;
|
||||
this.grabber = grabber;
|
||||
this.openFLVService=openFLVService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
log.info("this url:{} converterTranFactories start", url);
|
||||
grabber.setFrameRate(25);
|
||||
if (grabber.getImageWidth() > 1920) {
|
||||
grabber.setImageWidth(1920);
|
||||
}
|
||||
if (grabber.getImageHeight() > 1080) {
|
||||
grabber.setImageHeight(1080);
|
||||
}
|
||||
stream = new ByteArrayOutputStream();
|
||||
recorder = new FFmpegFrameRecorder(stream, grabber.getImageWidth(), grabber.getImageHeight(),
|
||||
grabber.getAudioChannels());
|
||||
converter = new OpenCVFrameConverter.ToIplImage();
|
||||
|
||||
recorder.setInterleaved(true);
|
||||
recorder.setVideoOption("preset", "ultrafast");
|
||||
recorder.setVideoOption("tune", "zerolatency");
|
||||
recorder.setVideoOption("crf", "25");
|
||||
recorder.setGopSize(50);
|
||||
recorder.setFrameRate(25);
|
||||
recorder.setSampleRate(grabber.getSampleRate());
|
||||
if (grabber.getAudioChannels() > 0) {
|
||||
recorder.setAudioChannels(grabber.getAudioChannels());
|
||||
recorder.setAudioBitrate(grabber.getAudioBitrate());
|
||||
recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
|
||||
}
|
||||
recorder.setFormat("flv");
|
||||
recorder.setVideoBitrate(grabber.getVideoBitrate());
|
||||
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
|
||||
recorder.start();
|
||||
if (headers == null) {
|
||||
headers = stream.toByteArray();
|
||||
stream.reset();
|
||||
writeResponse(headers);
|
||||
}
|
||||
int nullNumber = 0;
|
||||
while (runing) {
|
||||
// 抓取一帧
|
||||
Frame f = grabber.grab();
|
||||
if(openFLVService!=null&&openFLVService.openPreview()){
|
||||
//预处理
|
||||
IplImage iplImage = converter.convert(f);//抓取一帧视频并将其转换为图像,至于用这个图像用来做什么?加水印,人脸识别等等自行添加
|
||||
if (iplImage != null) {
|
||||
openFLVService.preview(iplImage);
|
||||
f = converter.convert(iplImage);
|
||||
}
|
||||
}
|
||||
if (f != null) {
|
||||
try {
|
||||
// 转码
|
||||
recorder.record(f);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (stream.size() > 0) {
|
||||
byte[] b = stream.toByteArray();
|
||||
stream.reset();
|
||||
writeResponse(b);
|
||||
if (outEntitys.isEmpty()) {
|
||||
log.info("没有输出退出");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
nullNumber++;
|
||||
if (nullNumber > 200) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(5);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
closeConverter();
|
||||
completeResponse();
|
||||
log.info("this url:{} converterTranFactories exit", url);
|
||||
factories.remove(this.key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出FLV视频流
|
||||
*
|
||||
* @param b
|
||||
*/
|
||||
public void writeResponse(byte[] b) {
|
||||
Iterator<AsyncContext> it = outEntitys.iterator();
|
||||
while (it.hasNext()) {
|
||||
AsyncContext o = it.next();
|
||||
try {
|
||||
o.getResponse().getOutputStream().write(b);
|
||||
} catch (Exception e) {
|
||||
log.info("移除一个输出");
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出转换
|
||||
*/
|
||||
public void closeConverter() {
|
||||
IOUtils.close(grabber);
|
||||
IOUtils.close(recorder);
|
||||
IOUtils.close(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭异步响应
|
||||
*/
|
||||
public void completeResponse() {
|
||||
Iterator<AsyncContext> it = outEntitys.iterator();
|
||||
while (it.hasNext()) {
|
||||
AsyncContext o = it.next();
|
||||
o.complete();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOutputStreamEntity(String key, AsyncContext entity) throws IOException {
|
||||
if (headers == null) {
|
||||
outEntitys.add(entity);
|
||||
} else {
|
||||
entity.getResponse().getOutputStream().write(headers);
|
||||
entity.getResponse().getOutputStream().flush();
|
||||
outEntitys.add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit() {
|
||||
this.runing = false;
|
||||
try {
|
||||
this.join();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package com.gc.easy.flv.factories.state;
|
||||
|
||||
/**
|
||||
* 转换器状态(初始化、打开、关闭、错误、运行)
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
public enum ConverterState {
|
||||
INITIAL, OPEN, CLOSE, ERROR, RUN
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.gc.easy.flv.factories.state;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class OutputImage {
|
||||
//图片
|
||||
private byte[] image;
|
||||
|
||||
private Date recordTime;
|
||||
|
||||
private Integer channel;
|
||||
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.gc.easy.flv.service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public interface IFLVService {
|
||||
|
||||
/**
|
||||
* 打开一个流地址
|
||||
*
|
||||
* @param url
|
||||
* @param response
|
||||
*/
|
||||
public void open(Integer channel,String url, HttpServletResponse response, HttpServletRequest request,IOpenFLVService openFLVService);
|
||||
public void open(String url, HttpServletResponse response, HttpServletRequest request,IOpenFLVService openFLVService);
|
||||
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.gc.easy.flv.service;
|
||||
|
||||
import org.bytedeco.opencv.opencv_core.IplImage;
|
||||
|
||||
public interface IOpenFLVService {
|
||||
|
||||
|
||||
/**
|
||||
* 通过通道号获取url
|
||||
* @param channel
|
||||
* @return
|
||||
*/
|
||||
String getUrl(Integer channel);
|
||||
|
||||
/**
|
||||
* 抓取一帧视频并将其转换为图像预处理
|
||||
* @param iplImage
|
||||
*/
|
||||
void preview(IplImage iplImage);
|
||||
|
||||
/**
|
||||
* 开启预处理
|
||||
* @return
|
||||
*/
|
||||
boolean openPreview();
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
package com.gc.easy.flv.service.impl;
|
||||
|
||||
|
||||
import com.gc.easy.flv.factories.Converter;
|
||||
import com.gc.easy.flv.factories.ConverterFactories;
|
||||
import com.gc.easy.flv.service.IFLVService;
|
||||
import com.gc.easy.flv.service.IOpenFLVService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* FLV流转换
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class FLVService implements IFLVService {
|
||||
|
||||
private ConcurrentHashMap<String, Converter> converters = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void open(String url, HttpServletResponse response, HttpServletRequest request, IOpenFLVService openFLVService) {
|
||||
open(null, url, response, request, openFLVService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open(Integer channel, String url, HttpServletResponse response, HttpServletRequest request, IOpenFLVService openFLVService) {
|
||||
String key = md5(url);
|
||||
AsyncContext async = request.startAsync();
|
||||
async.setTimeout(0);
|
||||
if (converters.containsKey(key)) {
|
||||
Converter c = converters.get(key);
|
||||
try {
|
||||
c.addOutputStreamEntity(key, async);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new IllegalArgumentException(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
List<AsyncContext> outs = new ArrayList<>();
|
||||
outs.add(async);
|
||||
ConverterFactories c = new ConverterFactories(url, key, converters, outs, openFLVService);
|
||||
c.start();
|
||||
converters.put(key, c);
|
||||
}
|
||||
response.setContentType("video/x-flv");
|
||||
response.setHeader("Connection", "keep-alive");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
try {
|
||||
response.flushBuffer();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public String md5(String plainText) {
|
||||
StringBuilder buf = null;
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(plainText.getBytes());
|
||||
byte b[] = md.digest();
|
||||
int i;
|
||||
buf = new StringBuilder("");
|
||||
for (int offset = 0; offset < b.length; offset++) {
|
||||
i = b[offset];
|
||||
if (i < 0)
|
||||
i += 256;
|
||||
if (i < 16)
|
||||
buf.append("0");
|
||||
buf.append(Integer.toHexString(i));
|
||||
}
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package com.gc.easy.flv.service.impl;
|
||||
|
||||
|
||||
import com.gc.easy.flv.service.IOpenFLVService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bytedeco.opencv.opencv_core.IplImage;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* FLV流转换
|
||||
*
|
||||
* @author gc.x
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OpenFLVService implements IOpenFLVService {
|
||||
|
||||
|
||||
@Override
|
||||
public String getUrl(Integer channel) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preview(IplImage iplImage) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean openPreview() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
package com.gc.easy.flv.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.bytedeco.javacv.FFmpegFrameGrabber;
|
||||
import org.bytedeco.javacv.Frame;
|
||||
import org.bytedeco.javacv.FrameGrabber;
|
||||
import org.bytedeco.javacv.Java2DFrameConverter;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FlvUtil {
|
||||
|
||||
private static BufferedImage getBufferedImageByFrame(String filePath) throws IOException {
|
||||
FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(filePath);
|
||||
return getBufferedImageByFrame(grabber);
|
||||
}
|
||||
|
||||
private static BufferedImage getBufferedImageByFrame(FFmpegFrameGrabber grabber)
|
||||
throws FrameGrabber.Exception {
|
||||
grabber.start();
|
||||
Frame frame;
|
||||
frame = grabber.grabImage();
|
||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
||||
BufferedImage buffer = converter.getBufferedImage(frame);
|
||||
grabber.stop();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static byte[] getFlvImg(String path) throws Exception {
|
||||
return bufferedImageToByteArray(getBufferedImageByFrame(path));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将BufferedImage转换为byte[]
|
||||
*
|
||||
* @param image
|
||||
* @return
|
||||
*/
|
||||
public static byte[] bufferedImageToByteArray(BufferedImage image) throws IOException {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "jpg", os);
|
||||
return os.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param data 文件数据
|
||||
* @param url 上传地址
|
||||
* @param fileName 文件名称
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject postFile(byte[] data, String url, String fileName) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
|
||||
ByteArrayResource contentsAsResource = new ByteArrayResource(data) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return fileName;
|
||||
}
|
||||
};
|
||||
paramMap.add("file", contentsAsResource);
|
||||
return restTemplate.postForObject(url, paramMap, JSONObject.class);
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>FLV Video Player</title>
|
||||
|
||||
<!-- 引入flv.js库 -->
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/flv.js/1.5.0/flv.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<video id="videoPlayer" th:style="'width:500px; height:500px;'" controls></video>
|
||||
|
||||
<script th:inline="javascript">
|
||||
(function () {
|
||||
var videoPath = /*[[${videoPath}]]*/ '';
|
||||
|
||||
if (flvjs.isSupported()) {
|
||||
var videoElement = document.getElementById('videoPlayer');
|
||||
var flvPlayer = flvjs.createPlayer({
|
||||
type: 'flv',
|
||||
url: 'http://localhost:8000' + videoPath
|
||||
});
|
||||
|
||||
flvPlayer.attachMediaElement(videoElement);
|
||||
flvPlayer.load();
|
||||
|
||||
// 播放视频
|
||||
videoElement.play();
|
||||
} else {
|
||||
console.error('FLV not supported');
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -1,37 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>eladmin</artifactId>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<version>1.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>eladmin-generator</artifactId>
|
||||
<name>代码生成模块</name>
|
||||
|
||||
<properties>
|
||||
<configuration.version>1.10</configuration.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-common</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!--模板引擎-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
|
||||
<dependency>
|
||||
<groupId>commons-configuration</groupId>
|
||||
<artifactId>commons-configuration</artifactId>
|
||||
<version>${configuration.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 列的数据信息
|
||||
* @author Tz
|
||||
* @date 2019-01-02
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@TableName("code_column")
|
||||
public class ColumnInfo implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "ID", hidden = true)
|
||||
@TableId(value = "column_id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "表名")
|
||||
private String tableName;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段名称")
|
||||
private String columnName;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段类型")
|
||||
private String columnType;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段键类型")
|
||||
private String keyType;
|
||||
|
||||
@ApiModelProperty(value = "字段额外的参数")
|
||||
private String extra;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段描述")
|
||||
private String remark;
|
||||
|
||||
@ApiModelProperty(value = "是否必填")
|
||||
private Boolean notNull;
|
||||
|
||||
@ApiModelProperty(value = "是否在列表显示")
|
||||
private Boolean listShow = true;
|
||||
|
||||
@ApiModelProperty(value = "是否表单显示")
|
||||
private Boolean formShow = true;
|
||||
|
||||
@ApiModelProperty(value = "表单类型")
|
||||
private String formType;
|
||||
|
||||
@ApiModelProperty(value = "查询 1:模糊 2:精确")
|
||||
private String queryType;
|
||||
|
||||
@ApiModelProperty(value = "字典名称")
|
||||
private String dictName;
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 代码生成配置
|
||||
* @author Tz
|
||||
* @date 2019-01-03
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@TableName("code_config")
|
||||
public class GenConfig implements Serializable {
|
||||
|
||||
public GenConfig(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "ID", hidden = true)
|
||||
@TableId(value = "config_id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "表名")
|
||||
private String tableName;
|
||||
|
||||
@ApiModelProperty(value = "接口名称")
|
||||
private String apiAlias;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "包路径")
|
||||
private String pack;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "模块名")
|
||||
private String moduleName;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "前端文件路径")
|
||||
private String path;
|
||||
|
||||
@ApiModelProperty(value = "前端文件路径")
|
||||
private String apiPath;
|
||||
|
||||
@ApiModelProperty(value = "作者")
|
||||
private String author;
|
||||
|
||||
@ApiModelProperty(value = "表前缀")
|
||||
private String prefix;
|
||||
|
||||
@ApiModelProperty(value = "是否覆盖")
|
||||
private Boolean cover = false;
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.domain.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 表的数据信息
|
||||
* @author Tz
|
||||
* @date 2019-01-02
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TableInfo {
|
||||
|
||||
@ApiModelProperty(value = "表名称")
|
||||
private Object tableName;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@ApiModelProperty(value = "创建日期:yyyy-MM-dd HH:mm:ss")
|
||||
private Object createTime;
|
||||
|
||||
@ApiModelProperty(value = "数据库引擎")
|
||||
private Object engine;
|
||||
|
||||
@ApiModelProperty(value = "编码集")
|
||||
private Object coding;
|
||||
|
||||
@ApiModelProperty(value = "备注")
|
||||
private Object remark;
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import me.zhengjie.domain.dto.TableInfo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2023-06-26
|
||||
*/
|
||||
@Mapper
|
||||
public interface ColumnInfoMapper extends BaseMapper<ColumnInfo> {
|
||||
|
||||
IPage<TableInfo> getTables(@Param("tableName") String tableName, Page<Object> page);
|
||||
|
||||
List<ColumnInfo> findByTableNameOrderByIdAsc(@Param("tableName") String tableName);
|
||||
|
||||
List<ColumnInfo> getColumns(@Param("tableName") String tableName);
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2023-06-26
|
||||
*/
|
||||
@Mapper
|
||||
public interface GenConfigMapper extends BaseMapper<GenConfig> {
|
||||
|
||||
GenConfig findByTableName(@Param("tableName") String tableName);
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.rest;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import me.zhengjie.service.GenConfigService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2019-01-14
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/genConfig")
|
||||
@Api(tags = "系统:代码生成器配置管理")
|
||||
public class GenConfigController {
|
||||
|
||||
private final GenConfigService genConfigService;
|
||||
|
||||
@ApiOperation("查询")
|
||||
@GetMapping(value = "/{tableName}")
|
||||
public ResponseEntity<GenConfig> queryGenConfig(@PathVariable String tableName){
|
||||
return new ResponseEntity<>(genConfigService.find(tableName), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@ApiOperation("修改")
|
||||
public ResponseEntity<GenConfig> updateGenConfig(@Validated @RequestBody GenConfig genConfig){
|
||||
return new ResponseEntity<>(genConfigService.update(genConfig.getTableName(), genConfig),HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.rest;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import me.zhengjie.domain.dto.TableInfo;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.service.GenConfigService;
|
||||
import me.zhengjie.service.GeneratorService;
|
||||
import me.zhengjie.utils.PageResult;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2019-01-02
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/generator")
|
||||
@Api(tags = "系统:代码生成管理")
|
||||
public class GeneratorController {
|
||||
|
||||
private final GeneratorService generatorService;
|
||||
private final GenConfigService genConfigService;
|
||||
|
||||
@Value("${generator.enabled}")
|
||||
private Boolean generatorEnabled;
|
||||
|
||||
@ApiOperation("查询数据库数据")
|
||||
@GetMapping(value = "/tables")
|
||||
public ResponseEntity<PageResult<TableInfo>> queryTables(@RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size){
|
||||
return new ResponseEntity<>(generatorService.getTables(name, new Page<>(page, size)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("查询字段数据")
|
||||
@GetMapping(value = "/columns")
|
||||
public ResponseEntity<PageResult<ColumnInfo>> queryColumns(@RequestParam String tableName){
|
||||
List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
|
||||
return new ResponseEntity<>(PageUtil.toPage(columnInfos), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("保存字段数据")
|
||||
@PutMapping
|
||||
public ResponseEntity<HttpStatus> saveColumn(@RequestBody List<ColumnInfo> columnInfos){
|
||||
generatorService.save(columnInfos);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("同步字段数据")
|
||||
@PostMapping(value = "sync")
|
||||
public ResponseEntity<HttpStatus> syncColumn(@RequestBody List<String> tables){
|
||||
for (String table : tables) {
|
||||
generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("生成代码")
|
||||
@PostMapping(value = "/{tableName}/{type}")
|
||||
public ResponseEntity<Object> generatorCode(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
|
||||
if(!generatorEnabled && type == 0){
|
||||
throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看!");
|
||||
}
|
||||
switch (type){
|
||||
// 生成代码
|
||||
case 0: generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
|
||||
break;
|
||||
// 预览
|
||||
case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
|
||||
// 打包
|
||||
case 2: generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
|
||||
break;
|
||||
default: throw new BadRequestException("没有这个选项");
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2019-01-14
|
||||
*/
|
||||
public interface GenConfigService extends IService<GenConfig> {
|
||||
|
||||
/**
|
||||
* 查询表配置
|
||||
* @param tableName 表名
|
||||
* @return 表配置
|
||||
*/
|
||||
GenConfig find(String tableName);
|
||||
|
||||
/**
|
||||
* 更新表配置
|
||||
* @param tableName 表名
|
||||
* @param genConfig 表配置
|
||||
* @return 表配置
|
||||
*/
|
||||
GenConfig update(String tableName, GenConfig genConfig);
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import me.zhengjie.domain.dto.TableInfo;
|
||||
import me.zhengjie.utils.PageResult;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2019-01-02
|
||||
*/
|
||||
public interface GeneratorService extends IService<ColumnInfo> {
|
||||
|
||||
/**
|
||||
* 查询数据库元数据
|
||||
*
|
||||
* @param name 表名
|
||||
* @param page 分页参数
|
||||
* @return /
|
||||
*/
|
||||
PageResult<TableInfo> getTables(String name, Page<Object> page);
|
||||
|
||||
/**
|
||||
* 得到数据表的元数据
|
||||
* @param name 表名
|
||||
* @return /
|
||||
*/
|
||||
List<ColumnInfo> getColumns(String name);
|
||||
|
||||
/**
|
||||
* 同步表数据
|
||||
* @param columnInfos /
|
||||
* @param columnInfoList /
|
||||
*/
|
||||
void sync(List<ColumnInfo> columnInfos, List<ColumnInfo> columnInfoList);
|
||||
|
||||
/**
|
||||
* 保持数据
|
||||
* @param columnInfos /
|
||||
*/
|
||||
void save(List<ColumnInfo> columnInfos);
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息
|
||||
*/
|
||||
void generator(GenConfig genConfig, List<ColumnInfo> columns);
|
||||
|
||||
/**
|
||||
* 预览
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息
|
||||
* @return /
|
||||
*/
|
||||
ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> columns);
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息
|
||||
* @param request /
|
||||
* @param response /
|
||||
*/
|
||||
void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 查询数据库的表字段数据数据
|
||||
* @param table /
|
||||
* @return /
|
||||
*/
|
||||
List<ColumnInfo> query(String table);
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import me.zhengjie.mapper.GenConfigMapper;
|
||||
import me.zhengjie.service.GenConfigService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2019-01-14
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@SuppressWarnings({"unchecked","all"})
|
||||
public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig> implements GenConfigService {
|
||||
|
||||
private final GenConfigMapper genConfigMapper;
|
||||
|
||||
@Override
|
||||
public GenConfig find(String tableName) {
|
||||
GenConfig genConfig = genConfigMapper.findByTableName(tableName);
|
||||
if(genConfig == null){
|
||||
return new GenConfig(tableName);
|
||||
}
|
||||
return genConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenConfig update(String tableName, GenConfig genConfig) {
|
||||
String separator = File.separator;
|
||||
String[] paths;
|
||||
String symbol = "\\";
|
||||
if (symbol.equals(separator)) {
|
||||
paths = genConfig.getPath().split("\\\\");
|
||||
} else {
|
||||
paths = genConfig.getPath().split(File.separator);
|
||||
}
|
||||
StringBuilder api = new StringBuilder();
|
||||
for (String path : paths) {
|
||||
api.append(path);
|
||||
api.append(separator);
|
||||
if ("src".equals(path)) {
|
||||
api.append("api");
|
||||
break;
|
||||
}
|
||||
}
|
||||
genConfig.setApiPath(api.toString());
|
||||
saveOrUpdate(genConfig);
|
||||
return genConfig;
|
||||
}
|
||||
}
|
@ -1,161 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import me.zhengjie.domain.dto.TableInfo;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.mapper.ColumnInfoMapper;
|
||||
import me.zhengjie.service.GeneratorService;
|
||||
import me.zhengjie.utils.*;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Tz
|
||||
* @date 2019-01-02
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorServiceImpl extends ServiceImpl<ColumnInfoMapper, ColumnInfo> implements GeneratorService {
|
||||
|
||||
private final ColumnInfoMapper columnInfoMapper;
|
||||
private final String CONFIG_MESSAGE = "请先配置生成器";
|
||||
|
||||
@Override
|
||||
public PageResult<TableInfo> getTables(String name, Page<Object> page) {
|
||||
return PageUtil.toPage(columnInfoMapper.getTables(name, page));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<ColumnInfo> getColumns(String tableName) {
|
||||
List<ColumnInfo> columnInfos = columnInfoMapper.findByTableNameOrderByIdAsc(tableName);
|
||||
if (CollectionUtil.isNotEmpty(columnInfos)) {
|
||||
return columnInfos;
|
||||
} else {
|
||||
columnInfos = query(tableName);
|
||||
saveBatch(columnInfos);
|
||||
return columnInfos;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ColumnInfo> query(String tableName) {
|
||||
List<ColumnInfo> columnInfos = columnInfoMapper.getColumns(tableName);
|
||||
for (ColumnInfo columnInfo : columnInfos) {
|
||||
columnInfo.setTableName(tableName);
|
||||
if(GenUtil.PK.equalsIgnoreCase(columnInfo.getKeyType())
|
||||
&& GenUtil.EXTRA.equalsIgnoreCase(columnInfo.getExtra())){
|
||||
columnInfo.setNotNull(false);
|
||||
}
|
||||
}
|
||||
return columnInfos;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void sync(List<ColumnInfo> columnInfos, List<ColumnInfo> columnInfoList) {
|
||||
// 第一种情况,数据库类字段改变或者新增字段
|
||||
for (ColumnInfo columnInfo : columnInfoList) {
|
||||
// 根据字段名称查找
|
||||
List<ColumnInfo> columns = columnInfos.stream().filter(c -> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList());
|
||||
// 如果能找到,就修改部分可能被字段
|
||||
if (CollectionUtil.isNotEmpty(columns)) {
|
||||
ColumnInfo column = columns.get(0);
|
||||
column.setColumnType(columnInfo.getColumnType());
|
||||
column.setExtra(columnInfo.getExtra());
|
||||
column.setKeyType(columnInfo.getKeyType());
|
||||
if (StringUtils.isBlank(column.getRemark())) {
|
||||
column.setRemark(columnInfo.getRemark());
|
||||
}
|
||||
saveOrUpdate(column);
|
||||
} else {
|
||||
// 如果找不到,则保存新字段信息
|
||||
save(columnInfo);
|
||||
}
|
||||
}
|
||||
// 第二种情况,数据库字段删除了
|
||||
for (ColumnInfo columnInfo : columnInfos) {
|
||||
// 根据字段名称查找
|
||||
List<ColumnInfo> columns = columnInfoList.stream().filter(c -> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList());
|
||||
// 如果找不到,就代表字段被删除了,则需要删除该字段
|
||||
if (CollectionUtil.isEmpty(columns)) {
|
||||
removeById(columnInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(List<ColumnInfo> columnInfos) {
|
||||
saveOrUpdateBatch(columnInfos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generator(GenConfig genConfig, List<ColumnInfo> columns) {
|
||||
if (genConfig.getId() == null) {
|
||||
throw new BadRequestException(CONFIG_MESSAGE);
|
||||
}
|
||||
try {
|
||||
GenUtil.generatorCode(columns, genConfig);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new BadRequestException("生成失败,请手动处理已生成的文件");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> columns) {
|
||||
if (genConfig.getId() == null) {
|
||||
throw new BadRequestException(CONFIG_MESSAGE);
|
||||
}
|
||||
List<Map<String, Object>> genList = GenUtil.preview(columns, genConfig);
|
||||
return new ResponseEntity<>(genList, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response) {
|
||||
if (genConfig.getId() == null) {
|
||||
throw new BadRequestException(CONFIG_MESSAGE);
|
||||
}
|
||||
try {
|
||||
File file = new File(GenUtil.download(columns, genConfig));
|
||||
String zipPath = file.getPath() + ".zip";
|
||||
ZipUtil.zip(file.getPath(), zipPath);
|
||||
FileUtil.downloadFile(request, response, new File(zipPath), true);
|
||||
} catch (IOException e) {
|
||||
throw new BadRequestException("打包失败");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.utils;
|
||||
|
||||
import org.apache.commons.configuration.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* sql字段转java
|
||||
*
|
||||
* @author Tz
|
||||
* @date 2019-01-03
|
||||
*/
|
||||
public class ColUtil {
|
||||
private static final Logger log = LoggerFactory.getLogger(ColUtil.class);
|
||||
|
||||
/**
|
||||
* 转换mysql数据类型为java数据类型
|
||||
*
|
||||
* @param type 数据库字段类型
|
||||
* @return String
|
||||
*/
|
||||
static String cloToJava(String type) {
|
||||
Configuration config = getConfig();
|
||||
assert config != null;
|
||||
return config.getString(type, "unknowType");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*/
|
||||
public static PropertiesConfiguration getConfig() {
|
||||
try {
|
||||
return new PropertiesConfiguration("gen.properties");
|
||||
} catch (ConfigurationException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,417 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import static me.zhengjie.utils.FileUtil.SYS_TEM_DIR;
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
*
|
||||
* @author Tz
|
||||
* @date 2019-01-02
|
||||
*/
|
||||
@Slf4j
|
||||
@SuppressWarnings({"unchecked", "all"})
|
||||
public class GenUtil {
|
||||
|
||||
private static final String TIMESTAMP = "Timestamp";
|
||||
|
||||
private static final String BIGDECIMAL = "BigDecimal";
|
||||
|
||||
public static final String PK = "PRI";
|
||||
|
||||
public static final String EXTRA = "auto_increment";
|
||||
|
||||
/**
|
||||
* 获取后端代码模板名称
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
private static List<String> getAdminTemplateNames() {
|
||||
List<String> templateNames = new ArrayList<>();
|
||||
templateNames.add("Entity");
|
||||
templateNames.add("Controller");
|
||||
templateNames.add("QueryCriteria");
|
||||
templateNames.add("Service");
|
||||
templateNames.add("ServiceImpl");
|
||||
templateNames.add("Mapper");
|
||||
templateNames.add("Mapper-xml");
|
||||
return templateNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前端代码模板名称
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
private static List<String> getFrontTemplateNames() {
|
||||
List<String> templateNames = new ArrayList<>();
|
||||
templateNames.add("index");
|
||||
templateNames.add("api");
|
||||
return templateNames;
|
||||
}
|
||||
|
||||
public static List<Map<String, Object>> preview(List<ColumnInfo> columns, GenConfig genConfig) {
|
||||
Map<String, Object> genMap = getGenMap(columns, genConfig);
|
||||
List<Map<String, Object>> genList = new ArrayList<>();
|
||||
// 获取后端模版
|
||||
List<String> templates = getAdminTemplateNames();
|
||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
for (String templateName : templates) {
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
Template template = engine.getTemplate("admin/" + templateName + ".ftl");
|
||||
map.put("content", template.render(genMap));
|
||||
map.put("name", templateName.replace("-xml", ".xml"));
|
||||
genList.add(map);
|
||||
}
|
||||
// 获取前端模版
|
||||
templates = getFrontTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
Template template = engine.getTemplate("front/" + templateName + ".ftl");
|
||||
map.put(templateName, template.render(genMap));
|
||||
map.put("content", template.render(genMap));
|
||||
map.put("name", templateName);
|
||||
genList.add(map);
|
||||
}
|
||||
return genList;
|
||||
}
|
||||
|
||||
public static String download(List<ColumnInfo> columns, GenConfig genConfig) throws IOException {
|
||||
// 拼接的路径:/tmpeladmin-gen-temp/,这个路径在Linux下需要root用户才有权限创建,非root用户会权限错误而失败,更改为: /tmp/eladmin-gen-temp/
|
||||
// String tempPath =SYS_TEM_DIR + "eladmin-gen-temp" + File.separator + genConfig.getTableName() + File.separator;
|
||||
String tempPath = SYS_TEM_DIR + "eladmin-gen-temp" + File.separator + genConfig.getTableName() + File.separator;
|
||||
Map<String, Object> genMap = getGenMap(columns, genConfig);
|
||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
// 生成后端代码
|
||||
List<String> templates = getAdminTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("admin/" + templateName + ".ftl");
|
||||
String filePath = getAdminFilePath(templateName, genConfig, genMap.get("className").toString(), tempPath + "eladmin" + File.separator);
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getCover() && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
// 生成前端代码
|
||||
templates = getFrontTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("front/" + templateName + ".ftl");
|
||||
String path = tempPath + "eladmin-web" + File.separator;
|
||||
String apiPath = path + "src" + File.separator + "api" + File.separator;
|
||||
String srcPath = path + "src" + File.separator + "views" + File.separator + genMap.get("changeClassName").toString() + File.separator;
|
||||
String filePath = getFrontFilePath(templateName, apiPath, srcPath, genMap.get("changeClassName").toString());
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getCover() && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
return tempPath;
|
||||
}
|
||||
|
||||
public static void generatorCode(List<ColumnInfo> columnInfos, GenConfig genConfig) throws IOException {
|
||||
Map<String, Object> genMap = getGenMap(columnInfos, genConfig);
|
||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
// 生成后端代码
|
||||
List<String> templates = getAdminTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("admin/" + templateName + ".ftl");
|
||||
String rootPath = System.getProperty("user.dir");
|
||||
String filePath = getAdminFilePath(templateName, genConfig, genMap.get("className").toString(), rootPath);
|
||||
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getCover() && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
|
||||
// 生成前端代码
|
||||
templates = getFrontTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("front/" + templateName + ".ftl");
|
||||
String filePath = getFrontFilePath(templateName, genConfig.getApiPath(), genConfig.getPath(), genMap.get("changeClassName").toString());
|
||||
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getCover() && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取模版数据
|
||||
private static Map<String, Object> getGenMap(List<ColumnInfo> columnInfos, GenConfig genConfig) {
|
||||
// 存储模版字段数据
|
||||
Map<String, Object> genMap = new HashMap<>(16);
|
||||
// 接口别名
|
||||
genMap.put("apiAlias", genConfig.getApiAlias());
|
||||
// 包名称
|
||||
genMap.put("package", genConfig.getPack());
|
||||
// 模块名称
|
||||
genMap.put("moduleName", genConfig.getModuleName());
|
||||
// 作者
|
||||
genMap.put("author", genConfig.getAuthor());
|
||||
// 创建日期
|
||||
genMap.put("date", LocalDate.now().toString());
|
||||
// 表名
|
||||
genMap.put("tableName", genConfig.getTableName());
|
||||
// 大写开头的类名
|
||||
String className = StringUtils.toCapitalizeCamelCase(genConfig.getTableName());
|
||||
// 小写开头的类名
|
||||
String changeClassName = StringUtils.toCamelCase(genConfig.getTableName());
|
||||
// 判断是否去除表前缀
|
||||
if (StringUtils.isNotEmpty(genConfig.getPrefix())) {
|
||||
className = StringUtils.toCapitalizeCamelCase(StrUtil.removePrefix(genConfig.getTableName(), genConfig.getPrefix()));
|
||||
changeClassName = StringUtils.toCamelCase(StrUtil.removePrefix(genConfig.getTableName(), genConfig.getPrefix()));
|
||||
changeClassName = StringUtils.uncapitalize(changeClassName);
|
||||
}
|
||||
// 保存类名
|
||||
genMap.put("className", className);
|
||||
// 保存小写开头的类名
|
||||
genMap.put("changeClassName", changeClassName);
|
||||
// 存在 Timestamp 字段
|
||||
genMap.put("hasTimestamp", false);
|
||||
// 查询类中存在 Timestamp 字段
|
||||
genMap.put("queryHasTimestamp", false);
|
||||
// 存在 BigDecimal 字段
|
||||
genMap.put("hasBigDecimal", false);
|
||||
// 查询类中存在 BigDecimal 字段
|
||||
genMap.put("queryHasBigDecimal", false);
|
||||
// 是否需要创建查询
|
||||
genMap.put("hasQuery", false);
|
||||
// 自增主键
|
||||
genMap.put("auto", false);
|
||||
// 存在字典
|
||||
genMap.put("hasDict", false);
|
||||
// 存在日期注解
|
||||
genMap.put("hasDateAnnotation", false);
|
||||
// 存储主键字段名
|
||||
genMap.put("pkIdName", "none");
|
||||
// 存储符号
|
||||
genMap.put("symbol", "#");
|
||||
// 保存字段信息
|
||||
List<Map<String, Object>> columns = new ArrayList<>();
|
||||
// 保存查询字段的信息
|
||||
List<Map<String, Object>> queryColumns = new ArrayList<>();
|
||||
// 存储字典信息
|
||||
List<String> dicts = new ArrayList<>();
|
||||
// 存储 between 信息
|
||||
List<Map<String, Object>> betweens = new ArrayList<>();
|
||||
// 存储不为空的字段信息
|
||||
List<Map<String, Object>> isNotNullColumns = new ArrayList<>();
|
||||
|
||||
for (ColumnInfo column : columnInfos) {
|
||||
Map<String, Object> listMap = new HashMap<>(16);
|
||||
// 字段描述
|
||||
listMap.put("remark", column.getRemark());
|
||||
// 字段类型
|
||||
listMap.put("columnKey", column.getKeyType());
|
||||
// 主键类型
|
||||
String colType = ColUtil.cloToJava(column.getColumnType());
|
||||
// 小写开头的字段名
|
||||
String changeColumnName = StringUtils.toCamelCase(column.getColumnName());
|
||||
// 大写开头的字段名
|
||||
String capitalColumnName = StringUtils.toCapitalizeCamelCase(column.getColumnName());
|
||||
if (PK.equals(column.getKeyType())) {
|
||||
// 存储主键类型
|
||||
genMap.put("pkColumnType", colType);
|
||||
// 存储小写开头的字段名
|
||||
genMap.put("pkChangeColName", changeColumnName);
|
||||
// 存储大写开头的字段名
|
||||
genMap.put("pkCapitalColName", capitalColumnName);
|
||||
// 存储主键字段名
|
||||
genMap.put("pkIdName", column.getColumnName());
|
||||
}
|
||||
// 是否存在 Timestamp 类型的字段
|
||||
if (TIMESTAMP.equals(colType)) {
|
||||
genMap.put("hasTimestamp", true);
|
||||
}
|
||||
// 是否存在 BigDecimal 类型的字段
|
||||
if (BIGDECIMAL.equals(colType)) {
|
||||
genMap.put("hasBigDecimal", true);
|
||||
}
|
||||
// 主键是否自增
|
||||
if (EXTRA.equals(column.getExtra())) {
|
||||
genMap.put("auto", true);
|
||||
}
|
||||
// 主键存在字典
|
||||
if (StringUtils.isNotBlank(column.getDictName())) {
|
||||
genMap.put("hasDict", true);
|
||||
if(!dicts.contains(column.getDictName()))
|
||||
dicts.add(column.getDictName());
|
||||
}
|
||||
|
||||
// 存储字段类型
|
||||
listMap.put("columnType", colType);
|
||||
// 存储字原始段名称
|
||||
listMap.put("columnName", column.getColumnName());
|
||||
// 不为空
|
||||
listMap.put("istNotNull", column.getNotNull());
|
||||
// 字段列表显示
|
||||
listMap.put("columnShow", column.getListShow());
|
||||
// 表单显示
|
||||
listMap.put("formShow", column.getFormShow());
|
||||
// 表单组件类型
|
||||
listMap.put("formType", StringUtils.isNotBlank(column.getFormType()) ? column.getFormType() : "Input");
|
||||
// 小写开头的字段名称
|
||||
listMap.put("changeColumnName", changeColumnName);
|
||||
//大写开头的字段名称
|
||||
listMap.put("capitalColumnName", capitalColumnName);
|
||||
// 字典名称
|
||||
listMap.put("dictName", column.getDictName());
|
||||
// 添加非空字段信息
|
||||
if (column.getNotNull()) {
|
||||
isNotNullColumns.add(listMap);
|
||||
}
|
||||
// 判断是否有查询,如有则把查询的字段set进columnQuery
|
||||
if (!StringUtils.isBlank(column.getQueryType())) {
|
||||
// 查询类型
|
||||
listMap.put("queryType", column.getQueryType());
|
||||
// 是否存在查询
|
||||
genMap.put("hasQuery", true);
|
||||
if (TIMESTAMP.equals(colType)) {
|
||||
// 查询中存储 Timestamp 类型
|
||||
genMap.put("queryHasTimestamp", true);
|
||||
}
|
||||
if (BIGDECIMAL.equals(colType)) {
|
||||
// 查询中存储 BigDecimal 类型
|
||||
genMap.put("queryHasBigDecimal", true);
|
||||
}
|
||||
if ("between".equalsIgnoreCase(column.getQueryType())) {
|
||||
betweens.add(listMap);
|
||||
} else {
|
||||
// 添加到查询列表中
|
||||
queryColumns.add(listMap);
|
||||
}
|
||||
}
|
||||
// 添加到字段列表中
|
||||
columns.add(listMap);
|
||||
}
|
||||
// 保存字段列表
|
||||
genMap.put("columns", columns);
|
||||
// 保存查询列表
|
||||
genMap.put("queryColumns", queryColumns);
|
||||
// 保存字段列表
|
||||
genMap.put("dicts", dicts);
|
||||
// 保存查询列表
|
||||
genMap.put("betweens", betweens);
|
||||
// 保存非空字段信息
|
||||
genMap.put("isNotNullColumns", isNotNullColumns);
|
||||
return genMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义后端文件路径以及名称
|
||||
*/
|
||||
private static String getAdminFilePath(String templateName, GenConfig genConfig, String className, String rootPath) {
|
||||
String projectPath = rootPath + File.separator + genConfig.getModuleName();
|
||||
String packagePath = projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator;
|
||||
String mpXmlPath = projectPath + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator;
|
||||
if (!ObjectUtils.isEmpty(genConfig.getPack())) {
|
||||
packagePath += genConfig.getPack().replace(".", File.separator) + File.separator;
|
||||
}
|
||||
|
||||
if ("Entity".equals(templateName)) {
|
||||
return packagePath + "domain" + File.separator + className + ".java";
|
||||
}
|
||||
|
||||
if ("Controller".equals(templateName)) {
|
||||
return packagePath + "rest" + File.separator + className + "Controller.java";
|
||||
}
|
||||
|
||||
if ("Service".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + className + "Service.java";
|
||||
}
|
||||
|
||||
if ("ServiceImpl".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
|
||||
}
|
||||
|
||||
if ("QueryCriteria".equals(templateName)) {
|
||||
return packagePath + "domain" + File.separator + "dto" + File.separator + className + "QueryCriteria.java";
|
||||
}
|
||||
|
||||
if ("Mapper".equals(templateName)) {
|
||||
return packagePath + "mapper" + File.separator + className + "Mapper.java";
|
||||
}
|
||||
|
||||
if ("Mapper-xml".equals(templateName)) {
|
||||
return mpXmlPath + "mapper" + File.separator + className + "Mapper.xml";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义前端文件路径以及名称
|
||||
*/
|
||||
private static String getFrontFilePath(String templateName, String apiPath, String path, String apiName) {
|
||||
|
||||
if ("api".equals(templateName)) {
|
||||
return apiPath + File.separator + apiName + ".js";
|
||||
}
|
||||
|
||||
if ("index".equals(templateName)) {
|
||||
return path + File.separator + "index.vue";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void genFile(File file, Template template, Map<String, Object> map) throws IOException {
|
||||
// 生成目标文件
|
||||
Writer writer = null;
|
||||
try {
|
||||
FileUtil.touch(file);
|
||||
writer = new FileWriter(file);
|
||||
template.render(map, writer);
|
||||
} catch (TemplateException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
assert writer != null;
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
#数据库类型转Java类型
|
||||
tinyint=Integer
|
||||
smallint=Integer
|
||||
mediumint=Integer
|
||||
int=Integer
|
||||
integer=Integer
|
||||
|
||||
bigint=Long
|
||||
|
||||
float=Float
|
||||
|
||||
double=Double
|
||||
|
||||
decimal=BigDecimal
|
||||
|
||||
bit=Boolean
|
||||
|
||||
char=String
|
||||
varchar=String
|
||||
tinytext=String
|
||||
text=String
|
||||
mediumtext=String
|
||||
longtext=String
|
||||
|
||||
date=Timestamp
|
||||
datetime=Timestamp
|
||||
timestamp=Timestamp
|
@ -1,49 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="me.zhengjie.mapper.ColumnInfoMapper">
|
||||
<resultMap id="BaseResultMap" type="me.zhengjie.domain.ColumnInfo">
|
||||
<id column="column_id" property="id"/>
|
||||
<result column="table_name" property="tableName"/>
|
||||
<result column="column_name" property="columnName"/>
|
||||
<result column="column_type" property="columnType"/>
|
||||
<result column="key_type" property="keyType"/>
|
||||
<result column="extra" property="extra"/>
|
||||
<result column="remark" property="remark"/>
|
||||
<result column="not_null" property="notNull"/>
|
||||
<result column="list_show" property="listShow"/>
|
||||
<result column="form_show" property="formShow"/>
|
||||
<result column="form_type" property="formType"/>
|
||||
<result column="query_type" property="queryType"/>
|
||||
<result column="dict_name" property="dictName"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
column_id, table_name, column_name, column_type, key_type, extra, remark, not_null, list_show, form_show, form_type, query_type, dict_name
|
||||
</sql>
|
||||
|
||||
<select id="getTables" resultType="me.zhengjie.domain.dto.TableInfo">
|
||||
select table_name, create_time, engine, table_collation as coding, table_comment as remark
|
||||
from information_schema.tables
|
||||
where table_schema = (select database())
|
||||
and table_name like concat('%',#{tableName},'%')
|
||||
order by create_time desc
|
||||
</select>
|
||||
|
||||
<select id="findByTableNameOrderByIdAsc" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List"/>
|
||||
from code_column
|
||||
where table_name = #{tableName}
|
||||
order by column_id
|
||||
</select>
|
||||
|
||||
<select id="getColumns" resultMap="BaseResultMap">
|
||||
select column_name, if(is_nullable = 'NO', 1, 0) not_null,
|
||||
data_type as column_type, column_comment as remark,
|
||||
column_key key_type, extra
|
||||
from information_schema.columns
|
||||
where table_name = #{tableName}
|
||||
and table_schema = (select database())
|
||||
order by ordinal_position
|
||||
</select>
|
||||
</mapper>
|
@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="me.zhengjie.mapper.GenConfigMapper">
|
||||
<resultMap id="BaseResultMap" type="me.zhengjie.domain.GenConfig">
|
||||
<id column="config_id" property="id"/>
|
||||
<result column="table_name" property="tableName"/>
|
||||
<result column="api_alias" property="apiAlias"/>
|
||||
<result column="pack" property="pack"/>
|
||||
<result column="module_name" property="moduleName"/>
|
||||
<result column="path" property="path"/>
|
||||
<result column="api_path" property="apiPath"/>
|
||||
<result column="author" property="author"/>
|
||||
<result column="prefix" property="prefix"/>
|
||||
<result column="cover" property="cover"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
config_id, table_name, api_alias, pack, module_name, path, api_path, author, prefix, cover
|
||||
</sql>
|
||||
|
||||
<select id="findByTableName" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List"/>
|
||||
FROM code_config
|
||||
WHERE table_name = #{tableName}
|
||||
</select>
|
||||
</mapper>
|
@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ${package}.rest;
|
||||
|
||||
import me.zhengjie.annotation.Log;
|
||||
import ${package}.domain.${className};
|
||||
import ${package}.service.${className}Service;
|
||||
import ${package}.domain.dto.${className}QueryCriteria;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import java.util.List;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.*;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import me.zhengjie.utils.PageResult;
|
||||
|
||||
/**
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
**/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "${apiAlias}")
|
||||
@RequestMapping("/api/${changeClassName}")
|
||||
public class ${className}Controller {
|
||||
|
||||
private final ${className}Service ${changeClassName}Service;
|
||||
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
@PreAuthorize("@el.check('${changeClassName}:list')")
|
||||
public void export${className}(HttpServletResponse response, ${className}QueryCriteria criteria) throws IOException {
|
||||
${changeClassName}Service.download(${changeClassName}Service.queryAll(criteria), response);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("查询${apiAlias}")
|
||||
@PreAuthorize("@el.check('${changeClassName}:list')")
|
||||
public ResponseEntity<PageResult<${className}>> query${className}(${className}QueryCriteria criteria){
|
||||
Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
|
||||
return new ResponseEntity<>(${changeClassName}Service.queryAll(criteria,page),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Log("新增${apiAlias}")
|
||||
@ApiOperation("新增${apiAlias}")
|
||||
@PreAuthorize("@el.check('${changeClassName}:add')")
|
||||
public ResponseEntity<Object> create${className}(@Validated @RequestBody ${className} resources){
|
||||
${changeClassName}Service.create(resources);
|
||||
return new ResponseEntity<>(HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@Log("修改${apiAlias}")
|
||||
@ApiOperation("修改${apiAlias}")
|
||||
@PreAuthorize("@el.check('${changeClassName}:edit')")
|
||||
public ResponseEntity<Object> update${className}(@Validated @RequestBody ${className} resources){
|
||||
${changeClassName}Service.update(resources);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
@Log("删除${apiAlias}")
|
||||
@ApiOperation("删除${apiAlias}")
|
||||
@PreAuthorize("@el.check('${changeClassName}:del')")
|
||||
public ResponseEntity<Object> delete${className}(@ApiParam(value = "传ID数组[]") @RequestBody List<${pkColumnType}> ids) {
|
||||
${changeClassName}Service.deleteAll(ids);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ${package}.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
<#if hasTimestamp>
|
||||
import java.sql.Timestamp;
|
||||
</#if>
|
||||
<#if hasBigDecimal>
|
||||
import java.math.BigDecimal;
|
||||
</#if>
|
||||
<#assign notBlankUsed = false>
|
||||
<#assign notNullUsed = false>
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.istNotNull && column.columnKey != 'PRI'>
|
||||
<#if column.columnType = 'String'>
|
||||
<#assign notBlankUsed = true>
|
||||
<#else>
|
||||
<#assign notNullUsed = true>
|
||||
</#if>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
<#if notBlankUsed>
|
||||
import javax.validation.constraints.NotBlank;
|
||||
</#if>
|
||||
<#if notNullUsed>
|
||||
import javax.validation.constraints.NotNull;
|
||||
</#if>
|
||||
import java.io.Serializable;
|
||||
<#if auto>
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
</#if>
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
/**
|
||||
* @description /
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
**/
|
||||
@Data
|
||||
@TableName("${tableName}")
|
||||
public class ${className} implements Serializable {
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
|
||||
<#if column.columnKey = 'PRI'>
|
||||
@TableId(value = "${column.columnName}"<#if auto>, type = IdType.AUTO</#if>)
|
||||
</#if>
|
||||
<#if column.istNotNull && column.columnKey != 'PRI'>
|
||||
<#if column.columnType = 'String'>
|
||||
@NotBlank
|
||||
<#else>
|
||||
@NotNull
|
||||
</#if>
|
||||
</#if>
|
||||
<#if column.remark != ''>
|
||||
@ApiModelProperty(value = "${column.remark}")
|
||||
<#else>
|
||||
@ApiModelProperty(value = "${column.changeColumnName}")
|
||||
</#if>
|
||||
private ${column.columnType} ${column.changeColumnName};
|
||||
</#list>
|
||||
</#if>
|
||||
|
||||
public void copy(${className} source){
|
||||
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="${package}.mapper.${className}Mapper">
|
||||
<#if columns??>
|
||||
<resultMap id="BaseResultMap" type="${package}.domain.${className}">
|
||||
<#list columns as column>
|
||||
<#if column.columnKey = 'PRI'>
|
||||
<id column="${column.columnName}" property="${column.changeColumnName}"/>
|
||||
</#if>
|
||||
<#if column.columnKey != 'PRI'>
|
||||
<result column="${column.columnName}" property="${column.changeColumnName}"/>
|
||||
</#if>
|
||||
</#list>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
<#list columns as column>${column.columnName}<#if column_has_next>, </#if></#list>
|
||||
</sql>
|
||||
</#if>
|
||||
|
||||
<select id="findAll" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List"/>
|
||||
from ${tableName}
|
||||
<#if queryColumns??>
|
||||
<where>
|
||||
<#list queryColumns as column>
|
||||
<if test="criteria.${column.changeColumnName} != null">
|
||||
<#if column.queryType = '='>
|
||||
and ${column.columnName} = ${symbol}{criteria.${column.changeColumnName}}
|
||||
</#if>
|
||||
<#if column.queryType = 'Like'>
|
||||
and ${column.columnName} like concat('%',${symbol}{criteria.${column.changeColumnName}},'%')
|
||||
</#if>
|
||||
<#if column.queryType = '!='>
|
||||
and ${column.columnName} != ${symbol}{criteria.${column.changeColumnName}}
|
||||
</#if>
|
||||
<#if column.queryType = 'NotNull'>
|
||||
and ${column.columnName} is not null
|
||||
</#if>
|
||||
<#if column.queryType = '>='>
|
||||
and ${column.columnName} >= ${symbol}{criteria.${column.changeColumnName}}
|
||||
</#if>
|
||||
<#if column.queryType = '<='>
|
||||
and ${column.columnName} <= ${symbol}{criteria.${column.changeColumnName}}
|
||||
</#if>
|
||||
</if>
|
||||
</#list>
|
||||
<#if betweens??>
|
||||
<#list betweens as column>
|
||||
<if test="criteria.${column.changeColumnName} != null and criteria.${column.changeColumnName}.size() > 0">
|
||||
AND ${column.columnName} BETWEEN ${symbol}{criteria.${column.changeColumnName}[0]} AND ${symbol}{criteria.${column.changeColumnName}[1]}
|
||||
</if>
|
||||
</#list>
|
||||
</#if>
|
||||
</where>
|
||||
</#if>
|
||||
<#if pkIdName != 'none'>
|
||||
order by ${pkIdName} desc
|
||||
</#if>
|
||||
</select>
|
||||
</mapper>
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ${package}.mapper;
|
||||
|
||||
import ${package}.domain.${className};
|
||||
import ${package}.domain.dto.${className}QueryCriteria;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
/**
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
**/
|
||||
@Mapper
|
||||
public interface ${className}Mapper extends BaseMapper<${className}> {
|
||||
|
||||
IPage<${className}> findAll(@Param("criteria") ${className}QueryCriteria criteria, Page<Object> page);
|
||||
|
||||
List<${className}> findAll(@Param("criteria") ${className}QueryCriteria criteria);
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ${package}.domain.dto;
|
||||
|
||||
import lombok.Data;
|
||||
<#if queryHasTimestamp>
|
||||
import java.sql.Timestamp;
|
||||
</#if>
|
||||
<#if queryHasBigDecimal>
|
||||
import java.math.BigDecimal;
|
||||
</#if>
|
||||
<#if betweens?? && (betweens?size > 0)>
|
||||
import java.util.List;
|
||||
</#if>
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
/**
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
**/
|
||||
@Data
|
||||
public class ${className}QueryCriteria{
|
||||
|
||||
@ApiModelProperty(value = "页码", example = "1")
|
||||
private Integer page = 1;
|
||||
|
||||
@ApiModelProperty(value = "每页数据量", example = "10")
|
||||
private Integer size = 10;
|
||||
<#if queryColumns??>
|
||||
<#list queryColumns as column>
|
||||
|
||||
<#if column.remark != ''>
|
||||
@ApiModelProperty(value = "${column.remark}")
|
||||
<#else>
|
||||
@ApiModelProperty(value = "${column.changeColumnName}")
|
||||
</#if>
|
||||
private ${column.columnType} ${column.changeColumnName};
|
||||
</#list>
|
||||
</#if>
|
||||
<#if betweens??>
|
||||
<#list betweens as column>
|
||||
private List<${column.columnType}> ${column.changeColumnName};
|
||||
</#list>
|
||||
</#if>
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ${package}.service;
|
||||
|
||||
import ${package}.domain.${className};
|
||||
import ${package}.domain.dto.${className}QueryCriteria;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import me.zhengjie.utils.PageResult;
|
||||
|
||||
/**
|
||||
* @description 服务接口
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
**/
|
||||
public interface ${className}Service extends IService<${className}> {
|
||||
|
||||
/**
|
||||
* 查询数据分页
|
||||
* @param criteria 条件
|
||||
* @param page 分页参数
|
||||
* @return PageResult
|
||||
*/
|
||||
PageResult<${className}> queryAll(${className}QueryCriteria criteria, Page<Object> page);
|
||||
|
||||
/**
|
||||
* 查询所有数据不分页
|
||||
* @param criteria 条件参数
|
||||
* @return List<${className}Dto>
|
||||
*/
|
||||
List<${className}> queryAll(${className}QueryCriteria criteria);
|
||||
|
||||
/**
|
||||
* 创建
|
||||
* @param resources /
|
||||
*/
|
||||
void create(${className} resources);
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
* @param resources /
|
||||
*/
|
||||
void update(${className} resources);
|
||||
|
||||
/**
|
||||
* 多选删除
|
||||
* @param ids /
|
||||
*/
|
||||
void deleteAll(List<${pkColumnType}> ids);
|
||||
|
||||
/**
|
||||
* 导出数据
|
||||
* @param all 待导出的数据
|
||||
* @param response /
|
||||
* @throws IOException /
|
||||
*/
|
||||
void download(List<${className}> all, HttpServletResponse response) throws IOException;
|
||||
}
|
@ -1,105 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Tz
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ${package}.service.impl;
|
||||
|
||||
import ${package}.domain.${className};
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.columnKey = 'UNI'>
|
||||
<#if column_index = 1>
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
</#if>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
import me.zhengjie.utils.FileUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import ${package}.service.${className}Service;
|
||||
import ${package}.domain.dto.${className}QueryCriteria;
|
||||
import ${package}.mapper.${className}Mapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import me.zhengjie.utils.PageResult;
|
||||
|
||||
/**
|
||||
* @description 服务实现
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
**/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {
|
||||
|
||||
private final ${className}Mapper ${changeClassName}Mapper;
|
||||
|
||||
@Override
|
||||
public PageResult<${className}> queryAll(${className}QueryCriteria criteria, Page<Object> page){
|
||||
return PageUtil.toPage(${changeClassName}Mapper.findAll(criteria, page));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<${className}> queryAll(${className}QueryCriteria criteria){
|
||||
return ${changeClassName}Mapper.findAll(criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void create(${className} resources) {
|
||||
${changeClassName}Mapper.insert(resources);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(${className} resources) {
|
||||
${className} ${changeClassName} = getById(resources.get${pkCapitalColName}());
|
||||
${changeClassName}.copy(resources);
|
||||
${changeClassName}Mapper.updateById(${changeClassName});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteAll(List<${pkColumnType}> ids) {
|
||||
${changeClassName}Mapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(List<${className}> all, HttpServletResponse response) throws IOException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (${className} ${changeClassName} : all) {
|
||||
Map<String,Object> map = new LinkedHashMap<>();
|
||||
<#list columns as column>
|
||||
<#if column.columnKey != 'PRI'>
|
||||
<#if column.remark != ''>
|
||||
map.put("${column.remark}", ${changeClassName}.get${column.capitalColumnName}());
|
||||
<#else>
|
||||
map.put(" ${column.changeColumnName}", ${changeClassName}.get${column.capitalColumnName}());
|
||||
</#if>
|
||||
</#if>
|
||||
</#list>
|
||||
list.add(map);
|
||||
}
|
||||
FileUtil.downloadExcel(list, response);
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
import request from '@/utils/request'
|
||||
|
||||
export function add(data) {
|
||||
return request({
|
||||
url: 'api/${changeClassName}',
|
||||
method: 'post',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
export function del(ids) {
|
||||
return request({
|
||||
url: 'api/${changeClassName}/',
|
||||
method: 'delete',
|
||||
data: ids
|
||||
})
|
||||
}
|
||||
|
||||
export function edit(data) {
|
||||
return request({
|
||||
url: 'api/${changeClassName}',
|
||||
method: 'put',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
export default { add, edit, del }
|
@ -1,169 +0,0 @@
|
||||
<#--noinspection ALL-->
|
||||
<template>
|
||||
<div class="app-container">
|
||||
<!--工具栏-->
|
||||
<div class="head-container">
|
||||
<#if hasQuery>
|
||||
<div v-if="crud.props.searchToggle">
|
||||
<!-- 搜索 -->
|
||||
<#if queryColumns??>
|
||||
<#list queryColumns as column>
|
||||
<#if column.queryType != 'BetWeen'>
|
||||
<label class="el-form-item-label"><#if column.remark != ''>${column.remark}<#else>${column.changeColumnName}</#if></label>
|
||||
<el-input v-model="query.${column.changeColumnName}" clearable placeholder="<#if column.remark != ''>${column.remark}<#else>${column.changeColumnName}</#if>" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
<#if betweens??>
|
||||
<#list betweens as column>
|
||||
<#if column.queryType = 'BetWeen'>
|
||||
<date-range-picker
|
||||
v-model="query.${column.changeColumnName}"
|
||||
start-placeholder="${column.changeColumnName}Start"
|
||||
end-placeholder="${column.changeColumnName}Start"
|
||||
class="date-item"
|
||||
/>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
<rrOperation :crud="crud" />
|
||||
</div>
|
||||
</#if>
|
||||
<!--如果想在工具栏加入更多按钮,可以使用插槽方式, slot = 'left' or 'right'-->
|
||||
<crudOperation :permission="permission" />
|
||||
<!--表单组件-->
|
||||
<el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
|
||||
<el-form ref="form" :model="form" <#if isNotNullColumns??>:rules="rules"</#if> size="small" label-width="80px">
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.formShow>
|
||||
<el-form-item label="<#if column.remark != ''>${column.remark}<#else>${column.changeColumnName}</#if>"<#if column.istNotNull> prop="${column.changeColumnName}"</#if>>
|
||||
<#if column.formType = 'Input'>
|
||||
<el-input v-model="form.${column.changeColumnName}" style="width: 370px;" />
|
||||
<#elseif column.formType = 'Textarea'>
|
||||
<el-input v-model="form.${column.changeColumnName}" :rows="3" type="textarea" style="width: 370px;" />
|
||||
<#elseif column.formType = 'Radio'>
|
||||
<#if (column.dictName)?? && (column.dictName)!="">
|
||||
<el-radio v-model="form.${column.changeColumnName}" v-for="item in dict.${column.dictName}" :key="item.id" :label="item.value">{{ item.label }}</el-radio>
|
||||
<#else>
|
||||
未设置字典,请手动设置 Radio
|
||||
</#if>
|
||||
<#elseif column.formType = 'Select'>
|
||||
<#if (column.dictName)?? && (column.dictName)!="">
|
||||
<el-select v-model="form.${column.changeColumnName}" filterable placeholder="请选择">
|
||||
<el-option
|
||||
v-for="item in dict.${column.dictName}"
|
||||
:key="item.id"
|
||||
:label="item.label"
|
||||
:value="item.value" />
|
||||
</el-select>
|
||||
<#else>
|
||||
未设置字典,请手动设置 Select
|
||||
</#if>
|
||||
<#else>
|
||||
<el-date-picker v-model="form.${column.changeColumnName}" type="datetime" style="width: 370px;" />
|
||||
</#if>
|
||||
</el-form-item>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
</el-form>
|
||||
<div slot="footer" class="dialog-footer">
|
||||
<el-button type="text" @click="crud.cancelCU">取消</el-button>
|
||||
<el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
<!--表格渲染-->
|
||||
<el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
|
||||
<el-table-column type="selection" width="55" />
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.columnShow>
|
||||
<#if (column.dictName)?? && (column.dictName)!="">
|
||||
<el-table-column prop="${column.changeColumnName}" label="<#if column.remark != ''>${column.remark}<#else>${column.changeColumnName}</#if>">
|
||||
<template slot-scope="scope">
|
||||
{{ dict.label.${column.dictName}[scope.row.${column.changeColumnName}] }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<#else>
|
||||
<el-table-column prop="${column.changeColumnName}" label="<#if column.remark != ''>${column.remark}<#else>${column.changeColumnName}</#if>" />
|
||||
</#if>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
<el-table-column v-if="checkPer(['admin','${changeClassName}:edit','${changeClassName}:del'])" label="操作" width="150px" align="center">
|
||||
<template slot-scope="scope">
|
||||
<udOperation
|
||||
:data="scope.row"
|
||||
:permission="permission"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
<!--分页组件-->
|
||||
<pagination />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import crud${className} from '@/api/${changeClassName}'
|
||||
import CRUD, { presenter, header, form, crud } from '@crud/crud'
|
||||
import rrOperation from '@crud/RR.operation'
|
||||
import crudOperation from '@crud/CRUD.operation'
|
||||
import udOperation from '@crud/UD.operation'
|
||||
import pagination from '@crud/Pagination'
|
||||
|
||||
const defaultForm = { <#if columns??><#list columns as column>${column.changeColumnName}: null<#if column_has_next>, </#if></#list></#if> }
|
||||
export default {
|
||||
name: '${className}',
|
||||
components: { pagination, crudOperation, rrOperation, udOperation },
|
||||
mixins: [presenter(), header(), form(defaultForm), crud()],
|
||||
<#if hasDict>
|
||||
dicts: [<#if hasDict??><#list dicts as dict>'${dict}'<#if dict_has_next>, </#if></#list></#if>],
|
||||
</#if>
|
||||
cruds() {
|
||||
return CRUD({ title: '${apiAlias}', url: 'api/${changeClassName}', idField: '${pkChangeColName}', sort: '${pkChangeColName},desc', crudMethod: { ...crud${className} }})
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
permission: {
|
||||
add: ['admin', '${changeClassName}:add'],
|
||||
edit: ['admin', '${changeClassName}:edit'],
|
||||
del: ['admin', '${changeClassName}:del']
|
||||
},
|
||||
rules: {
|
||||
<#if isNotNullColumns??>
|
||||
<#list isNotNullColumns as column>
|
||||
<#if column.istNotNull>
|
||||
${column.changeColumnName}: [
|
||||
{ required: true, message: '<#if column.remark != ''>${column.remark}</#if>不能为空', trigger: 'blur' }
|
||||
]<#if column_has_next>,</#if>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
}<#if hasQuery>,
|
||||
queryTypeOptions: [
|
||||
<#if queryColumns??>
|
||||
<#list queryColumns as column>
|
||||
<#if column.queryType != 'BetWeen'>
|
||||
{ key: '${column.changeColumnName}', display_name: '<#if column.remark != ''>${column.remark}<#else>${column.changeColumnName}</#if>' }<#if column_has_next>,</#if>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
]
|
||||
</#if>
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 钩子:在获取表格数据之前执行,false 则代表不获取数据
|
||||
[CRUD.HOOK.beforeRefresh]() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user