java 怎么快速找到实现类_JAVA懒开发:FreeMarker快速实现类的增删改查接口
太懶,不多說看圖,mybatis-generator工具執(zhí)行后的項(xiàng)目結(jié)構(gòu)
這個(gè)時(shí)候沒得額service接口類,和service實(shí)現(xiàn)類,也沒的controller類
執(zhí)行引擎工具類BeanGenerateUtil后結(jié)果
可以看見自動(dòng)創(chuàng)建了包,和service接口類,和service實(shí)現(xiàn)類與controller類,別的內(nèi)容我就不在一一展示了。自動(dòng)完成了單個(gè)類的增刪改查的API,注釋,注解,都有了,在也不用擔(dān)心忘記了配置@service注解什么的了,不過模板的一定保證正確哦。
BeanGenerateUtil類
public class BeanGenerateUtil {
private Configuration cfg;
private String projectUrl = "H:\\xxxxxxx\\lazyDevelop"; //項(xiàng)目路徑
@SuppressWarnings("deprecation")
public void init() throws IOException {
cfg = new Configuration();
// 設(shè)置模板存放位置
cfg.setDirectoryForTemplateLoading(new File(projectUrl + "//src//test//java//generating//template"));
}
public void process(BeanGenerateUtil bgUtil) throws IOException {
Map root = new HashMap();
String model_name = "User";
root.put("page_id", "lazy.develop");//生成的包名
root.put("dao_id", "lazy.develop.generator");//dao和實(shí)體類的包名
root.put("module_id", "id");//主鍵
root.put("model_name", "User");//類名
root.put("model_name_cn", "用戶");//描述
root.put("object", "user");//實(shí)列名
// 項(xiàng)目java文件位置
String ServicePath = projectUrl + "//src//main//java//";
/*************** 生成Service ***************/
String fileName = model_name + "Service.java";
String savePath = "com//lazy//develop//service//";
Template template = cfg.getTemplate("Service.ftl");
bgUtil.buildTemplate(root, ServicePath, savePath, fileName, template);
/*************** 生成ServiceImpl ***************/
String ServiceimplPath = projectUrl + "//src//main//java//";
fileName = model_name + "ServiceImpl.java";
savePath = "com//lazy//develop//service//impl//";
template = cfg.getTemplate("ServiceImpl.ftl");
bgUtil.buildTemplate(root, ServiceimplPath, savePath, fileName, template);
/*************** 生成Controller ***************/
String ControllerPath = projectUrl + "//src//main//java//";
fileName = model_name + "Controller.java";
savePath = "com//lazy//develop//controller//";
template = cfg.getTemplate("Controller.ftl");
bgUtil.buildTemplate(root, ControllerPath, savePath, fileName, template);
}
public void buildTemplate(Map root, String projectPath, String savePath, String fileName, Template template) {
String realFileName = projectPath + savePath + fileName;
String realSavePath = projectPath + "/" + savePath;
File newsDir = new File(realSavePath);
if (!newsDir.exists()) {
newsDir.mkdirs();
}
try {
Writer out = new OutputStreamWriter(new FileOutputStream(realFileName), "UTF-8");
template.process(root, out);
} catch (Exception e) {
System.out.println("==運(yùn)行異常:"+ e);
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BeanGenerateUtil bgUtil = new BeanGenerateUtil();
bgUtil.init();
bgUtil.process(bgUtil);
System.out.println("生成Bean成功");
}
}
展示一個(gè)模板
/**
* @filename:Const 2018年01月01日
* @project 微面 邊鵬 V1.0
* Copyright(c) 2017 BianP Co. Ltd.
* All right reserved.
*/
package com.${page_id}.service;
import com.${dao_id}.entity.${model_name};
/**
* @explain 邏輯層,接口類(${model_name_cn})
* @author BianP
* @class ${model_name}Service.java
*/
public interface ${model_name}Service {
/**
* @explain 查找對象
* @param id
* @return ${model_name}
* @throws Exception
*/
public ${model_name} selectByPrimaryKey(Long id);
/**
* @explain 保存對象(添加)
* @param ${model_name}
* @return int
* @throws Exception
*/
public int insertSelective(${model_name} ${object});
/**
* @explain 有選擇性:修改對象
* @param ${model_name}
* @return int
* @throws Exception
*/
public int updateByPrimaryKeySelective(${model_name} ${object});
/**
* @explain 有選擇性:全修改
* @param ${model_name}
* @return int
* @throws Exception
*/
public int updateByPrimaryKey(${model_name} ${object});
/**
* @explain 刪除對象
* @param id
* @return int
* @throws Exception
*/
public int deleteByPrimaryKey(Long id);
}
看到這,大家應(yīng)該都清楚了吧,很簡單的,這個(gè)方式的靈活性不是很高。不過對開發(fā)減少工作量還是不錯(cuò)的,也減少了工作因粗心造成的遺漏。
總結(jié)
以上是生活随笔為你收集整理的java 怎么快速找到实现类_JAVA懒开发:FreeMarker快速实现类的增删改查接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java增加内容辅助_Eclipse代码
- 下一篇: java统计各空气质量最大值_空气质量历