主要思路是根据po上的注解来生成相应的mapper文件,并输出在控制台上。
解析类:
package com.video.util;
import java.lang.reflect.Field;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import com.video.annotation.Column;
import com.video.annotation.ID;
import com.video.annotation.Table;
public class GeneratorUtil {
private static final ResourceBundle generatorBundle = ResourceBundle
.getBundle("generator");
private static final String START_KEY = "start";
private static final String END_KEY = "end";
private static final String INSERT_STATEMENT = "insert";
private static final String SEARCH_VO_STATEMENT = "selectVoBySo";
private static final String SEARCH_PO_STATEMENT = "selectPoBySo";
private static final String UPDATE_STATEMENT = "update";
private static final String DELETE_STATEMENT = "delete";
private static String tableName;
private static Map<String, String> propertyToColumnMap;
private static final String DECIMAL_SPLIT = ":";
public static void main(String[] args) {
String targetClassName = "com.video.domain.User";
init(targetClassName);
StringBuffer buffer = new StringBuffer();
buffer.append(generatorStart(targetClassName));
buffer.append(generatorSearchPoBySo(targetClassName));
buffer.append(generatorInsert(targetClassName));
buffer.append(generatorUpdate(targetClassName));
buffer.append(generatorEnd(targetClassName));
System.out.println(buffer.toString());
}
private static void init(String targetClassName) {
Class clazz = loadClassByName(targetClassName);
Table tableAno = (Table) clazz.getAnnotation(Table.class);
tableName = tableAno.name();
propertyToColumnMap = new HashMap<String, String>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(ID.class)) {
ID id = field.getAnnotation(ID.class);
try {
propertyToColumnMap.put(field.getName() + DECIMAL_SPLIT
+ field.getType().getName(), id.name());
} catch (Exception e) {
e.printStackTrace();
}
} else if (field.isAnnotationPresent(Column.class)) {
Column column = field.getAnnotation(Column.class);
propertyToColumnMap.put(field.getName() + DECIMAL_SPLIT
+ field.getType().getName(), column.name());
}
}
}
private static String generatorStart(String targetClassName) {
String start = generatorBundle.getString(START_KEY);
return MessageFormat.format(start, new Object[] { targetClassName })
+ "\n\n";
}
private static String generatorEnd(String targetClassName) {
String end = generatorBundle.getString(END_KEY);
return MessageFormat.format(end, new Object[] {}) + "\n";
}
private static String generatorSearchPoBySo(String targetClassName) {
String src = generatorBundle.getString("select");
return MessageFormat.format(src, new Object[] { SEARCH_PO_STATEMENT,
targetClassName, tableName })
+ "\n";
}
private static String generatorSearchVoBySo(String targetClassName) {
String src = generatorBundle.getString("select");
return MessageFormat.format(src, new Object[] { SEARCH_VO_STATEMENT,
targetClassName, tableName })
+ "\n";
}
private static String generatorInsert(String targetClassName) {
String src = generatorBundle.getString(INSERT_STATEMENT);
StringBuffer columnContent = new StringBuffer();
StringBuffer valuesContent = new StringBuffer();
int count = 0;
for (Entry<String, String> entry : propertyToColumnMap.entrySet()) {
if (count > 0) {
columnContent.append(",");
valuesContent.append(",");
}
columnContent.append(entry.getValue());
String[] keys = entry.getKey().split("\\" + DECIMAL_SPLIT);
valuesContent.append("#{" + keys[0] + ",");
valuesContent.append(getStringByType(keys[1]));
valuesContent.append("}");
count++;
}
return MessageFormat.format(src, new Object[] { targetClassName,
tableName, columnContent.toString(),valuesContent.toString() })
+ "\n";
}
private static String getStringByType(String type){
String res=null;
if (Long.class.getName().equals(type)) {
res="jdbcType=BIGINT";
} else if (String.class.getName().equals(type)) {
res="jdbcType=VARCHAR";
} else {
throw new RuntimeException("不支持的属性类型:" + type);
}
return res;
}
private static String generatorUpdate(String targetClassName) {
String src = generatorBundle.getString(UPDATE_STATEMENT);
StringBuffer content = new StringBuffer();
int count=0;
for(Entry<String, String> entity:propertyToColumnMap.entrySet()){
if(count>0){
content.append(",");
}
String[] keys=entity.getKey().split("\\"+DECIMAL_SPLIT);
content.append(entity.getValue()+"=#{"+keys[0]+",");
content.append(getStringByType(keys[1])+"}");
count++;
}
return MessageFormat.format(src, new Object[] { targetClassName,
tableName, content.toString()})
+ "\n";
}
public static Class loadClassByName(String targetClassName) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Class target = cl.loadClass(targetClassName);
return target;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}