太懒,不多说看图,mybatis-generator工具执行后的项目结构
这个时候没得额service接口类,和service实现类,也没的controller类
执行引擎工具类BeanGenerateUtil后结果
可以看见自动创建了包,和service接口类,和service实现类与controller类,别的内容我就不在一一展示了。自动完成了单个类的增删改查的API,注释,注解,都有了,在也不用担心忘记了配置@service注解什么的了,不过模板的一定保证正确哦。
BeanGenerateUtil类
public class BeanGenerateUtil {
private Configuration cfg;
private String projectUrl = "H:\\xxxxxxx\\lazyDevelop"; //项目路径
@SuppressWarnings("deprecation")
public void init() throws IOException {
cfg = new Configuration();
// 设置模板存放位置
cfg.setDirectoryForTemplateLoading(new File(projectUrl + "//src//test//java//generating//template"));
}
public void process(BeanGenerateUtil bgUtil) throws IOException {
Map<String, Object> root = new HashMap<String, Object>();
String model_name = "User";
root.put("page_id", "lazy.develop"); //生成的包名
root.put("dao_id", "lazy.develop.generator"); //dao和实体类的包名
root.put("module_id", "id"); //主键
root.put("model_name", "User"); //类名
root.put("model_name_cn", "用户"); //描述
root.put("object", "user"); //实列名
// 项目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<String, Object> 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("==运行异常:"+ 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成功");
}
}
展示一个模板
/**
* @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);
}
看到这,大家应该都清楚了吧,很简单的,这个方式的灵活性不是很高。不过对开发减少工作量还是不错的,也减少了工作因粗心造成的遗漏。