设为首页 收藏本站
查看: 1518|回复: 0

[经验分享] java去mybatis里面自动生成的Example文件 和烦人的注释

[复制链接]

尚未签到

发表于 2016-11-28 08:56:04 | 显示全部楼层 |阅读模式
上篇mybatis里面写了怎么配置mybatis自动生成代码的生成器。
但是生成出来的代码含有Example文件和相当多的注释,令我很反感,所以根据我自己的需求和我想要得到的效果,于是我就编写了的代码来解决这2个烦人的东西 DSC0000.gif
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 删除mybatis的注释类
* @author jynine
*
*/
public class DelAnnotation {
private static String PROJECTPATH = null;
private static String MODELPATH = null;
private static String DAOPATH = null;
private static String MAPPERPATH = null;
/**
* 初始化路径
* @param pro
*/
public static void initPath(Properties pro){
PROJECTPATH = pro.getProperty("projectpath");
if(PROJECTPATH != null && !PROJECTPATH.equals("")){
MODELPATH = PROJECTPATH + pro.getProperty("modelpath");
DAOPATH = PROJECTPATH + pro.getProperty("daopath");
MAPPERPATH = PROJECTPATH + pro.getProperty("mapperpath");
}
}
/**
*
* @param oldFile
* @param newFile
* @param className
*            类名
* @param type
*            0:model 1:dao 2:mapper
*/
public static void copyFile(File oldFile, File newFile, String className,
int type) {
BufferedReader reader = null;
FileWriter fileWriter = null;
try {
reader = new BufferedReader(new FileReader(oldFile));
fileWriter = new FileWriter(newFile);
String tempString = null;
int line = 1;
boolean tag = true;
boolean delimport = false;
boolean examTag = false;
String examStr = "";
while ((tempString = reader.readLine()) != null) {
if (type == 2) {
if (tempString.contains("<!--")) {
tag = false;
}
if(tempString.contains(className+"Mapper")){
tempString = tempString.replace(className+"Mapper", className+"Dao");
}
if(tempString.contains("Example") && !examTag){
examTag = true;
examStr = tempString.substring(tempString.indexOf("<")+1,tempString.indexOf(" ", tempString.indexOf("<")));
}
if (tag && !examTag) {
fileWriter.write(tempString + "\n");
}
if (tempString.contains("-->")) {
tag = true;
}
if(examStr != null && examStr != ""){
if(tempString.contains("</"+examStr+">")){
examTag = false;
}
}
line++;
} else {
delimport = false;
if (tempString.contains("/**")) {
tag = false;
}
if (tag) {
if (type == 1) {
if (tempString.indexOf(className + "Mapper") != -1) {
tempString = tempString.replaceAll(className
+ "Mapper", className + "Dao");
}
if (tempString.indexOf(className + "Example") != -1) {
delimport = true;
}
if(tempString.indexOf("record") != -1){
tempString = tempString.replaceAll(
"record",className.substring(0, 1).toLowerCase()
+ className.substring(1,
className.length()));
}
if (tempString.indexOf("example") != -1) {
tempString = tempString.replaceAll(
"example",className.substring(0, 1).toLowerCase()
+ className.substring(1,
className.length()));
}
if(!delimport){
fileWriter.write(tempString + "\n");
}
}else{
fileWriter.write(tempString + "\n");
}
}
if (tempString.contains("*/")) {
tag = true;
}
line++;
}
}
fileWriter.close();
reader.close();
oldFile.delete();
} catch (IOException e) {
System.out.println("文件删除失败:"+e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
fileWriter.close();
} catch (IOException e1) {
System.out.println("流关闭异常:"+e1.getMessage());
}
}
}
}
/**
* 删除所有注释
* @param table
*/
public static void delAllAnnotation(String table) {
delModelAnnotation(table);
delDaoAnnotation(table);
delXmlAnnotation(table);
System.out.println("注释清理成功!");
}
/**
* 删除类中的注释
* @param table
*/
public static void delModelAnnotation(String table){
File oldFile = new File(MODELPATH + table + ".java");
File newFile = new File(MODELPATH + table + "Temp1.java");
File tempFile = new File(MODELPATH + table + "Example.java");
tempFile.delete();
copyFile(oldFile, newFile, table, 0);
copyFile(newFile, oldFile, table, 0);
}
/**
* 删除dao中的注释
* @param table
*/
public static void delDaoAnnotation(String table){
File oldDaoFile = new File(DAOPATH + table + "Mapper.java");
File newDaoFile = new File(DAOPATH + table + "Dao.java");
copyFile(oldDaoFile, newDaoFile, table, 1);
}
/**
* 删除xml中的注释
* @param table
*/
public static void delXmlAnnotation(String table){
File oldMapperFile = new File(MAPPERPATH + table + "Mapper.xml");
File newMapperFile = new File(MAPPERPATH + table + "MapperTemp1.xml");
copyFile(oldMapperFile, newMapperFile, table, 2);
copyFile(newMapperFile, oldMapperFile, table, 2);
}

public static void main(String[] args) {
InputStream in =new DelAnnotation().getClass().getClassLoader()
.getResourceAsStream("config.properties");
Properties pro = new Properties();
try {
pro.load(in);
initPath(pro);
if(PROJECTPATH != null && !PROJECTPATH.equals("")){
delAllAnnotation(pro.getProperty("classname"));
}else{
System.out.println("请配置路径!");
}
} catch (IOException e) {
System.out.println("出错了!!");
}
}
}


config.properties配置文件

classname=TestMasterlog
projectpath=F:/myworkspace/TSSC
modelpath=/src/main/java/com/complaint/model/
daopath=/src/main/java/com/complaint/dao/
mapperpath=/src/main/resources/com/complaint/dao/mapping/

你也可以将上面的代码保存在和generator.bat(不知道generator.bat的童鞋可以看上一篇:http://jynine.iyunv.com/blog/1879707)同一个路径下,
然后再generator.bat里面添加下面的代码:
javac DelAnnotation.java
java DelAnnotation

generator.bat最终效果如下:

@echo off
echo==========mybatis开始生成代码================
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite  
javac DelAnnotation.java
java DelAnnotation
pause
echo==========mybatis生成代码完毕================

ps:路径和上一篇有出入,请自行修改成你自己的路径。童鞋们也可以修改这个类,让它可以支持同时操作多个类。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306412-1-1.html 上篇帖子: mybatis sql String>Double, 类型参数被强转为数值类型 下篇帖子: 源码解读Mybatis List列表In查询实现的注意事项
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表