Java自动创建类

MVC结构自动创建controller, service, dao

package testfiles;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @description: 自动创建controller, service, dao
 * @author chyod
 *
 */
public class AutoCreatedClasses {
	
	private static final String CONTROLLER_PACKAGE = "com/chyod/controller";
	private static final String SERVICE_PACKAGE = "com/chyod/service";
	private static final String SERVICE_IMPL_PACKAGE = "com/chyod/service/impl";
	private static final String DAO_PACKAGE = "com/chyod/dao";
	private static final String DAO_IMPL_PACKAGE = "com/chyod/dao/impl";
	
	public static void main(String[] args) {
		String name = "Gf";
		createdController(name);
		createdService(name);
		createdServiceImpl(name);
		createdDao(name);
		createdDaoImpl(name);
	}
	
	public static void createdController(String name) {
		String path = "src/"+CONTROLLER_PACKAGE+"/"+name+"Controller.java";
		String content = "package "+(CONTROLLER_PACKAGE.replaceAll("/", "."))+";"
        		+ "\n"
				+ "import "+(SERVICE_PACKAGE.replaceAll("/", "."))+".I"+name+"Service;"
        		+ "\n"
        		+ "@Controller"
        		+ "\n"
        		+ "public class "+name+"Controller{"
        		+ "\n"
        		+ "    @Autowired\n"
        		+ "    private I" + name + "Service service;"
        		+ "\n"
        		+ "}";
		writeContent(path, content);
	}
	
	public static void createdService(String name) {
		String path = "src/"+SERVICE_PACKAGE+"/I"+name+"Service.java";
		String content = "package "+(SERVICE_PACKAGE.replaceAll("/", "."))+";"
        		+ "\n"
        		+ "\n"
        		+ "public class I"+name+"Service{"
        		+ "\n"
        		+ "\n"
        		+ "}";
		writeContent(path, content);
	}
	
	public static void createdServiceImpl(String name) {
		String path = "src/"+SERVICE_IMPL_PACKAGE+"/"+name+"ServiceImpl.java";
		String content = "package "+(SERVICE_PACKAGE.replaceAll("/", "."))+";"
        		+ "\n"
        		+ "\n"
        		+ "@Service"
        		+ "\n"
        		+ "public class "+name+"ServiceImpl{"
        		+ "\n"
        		+ "\n"
        		+ "}";
		writeContent(path, content);
	}
	
	public static void createdDao(String name) {
		String path = "src/"+DAO_PACKAGE+"/I"+name+"SqlService.java";
		String content = "package "+(DAO_PACKAGE.replaceAll("/", "."))+";"
        		+ "\n"
        		+ "\n"
        		+ "public class I"+name+"SqlService{"
        		+ "\n"
        		+ "\n"
        		+ "}";
		writeContent(path, content);
	}
	
	public static void createdDaoImpl(String name) {
		String path = "src/"+DAO_IMPL_PACKAGE+"/"+name+"SqlServiceImpl.java";
		String content = "package "+(DAO_IMPL_PACKAGE.replaceAll("/", "."))+";"
        		+ "\n"
        		+ "\n"
        		+ "@Service"
        		+ "\n"
        		+ "public class "+name+"SqlServiceImpl{"
        		+ "\n"
        		+ "\n"
        		+ "}";
		writeContent(path, content);
	}
	
	public static void writeContent(String path, String content){
		
		 try{
		        File file = new File(path);
		        System.out.println(file.getAbsolutePath());
		        if(!file.exists()){
		        	file.createNewFile();
		        }
		 
		        //使用true,即进行append file 
		 
		        FileWriter fileWritter = new FileWriter(file, true);
		 
		 
		        fileWritter.write(content);
		 
		        fileWritter.close();
		 
		        System.out.println("finish");
		 
		    }catch(IOException e){
		        e.printStackTrace();
		 
		    }
	}
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值