Skip to content

Commit bba108f

Browse files
authored
Merge pull request #151
修改mapper.xml文件生成格式-->实现jdbcType无须手动修改
2 parents 9b9a24d + 4c2e51d commit bba108f

File tree

9 files changed

+136
-36
lines changed

9 files changed

+136
-36
lines changed

spring-boot-demo-codegen/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
<name>spring-boot-demo-codegen</name>
1111
<description>Demo project for Spring Boot</description>
12-
13-
<parent>
14-
<groupId>com.xkcoding</groupId>
15-
<artifactId>spring-boot-demo</artifactId>
16-
<version>1.0.0-SNAPSHOT</version>
17-
</parent>
18-
1912
<properties>
2013
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2114
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

spring-boot-demo-codegen/src/main/java/com/xkcoding/codegen/entity/ColumnEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public class ColumnEntity {
4141
* 属性类型
4242
*/
4343
private String attrType;
44+
/**
45+
* jdbc类型
46+
*/
47+
private String jdbcType;
4448
/**
4549
* 其他信息
4650
*/

spring-boot-demo-codegen/src/main/java/com/xkcoding/codegen/utils/CodeGenUtil.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ private List<String> getTemplates() {
6969
*/
7070
public void generatorCode(GenConfig genConfig, Entity table, List<Entity> columns, ZipOutputStream zip) {
7171
//配置信息
72-
Props props = getConfig();
72+
Props propsDB2Java = getConfig("generator.properties");
73+
Props propsDB2Jdbc = getConfig("jdbc_type.properties");
74+
7375
boolean hasBigDecimal = false;
7476
//表信息
7577
TableEntity tableEntity = new TableEntity();
@@ -85,7 +87,7 @@ public void generatorCode(GenConfig genConfig, Entity table, List<Entity> column
8587
if (StrUtil.isNotBlank(genConfig.getTablePrefix())) {
8688
tablePrefix = genConfig.getTablePrefix();
8789
} else {
88-
tablePrefix = props.getStr("tablePrefix");
90+
tablePrefix = propsDB2Java.getStr("tablePrefix");
8991
}
9092

9193
//表名转换成Java类名
@@ -108,8 +110,10 @@ public void generatorCode(GenConfig genConfig, Entity table, List<Entity> column
108110
columnEntity.setLowerAttrName(StrUtil.lowerFirst(attrName));
109111

110112
//列的数据类型,转换成Java类型
111-
String attrType = props.getStr(columnEntity.getDataType(), "unknownType");
113+
String attrType = propsDB2Java.getStr(columnEntity.getDataType(), "unknownType");
112114
columnEntity.setAttrType(attrType);
115+
String jdbcType = propsDB2Jdbc.getStr(columnEntity.getDataType(), "unknownType");
116+
columnEntity.setJdbcType(jdbcType);
113117
if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
114118
hasBigDecimal = true;
115119
}
@@ -152,21 +156,21 @@ public void generatorCode(GenConfig genConfig, Entity table, List<Entity> column
152156
if (StrUtil.isNotBlank(genConfig.getAuthor())) {
153157
map.put("author", genConfig.getAuthor());
154158
} else {
155-
map.put("author", props.getStr("author"));
159+
map.put("author", propsDB2Java.getStr("author"));
156160
}
157161

158162
if (StrUtil.isNotBlank(genConfig.getModuleName())) {
159163
map.put("moduleName", genConfig.getModuleName());
160164
} else {
161-
map.put("moduleName", props.getStr("moduleName"));
165+
map.put("moduleName", propsDB2Java.getStr("moduleName"));
162166
}
163167

164168
if (StrUtil.isNotBlank(genConfig.getPackageName())) {
165169
map.put("package", genConfig.getPackageName());
166170
map.put("mainPath", genConfig.getPackageName());
167171
} else {
168-
map.put("package", props.getStr("package"));
169-
map.put("mainPath", props.getStr("mainPath"));
172+
map.put("package", propsDB2Java.getStr("package"));
173+
map.put("mainPath", propsDB2Java.getStr("mainPath"));
170174
}
171175
VelocityContext context = new VelocityContext(map);
172176

@@ -213,8 +217,8 @@ private String tableToJava(String tableName, String tablePrefix) {
213217
/**
214218
* 获取配置信息
215219
*/
216-
private Props getConfig() {
217-
Props props = new Props("generator.properties");
220+
private Props getConfig(String fileName) {
221+
Props props = new Props(fileName);
218222
props.autoLoad(true);
219223
return props;
220224
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tinyint=TINYINT
2+
smallint=SMALLINT
3+
mediumint=MEDIUMINT
4+
int=INTEGER
5+
integer=INTEGER
6+
bigint=BIGINT
7+
float=FLOAT
8+
double=DOUBLE
9+
decimal=DECIMAL
10+
bit=BIT
11+
char=CHAR
12+
varchar=VARCHAR
13+
tinytext=VARCHAR
14+
text=VARCHAR
15+
mediumtext=VARCHAR
16+
longtext=VARCHAR
17+
date=DATE
18+
datetime=DATETIME
19+
timestamp=TIMESTAMP
20+
blob=BLOB
21+
longblob=LONGBLOB
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<property name="FILE_ERROR_PATTERN"
4+
value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} %file:%line: %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
5+
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
6+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
7+
<filter class="ch.qos.logback.classic.filter.LevelFilter">
8+
<level>INFO</level>
9+
</filter>
10+
<encoder>
11+
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
12+
<charset>UTF-8</charset>
13+
</encoder>
14+
</appender>
15+
16+
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
17+
<!--如果只是想要 Info 级别的日志,只是过滤 info 还是会输出 Error 日志,因为 Error 的级别高, 所以我们使用下面的策略,可以避免输出 Error 的日志-->
18+
<filter class="ch.qos.logback.classic.filter.LevelFilter">
19+
<!--过滤 Error-->
20+
<level>ERROR</level>
21+
<!--匹配到就禁止-->
22+
<onMatch>DENY</onMatch>
23+
<!--没有匹配到就允许-->
24+
<onMismatch>ACCEPT</onMismatch>
25+
</filter>
26+
<!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即,<File> 的日志都是当天的。-->
27+
<!--<File>logs/info.spring-boot-demo-logback.log</File>-->
28+
<!--滚动策略,按照时间滚动 TimeBasedRollingPolicy-->
29+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
30+
<!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间-->
31+
<FileNamePattern>logs/spring-boot-demo-logback/info.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern>
32+
<!--只保留最近90天的日志-->
33+
<maxHistory>90</maxHistory>
34+
<!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志-->
35+
<!--<totalSizeCap>1GB</totalSizeCap>-->
36+
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
37+
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 -->
38+
<maxFileSize>2MB</maxFileSize>
39+
</timeBasedFileNamingAndTriggeringPolicy>
40+
</rollingPolicy>
41+
<!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">-->
42+
<!--<maxFileSize>1KB</maxFileSize>-->
43+
<!--</triggeringPolicy>-->
44+
<encoder>
45+
<pattern>${FILE_LOG_PATTERN}</pattern>
46+
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
47+
</encoder>
48+
</appender>
49+
50+
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
51+
<!--如果只是想要 Error 级别的日志,那么需要过滤一下,默认是 info 级别的,ThresholdFilter-->
52+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
53+
<level>Error</level>
54+
</filter>
55+
<!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即,<File> 的日志都是当天的。-->
56+
<!--<File>logs/error.spring-boot-demo-logback.log</File>-->
57+
<!--滚动策略,按照时间滚动 TimeBasedRollingPolicy-->
58+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
59+
<!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间-->
60+
<FileNamePattern>logs/spring-boot-demo-logback/error.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern>
61+
<!--只保留最近90天的日志-->
62+
<maxHistory>90</maxHistory>
63+
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
64+
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 -->
65+
<maxFileSize>2MB</maxFileSize>
66+
</timeBasedFileNamingAndTriggeringPolicy>
67+
</rollingPolicy>
68+
<encoder>
69+
<pattern>${FILE_ERROR_PATTERN}</pattern>
70+
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
71+
</encoder>
72+
</appender>
73+
74+
<root level="info">
75+
<appender-ref ref="CONSOLE"/>
76+
<appender-ref ref="FILE_INFO"/>
77+
<appender-ref ref="FILE_ERROR"/>
78+
</root>
79+
</configuration>

spring-boot-demo-codegen/src/main/resources/template/Controller.java.vm

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package ${package}.${moduleName}.controller;
22

33
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
44
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5-
import com.xkcoding.common.R;
6-
import com.xkcoding.scaffold.log.annotations.ApiLog;
5+
import ${package}.${moduleName}.common.R;
76
import ${package}.${moduleName}.entity.${className};
87
import ${package}.${moduleName}.service.${className}Service;
9-
import lombok.AllArgsConstructor;
108
import org.springframework.web.bind.annotation.*;
9+
import org.springframework.beans.factory.annotation.Autowired;
1110
import io.swagger.annotations.Api;
1211
import io.swagger.annotations.ApiOperation;
1312
import io.swagger.annotations.ApiImplicitParam;
1413
import io.swagger.annotations.ApiImplicitParams;
15-
14+
import lombok.extern.slf4j.Slf4j;
1615
/**
1716
* <p>
1817
* ${comments}
@@ -26,13 +25,13 @@ import io.swagger.annotations.ApiImplicitParams;
2625
* @version: V1.0
2726
* @modified: ${author}
2827
*/
28+
@Slf4j
2929
@RestController
30-
@AllArgsConstructor
3130
@RequestMapping("/${pathName}")
3231
@Api(description = "${className}Controller", tags = {"${comments}"})
3332
public class ${className}Controller {
34-
35-
private final ${className}Service ${classname}Service;
33+
@Autowired
34+
private ${className}Service ${classname}Service;
3635

3736
/**
3837
* 分页查询${comments}
@@ -47,7 +46,7 @@ public class ${className}Controller {
4746
@ApiImplicitParam(name = "${classname}", value = "查询条件", required = true)
4847
})
4948
public R list${className}(Page page, ${className} ${classname}) {
50-
return new R<>(${classname}Service.page(page,Wrappers.query(${classname})));
49+
return R.success(${classname}Service.page(page,Wrappers.query(${classname})));
5150
}
5251

5352

@@ -62,19 +61,18 @@ public class ${className}Controller {
6261
@ApiImplicitParam(name = "${pk.lowerAttrName}", value = "主键id", required = true)
6362
})
6463
public R get${className}(@PathVariable("${pk.lowerAttrName}") ${pk.attrType} ${pk.lowerAttrName}){
65-
return new R<>(${classname}Service.getById(${pk.lowerAttrName}));
64+
return R.success(${classname}Service.getById(${pk.lowerAttrName}));
6665
}
6766

6867
/**
6968
* 新增${comments}
7069
* @param ${classname} ${comments}
7170
* @return R
7271
*/
73-
@ApiLog("新增${comments}")
7472
@PostMapping
7573
@ApiOperation(value = "新增${comments}", notes = "新增${comments}")
7674
public R save${className}(@RequestBody ${className} ${classname}){
77-
return new R<>(${classname}Service.save(${classname}));
75+
return R.success(${classname}Service.save(${classname}));
7876
}
7977

8078
/**
@@ -83,29 +81,27 @@ public class ${className}Controller {
8381
* @param ${classname} ${comments}
8482
* @return R
8583
*/
86-
@ApiLog("修改${comments}")
8784
@PutMapping("/{${pk.lowerAttrName}}")
8885
@ApiOperation(value = "修改${comments}", notes = "修改${comments}")
8986
@ApiImplicitParams({
9087
@ApiImplicitParam(name = "${pk.lowerAttrName}", value = "主键id", required = true)
9188
})
9289
public R update${className}(@PathVariable ${pk.attrType} ${pk.lowerAttrName}, @RequestBody ${className} ${classname}){
93-
return new R<>(${classname}Service.updateById(${classname}));
90+
return R.success(${classname}Service.updateById(${classname}));
9491
}
9592

9693
/**
9794
* 通过id删除${comments}
9895
* @param ${pk.lowerAttrName} id
9996
* @return R
10097
*/
101-
@ApiLog("删除${comments}")
10298
@DeleteMapping("/{${pk.lowerAttrName}}")
10399
@ApiOperation(value = "删除${comments}", notes = "删除${comments}")
104100
@ApiImplicitParams({
105101
@ApiImplicitParam(name = "${pk.lowerAttrName}", value = "主键id", required = true)
106102
})
107103
public R delete${className}(@PathVariable ${pk.attrType} ${pk.lowerAttrName}){
108-
return new R<>(${classname}Service.removeById(${pk.lowerAttrName}));
104+
return R.success(${classname}Service.removeById(${pk.lowerAttrName}));
109105
}
110106

111107
}

spring-boot-demo-codegen/src/main/resources/template/Entity.java.vm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import lombok.EqualsAndHashCode;
88
#if(${hasBigDecimal})
99
import java.math.BigDecimal;
1010
#end
11-
import java.io.Serializable;
1211
import java.time.LocalDateTime;
13-
12+
import io.swagger.annotations.ApiModel;
13+
import io.swagger.annotations.ApiModelProperty;
14+
import lombok.NoArgsConstructor;
1415
/**
1516
* <p>
1617
* ${comments}
@@ -25,6 +26,7 @@ import java.time.LocalDateTime;
2526
* @modified: ${author}
2627
*/
2728
@Data
29+
@NoArgsConstructor
2830
@TableName("${tableName}")
2931
@ApiModel(description = "${comments}")
3032
@EqualsAndHashCode(callSuper = true)

spring-boot-demo-codegen/src/main/resources/template/Mapper.xml.vm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<resultMap id="${classname}Map" type="${package}.${moduleName}.entity.${className}">
55
#foreach($column in $columns)
66
#if($column.lowerAttrName==$pk.lowerAttrName)
7-
<id property="${pk.lowerAttrName}" jdbcType="${pk.dataType}" column="${pk.columnName}"/>
7+
<id property="${pk.lowerAttrName}" jdbcType="${pk.jdbcType}" column="${pk.columnName}"/>
88
#else
9-
<result property="${column.lowerAttrName}" jdbcType="${column.dataType}" column="${column.columnName}"/>
9+
<result property="${column.lowerAttrName}" jdbcType="${column.jdbcType}" column="${column.columnName}"/>
1010
#end
1111
#end
1212
</resultMap>

spring-boot-demo-codegen/src/main/resources/template/ServiceImpl.java.vm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ${package}.${moduleName}.entity.${className};
55
import ${package}.${moduleName}.mapper.${className}Mapper;
66
import ${package}.${moduleName}.service.${className}Service;
77
import org.springframework.stereotype.Service;
8-
8+
import lombok.extern.slf4j.Slf4j;
99
/**
1010
* <p>
1111
* ${comments}
@@ -20,6 +20,7 @@ import org.springframework.stereotype.Service;
2020
* @modified: ${author}
2121
*/
2222
@Service
23+
@Slf4j
2324
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {
2425

2526
}

0 commit comments

Comments
 (0)