Skip to content

Commit 24e6cf0

Browse files
committed
feat: Optimize update commands and update VM templates
1 parent 5a7c11a commit 24e6cf0

File tree

10 files changed

+288
-264
lines changed

10 files changed

+288
-264
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ mybatis-cli g
6060
![1-init](src/static/4-init.png)
6161

6262
### 5. If the database fields are updated, execute the "mybatis-cli u" command again in the corresponding folder.
63-
63+
```
64+
mybatis-cli u
65+
```

README_CN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ mybatis-cli g
5555
![1-init](src/static/4-init.png)
5656

5757
### 5、如果数据库字段有更新,在相应文件夹下再次执行 "mybatis-cli u" 命令。
58-
58+
```
59+
mybatis-cli u
60+
```

bin/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,28 @@ function copyDirFile(filePath) {
4040
const fileNameArray = _fileName.split(".");
4141
const filePrefix = fileNameArray[0];
4242
const fileSuffix = `.${fileNameArray[1]}`;
43+
let splitStr;
4344
// 排除掉 .DS_Store 这个文件
4445
if (fileSuffix !== ".DS_Store") {
45-
const destPath = `${_dirName}/${filePrefix}Copy${fileSuffix}`;
46-
fs.copyFile(filedir, destPath, (err) => {});
47-
pathObj[filedir] = destPath;
46+
if (fileSuffix === ".java") {
47+
splitStr = "/** The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. */";
48+
const data1 = fs.readFileSync(filedir, "utf-8");
49+
const str1 = data1.toString();
50+
if (str1.includes(splitStr)){
51+
const destPath = `${_dirName}/${filePrefix}Copy${fileSuffix}`;
52+
fs.copyFile(filedir, destPath, (err) => {});
53+
pathObj[filedir] = destPath;
54+
}
55+
} else if (fileSuffix === ".xml") {
56+
splitStr = "<!-- The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. -->";
57+
const data1 = fs.readFileSync(filedir, "utf-8");
58+
const str1 = data1.toString();
59+
if (str1.includes(splitStr)){
60+
const destPath = `${_dirName}/${filePrefix}Copy${fileSuffix}`;
61+
fs.copyFile(filedir, destPath, (err) => {});
62+
pathObj[filedir] = destPath;
63+
}
64+
}
4865
}
4966
}
5067
if (isDir) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mybatis-cli",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "mybatis entity、mapper、xml generate tool",
55
"main": "bin/index.js",
66
"files": [

src/template/jasmine/tpl/Mapper.tpl.vm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#set($repoModel = {"table" : $table, "entityPkg":$model.entityPkg, "repoPkg": $model.repoPkg})
44

5-
65
#foreach($column in $table.columns)##
76
#if($column.name == 'is_deleted')
87
#set($repoModel.deleteColumn = $column)
@@ -12,5 +11,5 @@
1211
#end
1312
#end
1413

15-
$processor.generate("${model.tplPath}/Mapper.vm", "${model.targetPath}/${table.className}Mapper.xml", true, $repoModel)
14+
$processor.generate("${model.tplPath}/Mapper.vm", "${model.targetPath}/${table.className}Mapper.java", true, $repoModel)
1615
#end

src/template/jasmine/tpl/Mapper.vm

Lines changed: 64 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,82 @@
1-
#set ($entityType = "${model.entityPkg}.${model.table.className}")
21
#macro(name $rawName)
32
${rawName.substring(0,1).toLowerCase()}${rawName.substring(1)}##
43
#end
5-
<?xml version="1.0" encoding="UTF-8" ?>
6-
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
7-
<mapper namespace="${model.repoPkg}.${model.table.className}Mapper">
8-
<sql id="TableName">
9-
${model.table.name}
10-
</sql>
11-
12-
<sql id="BaseColumnList">
13-
#foreach($column in $model.table.columns)##
14-
#if($foreach.first) #end
15-
`${column.name}`##
16-
#if($foreach.hasNext ), #end##
17-
#end
4+
package $model.repoPkg;
185

19-
</sql>
6+
import ${model.entityPkg}.${model.table.className};
7+
import org.apache.ibatis.annotations.Param;
8+
import java.util.List;
209

21-
<resultMap id="BaseResultMap" type="${entityType}" autoMapping="false">
22-
#foreach($column in $model.table.columns)
23-
<result column="$column.name" property="$column.fieldName" jdbcType="$column.jdbcType"/>
24-
#end
25-
</resultMap>
26-
27-
<insert id="insert"#if($model.table.pks.size()) useGeneratedKeys="true" keyProperty="#name(${model.table.className}).${model.table.pks.get(0).fieldName}"#end>
28-
INSERT INTO
29-
<include refid="TableName"/>
30-
<trim prefix="(" suffix=")" suffixOverrides=",">
31-
#foreach($column in $model.table.columns)
32-
`${column.name}`,
10+
/**
11+
* ${model.table.className}Mapper
12+
*/
13+
public interface ${model.table.className}Mapper{
14+
/**
15+
*##
16+
#if($model.deleteColumn)
17+
logical delete
18+
#else
19+
physical delete
3320
#end
21+
* @param id
22+
* @return
23+
*/
24+
int deleteByPrimaryKey(#foreach($column in $model.table.pks)##
25+
@Param("${column.fieldName}") ${column.javaType} ${column.fieldName}##
26+
#if($foreach.hasNext ), #end##
27+
#end);
3428

35-
</trim>
36-
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
37-
#foreach($column in $model.table.columns)##
38-
#if($column.name != 'create_time' && $column.name != 'update_time')
39-
#{#name(${model.table.className}).${column.fieldName},jdbcType=${column.jdbcType}},
40-
#end
41-
#if($column.name == 'create_time' || $column.name == 'update_time')
42-
now(),
43-
#end
44-
#end
4529

46-
</trim>
47-
</insert>
48-
49-
<insert id="insertSelective"#if($model.table.pks.size()) useGeneratedKeys="true" keyProperty="#name(${model.table.className}).${model.table.pks.get(0).fieldName}"#end>
50-
INSERT INTO
51-
<include refid="TableName"/>
52-
<trim prefix="(" suffix=")" suffixOverrides=",">
53-
#foreach($column in $model.table.columns)##
54-
#if($column.name != 'create_time' && $column.name != 'update_time')
55-
<if test="#name(${model.table.className}).${column.fieldName} != null">
56-
`${column.name}`,
57-
</if>
58-
#end
59-
#if($column.name == 'create_time' || $column.name == 'update_time')
60-
`${column.name}`,
61-
#end
62-
#end
30+
${model.table.className} selectByPrimaryKey(#foreach($column in $model.table.pks)##
31+
@Param("${column.fieldName}") ${column.javaType} ${column.fieldName}##
32+
#if($foreach.hasNext ), #end##
33+
#end);
6334

64-
</trim>
65-
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
66-
#foreach($column in $model.table.columns)##
67-
#if($column.name != 'create_time' && $column.name != 'update_time')
68-
<if test="#name(${model.table.className}).${column.fieldName} != null">
69-
#{#name(${model.table.className}).${column.fieldName},jdbcType=${column.jdbcType}},
70-
</if>
71-
#end
72-
#if($column.name == 'create_time' || $column.name == 'update_time')
73-
now(),
74-
#end
75-
#end
7635

77-
</trim>
78-
</insert>
79-
80-
<select id="selectByPrimaryKey" resultMap="BaseResultMap">
81-
select
82-
<include refid="BaseColumnList"/>
83-
from
84-
<include refid="TableName"/>
85-
where
86-
#foreach($column in $model.table.pks)
87-
`${column.name}` = #{${column.fieldName}}
88-
#if($foreach.hasNext)
89-
and
90-
#end
91-
#end
92-
</select>
36+
/**
37+
* insert entity
38+
* @param #name(${model.table.className})
9339

40+
* @return
41+
*/
42+
int insert(@Param("#name(${model.table.className})") ${model.table.className} #name(${model.table.className}));
9443

95-
#if($model.deleteColumn)
96-
<update id="deleteByPrimaryKey">
97-
update
98-
<include refid="TableName"/>
99-
set ${model.deleteColumn.name} = 1
100-
where
101-
#foreach($column in $model.table.pks)
102-
`${column.name}` = #{${column.fieldName}}
103-
#if($foreach.hasNext)
104-
and
105-
#end
106-
#end
107-
</update>
108-
#else
109-
<delete id="deleteByPrimaryKey">
110-
DELETE from
111-
<include refid="TableName"/>
112-
where
113-
#foreach($column in $model.table.pks)
114-
`${column.name}` = #{${column.fieldName}}
115-
#if($foreach.hasNext)
116-
and
117-
#end
118-
#end
119-
</delete>
120-
#end
44+
/**
45+
* insert entity selective.
46+
* @param #name(${model.table.className})
12147

122-
<update id="updateByPrimaryKey">
123-
update
124-
<include refid="TableName"/>
125-
set
126-
<trim suffixOverrides=",">
127-
#foreach($column in $model.table.columns)##
128-
#if(!$column.PK && $column.name != 'create_time' && $column.name != 'update_time' && $column.name != $model.versionColumn.name)
129-
`${column.name}` = #{#name(${model.table.className}).${column.fieldName},jdbcType=${column.jdbcType}},
130-
#end##
131-
#if($column.name == 'update_time')
132-
`${column.name}` = now(),
133-
#end
134-
#end
135-
#if($model.versionColumn)
136-
`${model.versionColumn.name}` = `${model.versionColumn.name}` + 1,
137-
#end
138-
</trim>
139-
where
140-
#foreach($column in $model.table.pks)
141-
`${column.name}` = #{#name(${model.table.className}).${column.fieldName}}
142-
#if($foreach.hasNext)
143-
and
144-
#end
145-
#end
146-
#if($model.versionColumn)
147-
and `${model.versionColumn.name}` = #{#name(${model.table.className}).${model.versionColumn.fieldName},jdbcType=${model.versionColumn.jdbcType}}
148-
#end
149-
</update>
150-
151-
<update id="updateByPrimaryKeySelective">
152-
update
153-
<include refid="TableName"/>
154-
set
155-
<trim suffixOverrides=",">
156-
#foreach($column in $model.table.columns)##
157-
#if(!$column.PK && $column.name != 'create_time' && $column.name != 'update_time' && $column.name != $model.versionColumn.name)
158-
<if test="#name(${model.table.className}).${column.fieldName} != null">
159-
`${column.name}` = #{#name(${model.table.className}).${column.fieldName},jdbcType=${column.jdbcType}},
160-
</if>
161-
#if($column.jdbcType == 'TIMESTAMP')
162-
<if test="#name(${model.table.className}).${column.fieldName} == null">
163-
`${column.name}` = '0000-00-00 00:00:00',
164-
</if>
165-
#end
166-
#end##
167-
#if($column.name == 'update_time')
168-
`${column.name}` = now(),
169-
#end
170-
#end
48+
* @return
49+
*/
50+
int insertSelective(@Param("#name(${model.table.className})") ${model.table.className} #name(${model.table.className}));
51+
52+
53+
/**
54+
*##
17155
#if($model.versionColumn)
172-
`${model.versionColumn.name}` = `${model.versionColumn.name}` + 1,
173-
#end
174-
</trim>
175-
where
176-
#foreach($column in $model.table.pks)
177-
`${column.name}` = #{#name(${model.table.className}).${column.fieldName}}
178-
#if($foreach.hasNext)
179-
and
180-
#end
56+
update with optimistic lock
57+
#else
58+
update
18159
#end
60+
* @param #name(${model.table.className})
61+
62+
* @return
63+
*/
64+
int updateByPrimaryKey(@Param("#name(${model.table.className})") ${model.table.className} #name(${model.table.className}));
65+
66+
/**
67+
*##
18268
#if($model.versionColumn)
183-
and `${model.versionColumn.name}` = #{#name(${model.table.className}).${model.versionColumn.fieldName},jdbcType=${model.versionColumn.jdbcType}}
184-
#end
185-
</update>
69+
update with optimistic lock##
70+
#else
71+
update##
72+
#end selective
73+
* @param #name(${model.table.className})
74+
75+
* @return
76+
*/
77+
int updateByPrimaryKeySelective(@Param("#name(${model.table.className})") ${model.table.className} #name(${model.table.className}));
78+
18679

187-
<!-- The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. -->
80+
/** The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. */
18881

189-
</mapper>
82+
}

src/template/jasmine/tpl/Repository.tpl.vm renamed to src/template/jasmine/tpl/MapperXML.tpl.vm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#set($repoModel = {"table" : $table, "entityPkg":$model.entityPkg, "repoPkg": $model.repoPkg})
44

5+
56
#foreach($column in $table.columns)##
67
#if($column.name == 'is_deleted')
78
#set($repoModel.deleteColumn = $column)
@@ -11,5 +12,5 @@
1112
#end
1213
#end
1314

14-
$processor.generate("${model.tplPath}/Repository.vm", "${model.targetPath}/${table.className}Mapper.java", true, $repoModel)
15+
$processor.generate("${model.tplPath}/MapperXML.vm", "${model.targetPath}/${table.className}Mapper.xml", true, $repoModel)
1516
#end

0 commit comments

Comments
 (0)