java去mybatis里面自动生成的Example文件 和烦人的注释

6 篇文章 0 订阅
2 篇文章 0 订阅
上篇mybatis里面写了怎么配置mybatis自动生成代码的生成器。
但是生成出来的代码含有Example文件和相当多的注释,令我很反感,所以根据我自己的需求和我想要得到的效果,于是我就编写了的代码来解决这2个烦人的东西 :twisted: :

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.iteye.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生成代码完毕================


[color=red]ps:路径和上一篇有出入,请自行修改成你自己的路径。童鞋们也可以修改这个类,让它可以支持同时操作多个类。[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值