Entitys.java 文件源码

java
阅读 37 收藏 0 点赞 0 评论 0

项目:easycode 作者:
/**
 * 获取主键的值
 * @param target    Entity的class类型
 * @return  如果该Entity没有@Id注解,则return null,如果有一个则返回{val},两个则返回{val1, val2}
 *          其中的val值可能为null,所以需要实时判断下
 */
public static Object[] getPkValues(Object target) {
    Assert.notNull(target, "target param must not be null.");
    Assert.isInstanceOf(Entity.class, target);
    com.easycodebox.jdbc.Table table = Configuration.getTable(target.getClass());
    if(table != null) {
        List<com.easycodebox.jdbc.PkColumn> pks = table.getPrimaryKeys();
        if(pks != null && pks.size() > 0) {
            Object[] vals = new Object[pks.size()];
            for(int i = 0; i < pks.size(); i++) {
                Object val;
                try {
                    val = PropertyUtils.getSimpleProperty(target, pks.get(i).getName());
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    throw new BaseException("Obtain object({0}) property({1}) error.", e, target, pks.get(i).getName());
                }
                vals[i] = val;
            }
            return vals;
        }
    }
    return null;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号