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. springMVC的rest风格的url请求

    rest是一个架构风格,用url来访问网络上的任何资源.rest的一种思想就是用http中的动作get,post,put,delete,来进行增删改查. 这里介绍的是springMVC的rest请求. ...

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

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

  3. 离线安装 Chrome

    离线安装 Chrome 在这个帮助网页中最下面切换到中文 https://support.google.com/chrome/answer/95346 在网页的中上部点击 "离线安装 Chr ...

  4. 基于Verilog语言的可维护性设计技术

    [注]本文内容主体部分直接翻译参考文献[1]较多内容,因此本文不用于任何商业目的,也不会发表在任何学术刊物上,仅供实验室内部交流和IC设计爱好者交流之用. “曲意而使人喜,不若直节而使人忌:无善而致人 ...

  5. 数字信号处理C语言集(1.1 随机数的产生)

     main.cpp 所建工程文件如下图所示 uniform.h #ifndef _UNIFORM_H_ #define _UNIFORM_H_ double uniform(double a,doub ...

  6. cf B. Black Square

    B. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. LeetCode 0、《两数相加》

    一.给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 ...

  8. 笔记本E450机械硬盘数据迁移到固态硬盘

    背景: E450机械硬盘使用速度过慢,但E450只有一个SATA位,无法直接使用 “分区助手”迁移. 处理: 1.将固态硬盘通过USB口外接在笔记本上 2.正常打开E450,进入桌面 3.对固态硬盘进 ...

  9. window.location.href和window.top.location.href的区别

    if (window.location.href == window.top.location.href) {     window.top.location.href = "/index. ...

  10. mescroll报错

    1.Cannot read property 'insertBefore' of null:说明你的容器id未找到,应确认你的容器id名与你NEW的容器名一致: