package com.zt.common.utils;
|
|
import org.apache.commons.lang3.reflect.FieldUtils;
|
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Modifier;
|
import java.util.List;
|
import java.util.Vector;
|
|
|
/**
|
* @author 14539
|
*/
|
public class ModelToSqlUtils {
|
|
static List<Field> fields = new Vector<Field>();
|
private static Object target;
|
private static String idName;
|
private static Object idValue;
|
private static SqlType currentType;
|
private static List<Object> param = new Vector<Object>();
|
private static String sqlBuffer;
|
|
public static void ModelToSqlUtils(SqlType sqlType, Object target) {
|
ModelToSqlUtils.target = target;
|
switch (sqlType) {
|
case INSERT:
|
currentType = SqlType.INSERT;
|
createInsert();
|
break;
|
case UPDATE:
|
currentType = SqlType.UPDATE;
|
createUpdate();
|
break;
|
case DELETE:
|
currentType = SqlType.DELETE;
|
createDelete();
|
break;
|
}
|
}
|
|
public static void ModelToSqlUtils(Class<?> target) {
|
String tableName = getTableNameForClass(target);
|
getFields(target);
|
|
StringBuffer sqlBuffer = new StringBuffer();
|
sqlBuffer.append("DELETE FROM ").append(tableName).append(" WHERE ");
|
for (Field field : fields) {
|
if (!Modifier.isStatic(field.getModifiers())) {
|
ID id = field.getAnnotation(ID.class);
|
if (null != id) {
|
sqlBuffer.append(field.getName()).append("=?");
|
}
|
}
|
}
|
ModelToSqlUtils.sqlBuffer = sqlBuffer.toString();
|
}
|
|
/**
|
* 创建跟删除
|
*/
|
private static void createDelete() {
|
String tableName = getTableName();
|
getFields(target.getClass());
|
StringBuffer sqlBuffer = new StringBuffer();
|
sqlBuffer.append("DELETE FROM ").append(tableName).append(" WHERE ");
|
for (Field field : fields) {
|
if (!Modifier.isStatic(field.getModifiers())) {
|
ID id = field.getAnnotation(ID.class);
|
if (null != id) {
|
sqlBuffer.append(field.getName()).append(" = ? ");
|
param.add(readField(field));
|
}
|
}
|
}
|
System.err.println("delete:\t" + sqlBuffer.toString());
|
ModelToSqlUtils.sqlBuffer = sqlBuffer.toString();
|
}
|
|
protected static Object readField(Field field) {
|
try {
|
return FieldUtils.readField(field, target, true);
|
} catch (Exception e) {
|
throw new RuntimeException(currentType.name(), e);
|
}
|
}
|
|
/**
|
* 创建更新语句
|
*/
|
private static void createUpdate() {
|
String tableName = getTableName();
|
getFields(target.getClass());
|
StringBuffer sqlBuffer = new StringBuffer();
|
sqlBuffer.append("UPDATE ").append(tableName).append(" SET ");
|
|
for (Field field : fields) {
|
if (!Modifier.isStatic(field.getModifiers())) {
|
ID id = field.getAnnotation(ID.class);
|
if (id == null) {
|
sqlBuffer.append(field.getName()).append("=? , ");
|
param.add(readField(field));
|
} else {
|
idName = field.getName();
|
idValue = readField(field);
|
}
|
}
|
}
|
sqlBuffer.replace(sqlBuffer.length() - 2, sqlBuffer.length() - 1, " ");
|
if (idName == null) {
|
throw new RuntimeException("not found of " + target.getClass() + "'s ID");
|
}
|
sqlBuffer.append(" WHERE ").append(idName).append("=?");
|
param.add(idValue);
|
System.err.println("update:\t" + sqlBuffer.toString());
|
ModelToSqlUtils.sqlBuffer = sqlBuffer.toString();
|
}
|
|
/**
|
* 根据注解获取表名
|
*/
|
private static String getTableName() {
|
String tableName = null;
|
Class<?> clazz = target.getClass();
|
tableName = getTableNameForClass(clazz);
|
return tableName;
|
}
|
|
private static String getTableNameForClass(Class<?> clazz) {
|
String tableName;
|
Table table = clazz.getAnnotation(Table.class);
|
if (null != table) {
|
tableName = table.name();
|
if ("".equalsIgnoreCase(tableName)) {
|
tableName = clazz.getSimpleName();
|
}
|
} else {
|
tableName = clazz.getSimpleName();
|
}
|
return tableName;
|
}
|
|
/**
|
* 创建插入语句
|
*/
|
private static void createInsert() {
|
String tableName = getTableName();
|
getFields(target.getClass());
|
StringBuffer sqlBuffer = new StringBuffer();
|
sqlBuffer.append("INSERT INTO ").append(tableName).append("(");
|
|
for (Field field : fields) {
|
if (!Modifier.isStatic(field.getModifiers())) {
|
ID id = field.getAnnotation(ID.class);
|
if (id == null) {
|
sqlBuffer.append(field.getName()).append(",");
|
param.add(readField(field));
|
}
|
}
|
}
|
int length = sqlBuffer.length();
|
sqlBuffer.delete(length - 1, length).append(")values(");
|
int size = param.size();
|
for (int x = 0; x < size; x++) {
|
if (x != 0) {
|
sqlBuffer.append(",");
|
}
|
sqlBuffer.append("?");
|
}
|
sqlBuffer.append(")");
|
System.err.println("insert:\t" + sqlBuffer.toString());
|
ModelToSqlUtils.sqlBuffer = sqlBuffer.toString();
|
}
|
|
public static List<Object> getParam() {
|
return param;
|
}
|
|
public static String getSqlBuffer() {
|
return sqlBuffer;
|
}
|
|
public static String getIdName() {
|
return idName;
|
}
|
|
public static Object getIdValue() {
|
return idValue;
|
}
|
|
protected static void getFields(Class<?> clazz) {
|
if (Object.class.equals(clazz)) {
|
return;
|
}
|
Field[] fieldArray = clazz.getDeclaredFields();
|
for (Field file : fieldArray) {
|
fields.add(file);
|
}
|
getFields(clazz.getSuperclass());
|
}
|
|
public enum SqlType {
|
INSERT, UPDATE, DELETE
|
}
|
|
//创建注解,标识该model的table名
|
@java.lang.annotation.Target(value = {java.lang.annotation.ElementType.TYPE})
|
@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
|
public @interface Table {
|
String name() default "";
|
}
|
|
//创建注解,标识该model的id字段
|
@java.lang.annotation.Target(value = {java.lang.annotation.ElementType.FIELD})
|
@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
|
public @interface ID {
|
}
|
}
|