1.

 package com.glodon.gspm.adapter.plugin.common;

 import com.glodon.cloudt.tenancy.context.TenantContext;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.util.StringUtils; import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* 实体对象转Document类
* Created by hezg on 2017/4/4.
*/
public class Bean2Document<T> { //字段前缀
private static final char COLUMN_PREFIX = 'F';
//单词分隔符
private static final char UNDER_LINE = '_';
//字段属性映射
private transient Map<String, PropertyDescriptor> mappedFields; private static Map<Class<?>, Bean2Document> mapperMap = new ConcurrentHashMap<>(); public Bean2Document(Class<T> mappedClass) {
initialize(mappedClass);
} /**
* 创建一个新的Bean2Document类
*
* @param mappedClass 需要映射的实体类
*/
@SuppressWarnings("unchecked")
public static <T> Bean2Document<T> newInstance(Class<T> mappedClass) {
Bean2Document mapper = mapperMap.get(mappedClass);
if (mapper == null) {
mapper = new Bean2Document<>(mappedClass);
mapperMap.put(mappedClass, mapper);
}
return (Bean2Document<T>) mapper;
} /**
* 将实体属性初始化到mappedFields中
*
* @param mappedClass 需要初始化的实体类
*/
@SuppressWarnings("Duplicates")
protected void initialize(Class<T> mappedClass) {
this.mappedFields = new HashMap<>();
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
for (PropertyDescriptor pd : pds) {
if (pd.getWriteMethod() != null) {
this.mappedFields.put(pd.getName().toUpperCase(), pd);
}
}
} /**
* 将实体字段名转换为表字段名
*
* @param name 实体字段
* @return 返回对应表字段名
*/
@SuppressWarnings("Duplicates")
private String underscoreName(String name) {
if (!StringUtils.hasLength(name)) {
return "";
}
// 添加字段前缀
StringBuilder result = new StringBuilder();
if (!StringUtils.isEmpty(COLUMN_PREFIX)) {
result.append(COLUMN_PREFIX).append(UNDER_LINE);
}
result.append(name.substring(0, 1).toUpperCase());
for (int i = 1; i < name.length(); i++) {
String ss = name.substring(i, i + 1);
String slc = ss.toUpperCase();
if (ss.equals(slc)) {
result.append(UNDER_LINE).append(slc);
} else {
result.append(slc);
}
}
return result.toString();
} /**
* 将实体数据格式转换为Document格式
*
* @param data 实体数据
* @return Document格式数据
*/
public Document toDocument(T data) {
if (data == null) {
return null;
} Document document = new Document();
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(data);
for (PropertyDescriptor pd : this.mappedFields.values()) {
Object value = bw.getPropertyValue(pd.getName());
String underscoredName = underscoreName(pd.getName());
document.put(underscoredName, value);
}
//Mongo同一接口,所有类转Document时都带上租户Id
long tenantId = TenantContext.getCurrent()
.getTenantId(); document.put("F_TENANT_ID", tenantId);
return document;
}
}

11.Bean2Document-BEAN转document的更多相关文章

  1. spring装配Bean过程

    主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...

  2. 11月8日上午Jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件

    jquery基础知识 1.jquery文件的引入,所有的js代码要写在下面那段代码下面. <script src="../jquery-1.11.2.min.js">& ...

  3. IOC装配Bean(XML方式)

    Spring框架Bean实例化的方式 提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 无参数构造方法的实例化 <!-- 默认情况下使用的就是 ...

  4. Spring-配置bean的方法(工厂方法和Factorybean)【转】

    通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声 ...

  5. Spring IOC容器中注入bean

    一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...

  6. 【Spring】Spring系列2之bean的配置

    2.bean的配置 2.1.IOC概述 2.2.bean的获取 2.3.依赖注入方式 2.4.属性注入细节 内部bean,不需要ID,ID无效,外部不能引用: 2.5.集合属性注入 2.6.使用p命名 ...

  7. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

  8. document.referrer之隐藏来源

    document.referrer document.referrer是用来获取跳转链接的来源,正规的解释是:referrer 属性可返回载入当前文档的文档的 URL. 实际中使用在广告相关业务中较多 ...

  9. Bean 的生命周期 之 后处理Bean

    这里先把Bean 的生命周期总结一下,然后引出后处理Bean 首先,Bean 的生命周期总共有11步: 1.instantiate bean对象实例化 2.populate properties 封装 ...

  10. spring Bean的完整生命周期

    spring 容器中的bean的完整生命周期一共分为十一步完成. 1.bean对象的实例化 2.封装属性,也就是设置properties中的属性值 3.如果bean实现了BeanNameAware,则 ...

随机推荐

  1. 洛谷—— P1969 积木大赛

    https://www.luogu.org/problem/show?pid=1969 题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度 ...

  2. impala jdbc4的group by语句的bug,加上limit没错

    这里用的ImpalaJDBC4.jar SELECT field1 alias1 FROM table1 where field1 ='xxxx' group by alias1 这句话impala会 ...

  3. java 爬虫在 netbeans 里执行和单独执行结果不一样

    在做内容測试的时候.发现我的爬虫(前些文章略有提及)在 netbeans 里面可以成功爬取网页内容,而单独执行时,给定一个 url,爬到的网页却与在浏览器里面打开 url 的网页全然不一样,这是一个非 ...

  4. Anatomy of an IIS7 configuration path

    The concept of configuration paths is fundamental to managing and operating an IIS server, so I want ...

  5. “DNS隧道”盗号木马分析——类似hjack偷密码然后利用dns tunnel直传数据发送出去

    摘自:http://www.freebuf.com/articles/network/38276.html# 运行后不断监控顶端窗口,一旦发现为QQ,就弹出一个自己伪造的QQ登陆窗口,诱导用户输入密码 ...

  6. Linux常用截图软件

    1.Gnome-screenshot 是一款 GNOME 截图工具,顾名思义,它是一款用来对整个屏幕.一个特定的窗口或者用户所定义一些其他区域进行捕获的工具.该工具提供了几个其他的功能,包括对所捕获的 ...

  7. GPU-directX的发展历史

    GPU发展历史: GPU之前的基础: 1962 麻省理工学院的博士伊凡•苏泽兰发表的论文以及他的画板程序奠定了计算机图形学的基础. 1962-1984 没有专门图形处理硬件,由CPU完成 1984 专 ...

  8. libc.so.6: version GLIBC_2.14 not found

    https://blog.csdn.net/heylun/article/details/78833050

  9. 紫书 例题 9-4 UVa 116 ( 字典序递推顺序)

    这道题在递推方式和那个数字三角形有一点相像,很容易推出来 但是这道题要求的是字典序,这里就有一个递推顺序的问题 这里用逆推,顺推会很麻烦,为什么呢? 如果顺推的话,最后一行假设有种情况是最小值,那么你 ...

  10. PreferenceActivity、PreferenceFragment使用

    文件夹 文件夹 前言 PreferenceActivity preferences_scenario_1xml Preference Activity 演示 PreferenceFragment xm ...