Skip to content

Commit 8115024

Browse files
committed
first commit
0 parents  commit 8115024

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3498
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 80,
3+
"useTabs": false,
4+
"semi": true,
5+
"singleQuote": true,
6+
"tabWidth": 2
7+
}

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# mybatis-cli
2+
3+
## mybatis-cli 安装
4+
5+
```
6+
npm i mybatis-cli -g
7+
```
8+
9+
10+
11+
## mybatis-cli 使用
12+
13+
1、mybatis-cli 初始化配置。
14+
15+
```
16+
# 新建并进入 mybatis-cli-test 文件夹
17+
mkdir mybatis-cli-test && cd mybatis-cli-test
18+
19+
# mybatis-cli 初始化
20+
mybatis-cli i
21+
```
22+
23+
![1-init](src/static/1-init.png)
24+
25+
2、配置数据库。
26+
27+
![1-init](src/static/2-init.png)
28+
29+
3、生成Entity、Mappper、xml。
30+
31+
```
32+
mybatis-cli g
33+
```
34+
35+
![1-init](src/static/3-init.png)
36+
37+
4、Entity、Mappper、xml新增部分写在下面 "The above part of the comment..." 下面。
38+
39+
![1-init](src/static/4-init.png)
40+
41+
5、如果数据库字段有更新,在相应文件夹下再次执行 "mybatis-cli g" 命令。
42+

