java后端CRUD 代码生成器

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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值