提交文件

master
星期八 4 years ago
parent a02199fed2
commit 7a4ceac56f
  1. 27
      panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java
  2. 6
      panda-code/src/main/java/org/panda/code/uitl/ReflectionUtil.java
  3. 2
      panda-jdbc/src/main/java/org/panda/jdbc/BeanHander.java
  4. 6
      panda-jdbc/src/main/java/org/panda/jdbc/annotation/Table.java
  5. 10
      panda-jdbc/src/main/java/org/panda/jdbc/sql/BeanSqlUtil.java

@ -49,6 +49,33 @@ public class BeanUtil {
return clazz.getDeclaredFields();
}
/**
* 获取类属性包括父类属性
* @param clazz
*/
public static List<Method> getAllMethods(Class clazz){
final List<Method> methodList = new ArrayList<>();
Class currentClass = clazz;
while (currentClass!=null){
Method[] methods = currentClass.getMethods();
for (Method method:methods){
methodList.add(method);
}
currentClass = currentClass.getSuperclass();
}
return methodList;
}
/**
* 获取类方法
* @param clazz
* @return
*/
public static Method[] getMethods(Class clazz){
return clazz.getMethods();
}
/**
* 获取上一级父类的属性
* @param clazz

@ -37,10 +37,13 @@ public class ReflectionUtil {
/**
* 调用方法
* @param obj 调用对象
* @param method 调用方法
* @param args 参数
* @return
*/
public static Object invokeMethod(Object obj, Method method,Object... args){
Object result = null;
try {
method.setAccessible(true);
result = method.invoke(obj,args);
@ -49,7 +52,6 @@ public class ReflectionUtil {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}

@ -5,6 +5,7 @@ import org.panda.code.uitl.ReflectionUtil;
import org.panda.jdbc.sql.BeanSqlUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@ -27,6 +28,7 @@ public class BeanHander implements ResultSetHandler {
try {
Object bean = BeanUtil.instantiateClass(clazz);
List<Field> fieldList = BeanUtil.getAllField(clazz);
List<Method> methodList =BeanUtil.getAllMethods(clazz);
for (Field f:fieldList){
String fieldName = f.getName();
String columnName = BeanSqlUtil.caseToHump(fieldName);

@ -23,4 +23,10 @@ public @interface Table {
* @return
*/
String charset() default "utf-8";
/**
* 表名
* @return
*/
String name();
}

@ -2,6 +2,7 @@ package org.panda.jdbc.sql;
import org.panda.beans.util.BeanUtil;
import org.panda.jdbc.annotation.Table;
import java.lang.reflect.Field;
import java.util.HashMap;
@ -62,10 +63,17 @@ public class BeanSqlUtil {
* @return
*/
public static String beanNameToTableName(Class entityClazz){
String tableName = null;
Table table = (Table) entityClazz.getAnnotation(Table.class);
if (table==null) {
//类名
String entityName = BeanUtil.getEntityName(entityClazz);
//驼峰转换
String tableName = caseToHump(entityName);
tableName = caseToHump(entityName);
}else {
tableName = table.name();
}
return tableName;
}

Loading…
Cancel
Save