bin/index.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env node
2+
3+
const yargs = require("yargs");
4+
const figlet = require("figlet");
5+
const fs = require("fs");
6+
const util = require("util");
7+
const child_process = require("child_process");
8+
const exec = util.promisify(child_process.exec);
9+
const fsExtra = require('fs-extra');
10+
const path = require("path");
11+
const srcPath = path.join(process.cwd(), "src");
12+
if (!fs.existsSync(srcPath)){fs.mkdirSync(srcPath);}
13+
14+
let pathObj = {};
15+
// 展示log
16+
let showLog = false;
17+
18+
console.log(figlet.textSync("mybatis-cli", {
19+
horizontalLayout: "full"
20+
}));
21+
22+
/**
23+
* 拷贝文件夹下的文件
24+
* @param filePath 文件路径
25+
*/
26+
function copyDirFile(filePath) {
27+
//根据文件路径读取文件,返回文件列表
28+
let files = fs.readdirSync(filePath).filter(item => item !== '.DS_Store');
29+
//遍历读取到的文件列表
30+
for (const fileName of files) {
31+
//获取当前文件的绝对路径
32+
const filedir = path.join(filePath, fileName);
33+
//根据文件路径获取文件信息,返回一个fs.Stats对象
34+
const stats = fs.statSync(filedir);
35+
const isFile = stats.isFile(); //是文件
36+
const isDir = stats.isDirectory(); //是文件夹
37+
if (isFile) {
38+
const _dirName = path.dirname(filedir);
39+
const _fileName = path.basename(filedir);
40+
const fileNameArray = _fileName.split(".");
41+
const filePrefix = fileNameArray[0];
42+
const fileSuffix = `.${fileNameArray[1]}`;
43+
// 排除掉 .DS_Store 这个文件
44+
if (fileSuffix !== ".DS_Store") {
45+
const destPath = `${_dirName}/${filePrefix}Copy${fileSuffix}`;
46+
fs.copyFile(filedir, destPath, (err) => {});
47+
pathObj[filedir] = destPath;
48+
}
49+
}
50+
if (isDir) {
51+
copyDirFile(filedir); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
52+
}
53+
}
54+
}
55+
56+
/**
57+
* 更新文件夹下的文件
58+
* @param filePath 文件路径
59+
*/
60+
function updateDirFile(filePath) {
61+
showLog && console.log('updateDirFile')
62+
//根据文件路径读取文件,返回文件列表
63+
const files = fs.readdirSync(filePath).filter(item => item !== '.DS_Store');
64+
//遍历读取到的文件列表
65+
for (const fileName of files) {
66+
//获取当前文件的绝对路径
67+
const filedir = path.join(filePath, fileName);
68+
//根据文件路径获取文件信息,返回一个fs.Stats对象
69+
const stats = fs.statSync(filedir);
70+
const isFile = stats.isFile(); //是文件
71+
const isDir = stats.isDirectory(); //是文件夹
72+
if (isFile) {
73+
_fileName = path.basename(filedir);
74+
const fileNameArray = _fileName.split(".");
75+
fileSuffix = `.${fileNameArray[1]}`;
76+
let splitStr;
77+
if (fileSuffix === ".java") {
78+
splitStr = "/** The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. */";
79+
} else if (fileSuffix === ".xml") {
80+
splitStr = "<!-- The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. -->";
81+
} else {
82+
splitStr = "The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment.";
83+
}
84+
if (pathObj[filedir] && fs.existsSync(pathObj[filedir])) {
85+
let data1 = fs.readFileSync(filedir, "utf-8");
86+
let str1 = data1.toString();
87+
const _data1 = str1.split(splitStr);
88+
updateFileStr = _data1[0];
89+
90+
let data2 = fs.readFileSync(pathObj[filedir], "utf-8");
91+
let str2 = data2.toString();
92+
const _data2 = str2.split(splitStr);
93+
copyFileStr = _data2[1];
94+
resultStr = updateFileStr + splitStr + copyFileStr;
95+
// 写入文件
96+
fs.writeFileSync(filedir, resultStr, function (err) {
97+
if (err) return console.log(err);
98+
});
99+
}
100+
}
101+
if (isDir) {
102+
updateDirFile(filedir); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
103+
}
104+
}
105+
}
106+
107+
/**
108+
* 删除文件
109+
* @param delPath 文件路径
110+
*/
111+
function deleteFile(delPath) {
112+
try {
113+
// 判断文件或文件夹是否存在
114+
if (fs.existsSync(delPath)) {
115+
fs.unlinkSync(delPath);
116+
} else {
117+
console.log("inexistence path:", delPath);
118+
}
119+
} catch (error) {
120+
console.log("del error", error);
121+
}
122+
}
123+
124+
/**
125+
* 删除文件夹下的文件
126+
*/
127+
function deleteDirFile() {
128+
for (const filePath in pathObj) {
129+
//根据文件路径获取文件信息,返回一个fs.Stats对象
130+
const stats = fs.statSync(filePath);
131+
const isFile = stats.isFile(); //是文件
132+
if (isFile && pathObj[filePath] && fs.existsSync(pathObj[filePath])) {
133+
deleteFile(pathObj[filePath]);
134+
}
135+
}
136+
}
137+
138+
139+
/**
140+
* init 初始化
141+
*/
142+
async function init() {
143+
try {
144+
const globalNodeModulesPathObj = await exec('npm root -g') || {};
145+
const globalNodeModulesPath = globalNodeModulesPathObj.stdout.replace('\n', '');
146+
const globalJasmineTemplatePath = path.join(globalNodeModulesPath, 'mybatis-cli/src/template');
147+
// Async with promises:
148+
fsExtra.copy(globalJasmineTemplatePath, `${process.cwd()}`)
149+
.then(() => console.log('mybatis-cli init success'))
150+
.catch((err) => console.error(err));
151+
} catch (error) {
152+
console.log("mybatis-cli init error: " + error);
153+
}
154+
}
155+
156+
/**
157+
* generate 生成mybatis entity、mapper、xml
158+
*/
159+
async function generate() {
160+
try {
161+
const globalNodeModulesPathObj = await exec('npm root -g') || {};
162+
const globalNodeModulesPath = globalNodeModulesPathObj.stdout.replace('\n', '');
163+
const globalJasmineBinPath = path.join(globalNodeModulesPath, 'mybatis-cli/src/jasmine-bin/bin/jasmine');
164+
// 创建文件副本
165+
copyDirFile(srcPath);
166+
// 生成mybatis相关文件
167+
await exec(`${globalJasmineBinPath} ${process.cwd()}/jasmine.properties`);
168+
// 更新文件
169+
updateDirFile(srcPath);
170+
// 删除文件副本
171+
deleteDirFile();
172+
console.log('mybatis-cli generate success');
173+
} catch (error) {
174+
console.log("mybatis-cli generate error: " + error);
175+
}
176+
}
177+
178+
// 输出支持的命令
179+
yargs
180+
.scriptName('mybatis-cli')
181+
.usage('$0 <cmd> [args]')
182+
.command(
183+
'i',
184+
'初始化生成mybatis-cli配置文件',
185+
(yargs) => {},
186+
function (argv) {
187+
init();
188+
}
189+
)
190+
.command(
191+
'g',
192+
'生成mybatis',
193+
(yargs) => {},
194+
function (argv) {
195+
generate();
196+
}
197+
)
198+
.help().argv;

