【spring】代码生成器

发布时间:2024年01月21日

?

? ? ? ?📝个人主页:五敷有你? ? ??

?🔥系列专栏:spring

??稳中求进,晒太阳

代码生成器(本质IO流)

在mybatis的逆向工程生成model和mapper接口和xml文件后,还需要反复的写Service的接口和实现类。很繁琐和降智。所以可以手写代码模板生成。

1、首先在test包下创建模板(如下这个位置)

不懂为什么继承这个的去看我的(抽取代码思想=》链接)

代码也给你们粘贴一份(注意该一些包名啥的)

package com.qcby.service;

import com.qcby.pojo.Demo;

public interface DemoService extends BaseService<Demo>{

}

2、粘贴我的代码生成器执行类

不需要引入依赖因为我这个主要是依靠java中IO流实现的,不需要外部依赖。?

(注意改变路径)

package com.qcby.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * 代码生成器执行类
 */
public class SourceGenerator {
    public static void main(String[] args) throws Exception {
        generatorSource("Song");
//        generatorSource("Comment");
//        generatorSource("Mtype");
//        generatorSource("Songer");
//        generatorSource("TxUser");
    }

    public static void generatorSource(String objName) throws Exception{
        generateQuery(objName);
        generateDao(objName);
        generateService(objName);
        generateServiceImpl(objName);
    }


    public static void generateQuery(String objName) throws Exception{
        //EbItem
        //创建文件输入流
        BufferedReader br = new BufferedReader(new FileReader("hami_core/src/test/resources/tpl/DemoQuery.tpl"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("hami_core/src/main/java/com/qcby/query/"+objName+"Query.java"));
        String line = null;
        while((line = br.readLine()) != null){
            line = line.replace("Demo", objName);
            bw.write(line);
            bw.newLine();

        }
        bw.close();
        br.close();
    }

    public static void generateDao(String objName) throws Exception{
        //EbItem
        //创建文件输入流
        BufferedReader br = new BufferedReader(new FileReader("hami_core\\src\\test\\resources\\tpl\\DemoMapper.tpl"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("hami_core/src/main/java/com/qcby/mapper/"+objName+"Mapper.java"));
        String line = null;
        while((line = br.readLine()) != null){
            line = line.replace("Demo", objName);
            bw.write(line);
            bw.newLine();

        }
        bw.close();
        br.close();
    }
    public static void generateService(String objName) throws Exception{
        //EbItem
        //创建文件输入流
        BufferedReader br = new BufferedReader(new FileReader("hami_core/src/test/resources/tpl/DemoService.tpl"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("hami_core/src/main/java/com/qcby/service/"+objName+"Service.java"));
        String line = null;
        while((line = br.readLine()) != null){
            line = line.replace("Demo", objName);
            bw.write(line);
            bw.newLine();

        }
        bw.close();
        br.close();
    }
    public static void generateServiceImpl(String objName) throws Exception{

        //把传递过来的实体类的名字改成小写一份
        String lowerObjName = objName.substring(0, 1).toLowerCase()+objName.substring(1, objName.length());

        //EbItem
        //创建文件输入流
        BufferedReader br = new BufferedReader(new FileReader("hami_core/src/test/resources/tpl/DemoServiceImpl.tpl"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("hami_core/src/main/java/com/qcby/service/impl/"+objName+"ServiceImpl.java"));
        String line = null;
        while((line = br.readLine()) != null){
            line = line.replace("Demo", objName);
            line = line.replace("demo", lowerObjName);
            bw.write(line);
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}

运行即可。

文章来源:https://blog.csdn.net/m0_62645012/article/details/135725553
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。