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. Git学习总结(6)——作为一名程序员这些代码托管工具你都知道吗?

    作为一名程序员这些代码托管工具你都知道吗? 作为一名优秀的开发者,大家都会用到代码托管,我本人用的是github,确实github里面有很多很多开源的项目,所以我们目前的创业项目程序员客栈www.pr ...

  2. 全面解读Java中的枚举类型enum的使用

    这篇文章主要介绍了Java中的枚举类型enum的使用,开始之前先讲解了枚举的用处,然后还举了枚举在操作数据库时的实例,需要的朋友可以参考下 关于枚举 大多数地方写的枚举都是给一个枚举然后例子就开始sw ...

  3. Select For update语句浅析

    Select -forupdate语句是我们经常使用手工加锁语句.通常情况下,select语句是不会对数据加锁,妨碍影响其他的DML和DDL操作.同时,在多版本一致读机制的支持下,select语句也不 ...

  4. ArcGIS api for javascript——图形-选择一个范围内的点

    描述 本例展示了如何使用图形显示查询结果,如何使用draw toolbar在地图上选择图形和如何修改图形符号为“高亮”. 在地图上画一个矩形区域,加亮矩形范围内的城市.这个应用统计高亮的城市个数并列出 ...

  5. C#+ArcGIS Engine 获取地图中选中的要素

    转自 C#+ArcGIS Engine 获取地图中选中的要素 C#+ArcGIS Engine 获取地图中选中的要素 提供一种简单遍历获取地图中选中要素的方法,代码如下: List<IFeatu ...

  6. [React] Implement a React Context Provider

    If you have state that needs to exist throughout your application, then you may find yourself passin ...

  7. uip UDP server广播模式(client能够随意port,而且主动向client发送数据)

    眼下移植uip,发现UDP server模式下,必须指定本地port以及clientport,否则仅仅能讲clientport设置为0,才干接收随意port的数据,可是无法发送数据,由于此时clien ...

  8. vue19 组建 Vue.extend component、组件模版、动态组件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. git commit template

    https://www.zhihu.com/question/27462267/answer/204658544 https://gist.github.com/adeekshith/cd4c95a0 ...

  10. Visual Code的调试

    Run 'Debug: Download .NET Core Debugger' in the Command Palette or open a .NET project directory to ...