使用freemarker,根据表自动生成实体类、指令、Controller、Service、Dao、html
时间: 2023-07-04 09:11:51 浏览: 223
您可以使用FreeMarker作为模板引擎,通过编写模板来自动生成所需的实体类、指令、Controller、Service、Dao和HTML等文件。以下是一些示例模板以供参考:
实体类模板:
```java
package ${basePackage}.entity;
import lombok.Data;
@Data
public class ${className} {
<#list properties as prop>
/**
* ${prop.comment}
*/
private ${prop.type} ${prop.name};
</#list>
}
```
指令模板:
```java
package ${basePackage}.command;
import lombok.Data;
@Data
public class ${className}Command {
<#list properties as prop>
/**
* ${prop.comment}
*/
private ${prop.type} ${prop.name};
</#list>
}
```
Controller模板:
```java
package ${basePackage}.controller;
import ${basePackage}.command.${className}Command;
import ${basePackage}.entity.${className};
import ${basePackage}.service.${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/${classPath}")
public class ${className}Controller {
@Autowired
private ${className}Service ${className?uncap_first}Service;
@PostMapping
public ${className} create(@RequestBody ${className}Command ${className?uncap_first}Command) {
return ${className?uncap_first}Service.create(${className?uncap_first}Command);
}
@GetMapping("/{id}")
public ${className} getById(@PathVariable Long id) {
return ${className?uncap_first}Service.getById(id);
}
@PutMapping("/{id}")
public ${className} updateById(@PathVariable Long id, @RequestBody ${className}Command ${className?uncap_first}Command) {
return ${className?uncap_first}Service.updateById(id, ${className?uncap_first}Command);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Long id) {
${className?uncap_first}Service.deleteById(id);
}
}
```
Service模板:
```java
package ${basePackage}.service;
import ${basePackage}.command.${className}Command;
import ${basePackage}.entity.${className};
import ${basePackage}.dao.${className}Dao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ${className}Service {
@Autowired
private ${className}Dao ${className?uncap_first}Dao;
public ${className} create(${className}Command ${className?uncap_first}Command) {
${className} ${className?uncap_first} = new ${className}();
BeanUtils.copyProperties(${className?uncap_first}Command, ${className?uncap_first});
return ${className?uncap_first}Dao.save(${className?uncap_first});
}
public ${className} getById(Long id) {
return ${className?uncap_first}Dao.getById(id);
}
public ${className} updateById(Long id, ${className}Command ${className?uncap_first}Command) {
${className} ${className?uncap_first} = ${className?uncap_first}Dao.getById(id);
BeanUtils.copyProperties(${className?uncap_first}Command, ${className?uncap_first});
return ${className?uncap_first}Dao.save(${className?uncap_first});
}
public void deleteById(Long id) {
${className?uncap_first}Dao.deleteById(id);
}
}
```
Dao模板:
```java
package ${basePackage}.dao;
import ${basePackage}.entity.${className};
import org.springframework.data.jpa.repository.JpaRepository;
public interface ${className}Dao extends JpaRepository<${className}, Long> {
}
```
HTML模板:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${className}列表</title>
</head>
<body>
<h1>${className}列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<#list properties as prop>
<th>${prop.comment}</th>
</#list>
<th>操作</th>
</tr>
</thead>
<tbody>
<#list dataList as data>
<tr>
<td>${data.id}</td>
<#list properties as prop>
<td>${data[prop.name]}</td>
</#list>
<td>
<a href="/${classPath}/${data.id}">详情</a>
<a href="/${classPath}/${data.id}/edit">编辑</a>
<a href="/${classPath}/${data.id}?_method=DELETE">删除</a>
</td>
</tr>
</#list>
</tbody>
</table>
<a href="/${classPath}/new">新建${className}</a>
</body>
</html>
```
以上仅是模板的示例,您可以根据自己的需求进行修改和扩展。使用FreeMarker生成文件的具体步骤如下:
1. 定义模板文件,可以使用FreeMarker的语法编写模板;
2. 定义模板数据,可以是Java对象或者Map等;
3. 使用FreeMarker的Template类加载模板文件,得到Template对象;
4. 创建Writer对象,将渲染后的模板内容输出到文件中。
以下是一个示例代码,用于生成实体类文件:
```java
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class EntityGenerator {
public static void generate(String basePackage, String className, Map<String, Object> properties) throws IOException, TemplateException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setClassLoaderForTemplateLoading(EntityGenerator.class.getClassLoader(), "templates");
cfg.setDefaultEncoding("UTF-8");
Template template = cfg.getTemplate("entity.ftl");
Map<String, Object> data = new HashMap<>();
data.put("basePackage", basePackage);
data.put("className", className);
data.put("properties", properties);
File file = new File(String.format("%s/src/main/java/%s/entity/%s.java", System.getProperty("user.dir"), basePackage.replaceAll("\\.", "/"), className));
FileWriter writer = new FileWriter(file);
template.process(data, writer);
writer.close();
System.out.println(String.format("Entity generated: %s", file.getAbsolutePath()));
}
}
```
使用方式示例:
```java
Map<String, Object> properties = new HashMap<>();
properties.put("id", new Property("Long", "ID"));
properties.put("name", new Property("String", "名称"));
properties.put("age", new Property("Integer", "年龄"));
EntityGenerator.generate("com.example.demo", "User", properties);
```
这段代码将会生成一个名为User.java的实体类文件,包含id、name和age三个属性。
阅读全文