demo/mybatis-cli-demo/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
# log
23+
*.log
24+
*.gz
25+
26+
### NetBeans ###
27+
/nbproject/private/
28+
/nbbuild/
29+
/dist/
30+
/nbdist/
31+
/.nb-gradle/
32+
build/
33+
!**/src/main/**/build/
34+
!**/src/test/**/build/
35+
36+
### VS Code ###
37+
.vscode/
38+
.DS_Store

demo/mybatis-cli-demo/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# mybatis-cli-demo
2+
3+
# 使用
4+
1、新建mysql数据 mybatis-cli
5+
2、通过 mybatis-cli.sql 创建表。
6+
3、在 mp-api/src/main/resources/application.yml 修改相关数据库配置。
7+
```
8+
server:
9+
port: 8083
10+
11+
spring:
12+
mvc:
13+
static-path-pattern: /**
14+
datasource:
15+
driver-class-name: com.mysql.cj.jdbc.Driver
16+
url: jdbc:mysql://127.0.0.1:3306/mybatis-cli?serverTimezone=Asia/Shanghai&characterEncoding=utf8
17+
username: xxx
18+
password: xxx
19+
```
20+
4、启动应用
21+
打开 mp-api/src/main/java/com/lucaswangdev/MpApiApplication.java 文件,启动应用。
22+
23+
5、打包应用
24+
打包:在maven设置,mybatis-cli-demo => Lifecycle => install
25+
26+
参考:
27+
Maven打包报错:jar:1.0-SNAPSHOT is missing, no dependency information available
28+
https://blog.csdn.net/weixin_50707679/article/details/116609127
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>mybatis-cli-demo</artifactId>
7+
<groupId>com.lucaswangdev</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>mp-api</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<dependencies>
16+
<!--spring-boot-starter-web-->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<!--spring-boot-starter-test-->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
</dependency>
27+
28+
<!--依赖dao模块-->
29+
<dependency>
30+
<groupId>com.lucaswangdev</groupId>
31+
<artifactId>mp-dao</artifactId>
32+
<version>1.0-SNAPSHOT</version>
33+
</dependency>
34+
35+
<!-- pagehelper 分页插件 -->
36+
<dependency>
37+
<groupId>com.github.pagehelper</groupId>
38+
<artifactId>pagehelper-spring-boot-starter</artifactId>
39+
<version>1.2.12</version>
40+
</dependency>
41+
42+
<!--log插件-->
43+
<dependency>
44+
<groupId>org.slf4j</groupId>
45+
<artifactId>slf4j-api</artifactId>
46+
</dependency>
47+
48+
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
49+
<dependency>
50+
<groupId>com.alibaba.fastjson2</groupId>
51+
<artifactId>fastjson2</artifactId>
52+
<version>2.0.19</version>
53+
</dependency>
54+
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<!--解决SpringBoot打包成jar后运行提示没有主清单属性-->
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
<configuration>
64+
<fork>true</fork>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>

0 commit comments

Comments
 (0)