import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* Description: CRUD 代码生成器
* 该工具可以根据传入的类名生成对应的 Controller、Service、ServiceImpl、Mapper 接口和实体类。
*
* @author 曲博
* @date 2025/6/25
*/
public class JavaFileGenerator {
public static void main(String[] args) throws IOException {
// 这里输入你的类名
final String beanName = "EyeWashChecklist";
// 作者名
final String author = "曲博";
generateFiles(beanName, author);
}
@SuppressWarnings({"ResultOfMethodCallIgnored", "AlibabaMethodTooLong"})
public static void generateFiles(String beanName, String author) throws IOException {
String lowerBean = toLowerCamel(beanName);
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
String basePath = "output/";
new File(basePath).mkdirs();
String classComment = String.format(
"""
/**
* Description:
*
* @author %s
* @date %s
*/""", author, dateStr);
// 1. Controller
String controllerContent = String.format("""
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import javax.validation.Valid;
import java.util.Map;
%s
@RestController
@AllArgsConstructor
@RequestMapping("%s")
public class %sController {
private %sService %sService;
@GetMapping("/list")
@ApiOperation(value = "分页", notes = "传入查询参数")
public R<IPage<%s>> list(@ApiIgnore @RequestParam Map<String, Object> information, Query query) {
return R.data(%sService.page(Condition.getPage(query), Condition.getQueryWrapper(information, %s.class)));
}
@PostMapping("/submit")
@ApiOperation(value = "新增或修改", notes = "传入保存信息")
public R<?> submit(@Valid @RequestBody %s information) {
return R.status(%sService.saveOrUpdate(information));
}
@PostMapping("/remove")
@ApiOperation(value = "逻辑删除", notes = "传入ids")
public R<?> remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
return R.status(%sService.removeByIds(Func.toLongList(ids)));
}
}
""", classComment, lowerBean, beanName, beanName, lowerBean,
beanName, lowerBean, beanName, beanName, lowerBean, lowerBean);
writeToFile(basePath + beanName + "Controller.java", controllerContent);
// 2. Service Interface
String serviceContent = String.format("""
%s
public interface %sService extends BaseService<%s> {
}
""", classComment, beanName, beanName);
writeToFile(basePath + beanName + "Service.java", serviceContent);
// 3. ServiceImpl
String serviceImplContent = String.format("""
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
%s
@Service
@AllArgsConstructor
public class %sServiceImpl extends BaseServiceImpl<%sMapper, %s> implements %sService {
}
""", classComment, beanName, beanName, beanName, beanName);
writeToFile(basePath + beanName + "ServiceImpl.java", serviceImplContent);
// 4. Mapper Interface
String mapperContent = String.format("""
%s
public interface %sMapper extends BaseMapper<%s> {
}
""", classComment, beanName, beanName);
writeToFile(basePath + beanName + "Mapper.java", mapperContent);
}
private static void writeToFile(String path, String content) throws IOException {
try (FileWriter writer = new FileWriter(path)) {
writer.write(content);
System.out.println("已生成: " + path);
}
}
private static String toLowerCamel(String name) {
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
}
09-04
5589

11-09
2949
