java|实体bean 根据JPA注解 @table @column 构建sql 语句
1.ClassUtils 辅助类可以传参 实体及属性获取方法名及列名
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import org.apache.log4j.Logger;
/**
* 类辅助
*
* User: dking
*/
public class ClassUtils
/** 日志对象 */
private static final Logger LOG = Logger.getLogger(ClassUtils.class);
private Class
/**
* Map keyed by class containing CachedIntrospectionResults.
* Needs to be a WeakHashMap with WeakReferences as values to allow
* for proper garbage collection in case of multiple class loaders.
*/
private static final Map
.synchronizedMap(new WeakHashMap
/**
* 获取类本身的BeanInfo,不包含父类属性
*
* @param clazz
* @return
*/
public static BeanInfo getSelfBeanInfo(Class> clazz) {
try {
BeanInfo beanInfo;
if (classCache.get(clazz) == null) {
beanInfo = Introspector.getBeanInfo(clazz, clazz.getSuperclass());
classCache.put(clazz, beanInfo);
// Immediately remove class from Introspector cache, to allow for proper
// garbage collection on class loader shutdown - we cache it here anyway,
// in a GC-friendly manner. In contrast to CachedIntrospectionResults,
// Introspector does not use WeakReferences as values of its WeakHashMap!
Class classToFlush = clazz;
do {
Introspector.flushFromCaches(classToFlush);
classToFlush = classToFlush.getSuperclass();
} while (classToFlush != null);
} else {
beanInfo = classCache.get(clazz);
}
return beanInfo;
} catch (IntrospectionException e) {
LOG.error("获取BeanInfo失败", e);
//throw new MincoderException(e);
}
return null;
}
/**
*
获得表名
@param clazz 映射到数据库的po类
@return String
*/
@SuppressWarnings({"unchecked","rawtypes"})
public static String getTableName(Class clazz) {
Table annotation = (Table)clazz.getAnnotation(Table.class);
if(annotation != null){
System.out.println("annotation.name():"+annotation.name());
return annotation.name();
}
return null;
}
/**
*
获得列名
@param clazz 映射到数据库的po类
@param icol 第几列
@return String
*/
@SuppressWarnings({"unchecked","rawtypes"})
public static String getColumnName(Class> clazz, String name) {
try{
Method method = clazz.getDeclaredMethod(name, clazz);
//
Method method= clazz.getMethod(name, clazz);
//
Method method= clazz.getMethod(name, clazz.getClasses());
Column c = method.getAnnotation(Column.class);
return c.name();
}catch(Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 获取方法名
* @param clazz
* @param field
* @return
*/
@SuppressWarnings({"rawtypes"})
public static String getMethodName(Class clazz, String field) {
try{
PropertyDescriptor pd = new PropertyDescriptor(field, clazz);
if (pd != null) {
Method getMethod = pd.getReadMethod();
//获得get方法
Column column = getMethod.getAnnotation(Column.class);
//
System.out.println(getMethod.getName()+" "+column.name());
return getMethod.getName();
}
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取@column 注解值
* @param clazz
* @param field
* @return
*/
@SuppressWarnings({"rawtypes"})
public static String getMethodNameColumn(Class clazz, String field) {
try{
PropertyDescriptor pd = new PropertyDescriptor(field, clazz);
if (pd != null) {
Method getMethod = pd.getReadMethod();
//获得get方法
Column column = getMethod.getAnnotation(Column.class);
//
System.out.println(getMethod.getName()+" "+column.name());
return column.name();
}
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
/*
*
* Discription : 获取字段名称 并将数据放入map集合中
*
* @param clazz
* @return Map
* @throws
* @author : dking
* @date 2013-9-30 上午9:51:49
*/
public static Map getColumnNames(Class> clazz) {
Map map = new HashMap();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
Column c = method.getAnnotation(Column.class);
// 获取注视中注解的对象
if (null != c) {
map.put(c.name(),c.name());
} else {
JoinColumn jc = method.getAnnotation(JoinColumn.class);
// 获取外键的字段名称
if (null != jc) {
map.put(jc.name(),jc.name());
}
}
}
return map;
}
/**
* 初始化实例
*
* @param clazz
* @return
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static Object newInstance(Class> clazz) throws InstantiationException, IllegalAccessException {
return clazz.newInstance();
}
public static void main(String[] args) {
}
}
2. 执行sql的上下文内容,返回执行sql的语句及参数
import java.util.List;
/**
* 执行sql的上下文内容
*
* User: dking
*/
public class SqlContext {
/** 执行的sql */
private StringBuilder sql;
/** 主键名称 */
private String primaryKey;
/** 参数,对应sql中的?号 */
private List
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- Java基础-高级特性-枚举实现状态机