用springboot+beetl生成一套根据entity实体类生成的controller,service,mapper的代码模板
时间: 2023-09-02 10:09:58 浏览: 215
代码生成器,可以生成entity,controller,service,mapper里的代码,带有swagger2
好的,可以使用以下步骤来生成SpringBoot+Beetl的代码模板:
1. 首先,在你的SpringBoot项目中添加Beetl的依赖,可以在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.beetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>2.8.1.RELEASE</version>
</dependency>
```
2. 创建一个Beetl模板文件,命名为entity.btl,内容如下:
```java
package ${packageName}.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Data
@Entity
@Table(name = "${tableName}")
public class ${className} {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
<#list columns as column>
/**
* ${column.columnComment}
*/
private ${column.columnType} ${column.columnName};
</#list>
private Date createTime;
private Date updateTime;
}
```
3. 在同级目录下创建generator.btl,用于生成Controller、Service和Mapper的代码,内容如下:
```java
package ${packageName};
import org.beetl.core.Template;
import org.beetl.sql.core.NameConversion;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.core.kit.StringUtils;
import org.beetl.sql.core.mapper.BaseMapper;
import org.beetl.sql.core.mapper.MapperInvoke;
import org.beetl.sql.core.mapper.builder.MapperBuilder;
import org.beetl.sql.core.mapper.builder.MapperConfig;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class ${className}Generator {
private static final String CONTROLLER_TEMPLATE = "package ${packageName}.controller;\n" +
"\n" +
"import ${packageName}.entity.${className};\n" +
"import ${packageName}.service.${className}Service;\n" +
"import lombok.RequiredArgsConstructor;\n" +
"import org.springframework.http.HttpStatus;\n" +
"import org.springframework.http.ResponseEntity;\n" +
"import org.springframework.web.bind.annotation.*;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"@RestController\n" +
"@RequestMapping(\"/${tableName}\")\n" +
"@RequiredArgsConstructor\n" +
"public class ${className}Controller {\n" +
"\n" +
" private final ${className}Service ${classNameVar}Service;\n" +
"\n" +
" @GetMapping\n" +
" public ResponseEntity<List<${className}>> getAll${className}() {\n" +
" return ResponseEntity.ok(${classNameVar}Service.getAll());\n" +
" }\n" +
"\n" +
" @GetMapping(\"/{id}\")\n" +
" public ResponseEntity<${className}> get${className}ById(@PathVariable Long id) {\n" +
" return ResponseEntity.ok(${classNameVar}Service.getById(id));\n" +
" }\n" +
"\n" +
" @PostMapping\n" +
" public ResponseEntity<${className}> create${className}(@RequestBody ${className} ${classNameVar}) {\n" +
" return ResponseEntity.status(HttpStatus.CREATED).body(${classNameVar}Service.create(${classNameVar}));\n" +
" }\n" +
"\n" +
" @PutMapping(\"/{id}\")\n" +
" public ResponseEntity<${className}> update${className}(@PathVariable Long id, @RequestBody ${className} ${classNameVar}) {\n" +
" return ResponseEntity.ok(${classNameVar}Service.update(id, ${classNameVar}));\n" +
" }\n" +
"\n" +
" @DeleteMapping(\"/{id}\")\n" +
" public ResponseEntity<Void> delete${className}(@PathVariable Long id) {\n" +
" ${classNameVar}Service.deleteById(id);\n" +
" return ResponseEntity.noContent().build();\n" +
" }\n" +
"}";
private static final String SERVICE_TEMPLATE = "package ${packageName}.service;\n" +
"\n" +
"import ${packageName}.entity.${className};\n" +
"import ${packageName}.mapper.${className}Mapper;\n" +
"import lombok.RequiredArgsConstructor;\n" +
"import org.springframework.stereotype.Service;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"@Service\n" +
"@RequiredArgsConstructor\n" +
"public class ${className}Service {\n" +
"\n" +
" private final ${className}Mapper ${classNameVar}Mapper;\n" +
"\n" +
" public List<${className}> getAll() {\n" +
" return ${classNameVar}Mapper.all();\n" +
" }\n" +
"\n" +
" public ${className} getById(Long id) {\n" +
" return ${classNameVar}Mapper.single(id);\n" +
" }\n" +
"\n" +
" public ${className} create(${className} ${classNameVar}) {\n" +
" ${classNameVar}.setId(null);\n" +
" ${classNameVar}Mapper.insert(${classNameVar});\n" +
" return ${classNameVar};\n" +
" }\n" +
"\n" +
" public ${className} update(Long id, ${className} ${classNameVar}) {\n" +
" ${classNameVar}.setId(id);\n" +
" ${classNameVar}Mapper.updateTemplateById(${classNameVar});\n" +
" return ${classNameVar};\n" +
" }\n" +
"\n" +
" public void deleteById(Long id) {\n" +
" ${classNameVar}Mapper.deleteById(id);\n" +
" }\n" +
"}";
private static final String MAPPER_TEMPLATE = "package ${packageName}.mapper;\n" +
"\n" +
"import ${packageName}.entity.${className};\n" +
"import org.beetl.sql.core.mapper.BaseMapper;\n" +
"import org.beetl.sql.core.mapper.MapperTemplate;\n" +
"import org.springframework.stereotype.Repository;\n" +
"\n" +
"@Repository\n" +
"public interface ${className}Mapper extends BaseMapper<${className}> {\n" +
"\n" +
"}";
public static void main(String[] args) throws Exception {
String packageName = "com.example.demo";
String tablePrefix = "t_";
String entityName = "${className}";
String classNameVar = StringUtils.toLowerCaseFirstOne(entityName);
String entityPackage = packageName + ".entity";
String servicePackage = packageName + ".service";
String controllerPackage = packageName + ".controller";
String mapperPackage = packageName + ".mapper";
// 获取所有的表名
List<String> tableNames = new ArrayList<>();
tableNames.add("user");
tableNames.add("role");
for (String tableName : tableNames) {
// 获取表的元数据信息
TableDesc tableDesc = MapperBuilder.INSTANCE.getMapperConfig().getDbConfig().getTableDesc(tableName);
// 将下划线转换为驼峰命名
String className = NameConversion.underlineToCamel(tableDesc.getName(), true);
String fileName = className + ".java";
// 获取模板
Template controllerTemplate = MapperBuilder.INSTANCE.getEngine().getTemplate(CONTROLLER_TEMPLATE);
Template serviceTemplate = MapperBuilder.INSTANCE.getEngine().getTemplate(SERVICE_TEMPLATE);
Template mapperTemplate = MapperBuilder.INSTANCE.getEngine().getTemplate(MAPPER_TEMPLATE);
// 设置模板参数
controllerTemplate.binding("packageName", controllerPackage);
controllerTemplate.binding("tableName", tableName);
controllerTemplate.binding("className", className);
controllerTemplate.binding("classNameVar", classNameVar);
serviceTemplate.binding("packageName", servicePackage);
serviceTemplate.binding("tableName", tableName);
serviceTemplate.binding("className", className);
serviceTemplate.binding("classNameVar", classNameVar);
mapperTemplate.binding("packageName", mapperPackage);
mapperTemplate.binding("tableName", tableName);
mapperTemplate.binding("className", className);
mapperTemplate.binding("classNameVar", classNameVar);
// 创建文件夹
Files.createDirectories(Paths.get("src/main/java/" + controllerPackage.replace(".", "/")));
Files.createDirectories(Paths.get("src/main/java/" + servicePackage.replace(".", "/")));
Files.createDirectories(Paths.get("src/main/java/" + mapperPackage.replace(".", "/")));
// 生成文件
MapperInvoke invoke = new MapperInvoke();
invoke.setEntityPackage(entityPackage);
invoke.setMapperClass(BaseMapper.class);
invoke.setMapperTemplate(MapperTemplate.class);
invoke.setSqlManager(MapperBuilder.INSTANCE.getSqlManager());
invoke.addOutput(controllerPackage, fileName, controllerTemplate.render());
invoke.addOutput(servicePackage, fileName, serviceTemplate.render());
invoke.addOutput(mapperPackage, fileName, mapperTemplate.render());
MapperConfig config = new MapperConfig();
config.setMapperInvoke(invoke);
config.setEntityPackage(entityPackage);
config.setMapperPackage(mapperPackage);
config.setDaoSuffix("Mapper");
config.setSqlManager(MapperBuilder.INSTANCE.getSqlManager());
config.setTablePrefix(tablePrefix);
config.init();
MapperBuilder.INSTANCE.build(config);
}
}
}
```
4. 运行${className}Generator类的main方法,即可自动生成Controller、Service和Mapper的代码。
需要注意的是,这里的代码模板只是一个简单的示例,如果需要根据实际需求进行修改。同时,生成的代码中可能需要根据实际情况进行一些调整和优化,比如添加日志记录、异常处理等。
阅读全文