change mongo

back_recharge
wangyuan 2019-03-12 18:59:14 +08:00
parent a5ade7d53b
commit e4b65bc7aa
5 changed files with 15 additions and 497 deletions

View File

@ -1,412 +0,0 @@
package com.ljsd.common.mogodb;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
public class BeanUtil {
private static Map<Class<?>, Field[]> fieldMap = new ConcurrentHashMap<>();
//是否是byte[]
private static boolean isByteArray(Object obj){
return obj instanceof byte[];
}
//是否是数组
private static boolean isArray(Object obj){
if(isByteArray(obj)){
return false;
}
return obj.getClass().isArray();
}
//将数组对象转为DBObject
private static DBObject array2DBObject(Object obj) throws IllegalAccessException {
Object[] arr = (Object[]) obj;
BasicDBList dbList = new BasicDBList();
for(int i=0; i<arr.length; i++){
Object dbObject = bean2DBObject(arr[i]);
dbList.add(dbObject);
}
return dbList;
}
//是否是list
private static boolean isList(Object obj){
return obj instanceof List;
}
//将list对象转为DBObject
private static DBObject list2DBObject(Object obj) throws IllegalAccessException {
List list = (List) obj;
BasicDBList dbList = new BasicDBList();
for(int i=0; i<list.size(); i++){
Object dbObject = bean2DBObject(list.get(i));
dbList.add(dbObject);
}
return dbList;
}
//是否是map对象
private static boolean isMap(Object obj){
return obj instanceof Map;
}
//将map对象转为DBObject
private static DBObject map2DBObject(Object obj) throws IllegalAccessException {
Map<Object, Object> map = (Map) obj;
DBObject dbObject = new BasicDBObject();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
Object dbObject1 = bean2DBObject(entry.getValue());
dbObject.put(entry.getKey().toString(), dbObject1);
}
return dbObject;
}
//是否是集合对象
private static boolean isSet(Object obj){
return obj instanceof Set;
}
//将集合对象转为DBObject
private static DBObject set2DBObject(Object obj) throws IllegalAccessException {
Set<Object> set = (Set) obj;
BasicDBList dbList = new BasicDBList();
for(Object val: set){
Object dbObject = bean2DBObject(val);
dbList.add(dbObject);
}
return dbList;
}
private static <T> boolean isBaseType(T param){
if (param instanceof Integer) {//判断变量的类型
return true;
} else if (param instanceof String) {
return true;
} else if (param instanceof Double) {
return true;
} else if (param instanceof Float) {
return true;
} else if (param instanceof Long) {
return true;
} else if (param instanceof Boolean) {
return true;
} else if (param instanceof Date) {
return true;
} else if (param instanceof Byte) {
return true;
}
return false;
}
private static Field[] getFields(Class<?> clazz){
Field[] fields = fieldMap.get(clazz);
if(fields == null) {
fields = clazz.getDeclaredFields();
fieldMap.put(clazz, fields);
}
return fields;
}
public static <T> Object bean2DBObject(T bean) throws IllegalArgumentException,
IllegalAccessException {
if (bean == null) {
return null;
}
if(isBaseType(bean)){
return bean;
}
DBObject dbObject = new BasicDBObject();
if (isMap(bean)) {
return map2DBObject(bean);
} else if (isSet(bean)) {
return set2DBObject(bean);
} else if (isArray(bean)) {
return array2DBObject(bean);
} else if (isList(bean)) {
return list2DBObject(bean);
} else if (isByteArray(bean)) {
return dbObject;
}
// 获取对象对应类中的所有属性域
Class<?> clazz = bean.getClass();
while(clazz != null) {
Field[] fields = getFields(clazz);
for (Field field : fields) {
// 获取属性名
String varName = field.getName();
if(varName.startsWith("_")){
continue;
} else if(varName.equals("root")){
continue;
} else if (varName.equals("id")) {
varName = "_id";
}
// 修改访问控制权限
boolean accessFlag = field.isAccessible();
if (!accessFlag) {
field.setAccessible(true);
}
Object param = field.get(bean);
if (param == null) {
continue;
}
if (isBaseType(param)) {
dbObject.put(varName, param);
} else {
DBObject rdbObject = (DBObject) bean2DBObject(param);
dbObject.put(varName, rdbObject);
}
// 恢复访问控制权限
field.setAccessible(accessFlag);
}
clazz = clazz.getSuperclass();
}
return dbObject;
}
//从基础类型中取得值
private static Object getBaseObj(Object object, Class<?> clazz){
if(clazz == String.class){
return object;
}
if(clazz == int.class){
return object;
}
if(clazz == long.class){
return object;
}
if( clazz.equals(float.class)){
return ((Double)object).floatValue();
}
if(clazz == double.class){
return object;
}
if(clazz == byte.class){
return ((Integer)object).byteValue();
}
if(clazz == boolean.class){
return object;
}
if(clazz == Integer.class){
return object;
}
if(clazz == Long.class){
return object;
}
if( clazz.equals(Float.class)){
return ((Double)object).floatValue();
}
if(clazz == Double.class){
return object;
}
if(clazz == Byte.class){
return object;
}
if(clazz == Boolean.class){
return object;
}
return null;
}
//是否是map对象
private static boolean isMap(Class<?> clazz){
if( clazz == Map.class){
return true;
}
return false;
}
//取得key对应的值
private static Object getKey(String key, Class<?> clazz){
if(clazz == Integer.class){
return Integer.valueOf(key);
}
if(clazz == Long.class){
return Long.valueOf(key);
}
return key;
}
//将DBObject转为map
private static <K, V> Map<K, V> dbObject2Map(DBObject dbObject, Field field) throws Exception {
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Type[] types = pt.getActualTypeArguments();
Map map = new HashMap<K, V>();
BasicDBObject dbObj = (BasicDBObject)dbObject;
for (Map.Entry<String, Object> entry : dbObj.entrySet()) {
Type type = types[1];
if(type.getClass()== ParameterizedTypeImpl.class){
Class actualTypeArgument = (Class) (((ParameterizedTypeImpl) type).getActualTypeArguments()[0]);
ArrayList<Object> objects = new ArrayList<>();
BasicDBList values = (BasicDBList)entry.getValue();
Iterator<Object> iterator = values.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
Object obj = getObj(next,actualTypeArgument);
objects.add(obj);
}
Object key = getKey(entry.getKey(), (Class)types[0]);
map.put(key,objects);
}else{
Object obj = getObj(entry.getValue(), (Class)type);
// Object value = ((Class)types[1]).newInstance();
// Object obj = dbObject2Bean((DBObject) entry.getValue(), value, root);
Object key = getKey(entry.getKey(), (Class)types[0]);
map.put(key, obj);
}
}
return map;
}
//是否是集合对象
private static boolean isSet(Class<?> clazz){
if(clazz == Set.class){
return true;
}
return false;
}
//将dbobj转为普通对象
private static Object getObj(Object dbobj, Class<?> clazz) throws Exception {
Object obj = getBaseObj(dbobj, clazz);
if(obj == null){
Constructor constructor = clazz.getDeclaredConstructor();
boolean isAccessible = constructor.isAccessible();
constructor.setAccessible(true);
obj = constructor.newInstance();
constructor.setAccessible(isAccessible);
dbObject2Bean((DBObject) dbobj, obj);
}
return obj;
}
//将DBObject转为set
private static <T> Set<T> dbObject2Set(DBObject dbObject, Field field) throws Exception {
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Type[] types = pt.getActualTypeArguments();
Set set = ConcurrentHashMap.<T> newKeySet();
BasicDBList dbList = (BasicDBList)dbObject;
for (Object dbobj : dbList) {
Object obj = getObj(dbobj, (Class)types[0]);
set.add(obj);
}
return set;
}
//是否是byte[]
private static boolean isByteArray(Class<?> clazz){
return clazz == byte[].class;
}
//是否是数组
private static boolean isArray(Class<?> clazz){
if(isByteArray(clazz)){
return false;
}
return clazz.isArray();
}
//将DBObject转换为数组
private static Object[] dbObject2Array(DBObject dbObject, Class<?> clazz) throws Exception {
BasicDBList basicDBList = (BasicDBList)dbObject;
Object[] objs = (Object[])Array.newInstance(clazz.getComponentType(), basicDBList.size());
for(int i=0; i<basicDBList.size(); i++){
Object dbobj = (Object)basicDBList.get(i);
Object obj = getObj(dbobj, clazz.getComponentType());
objs[i] = obj;
}
return objs;
}
//是否是list
private static boolean isList(Class<?> clazz){
if(clazz == List.class){
return true;
}
if(clazz == ArrayList.class){
return true;
}
if(clazz == CopyOnWriteArrayList.class){
return true;
}
return false;
}
//将DBObject转为list
private static <T> List<T> dbObject2List(DBObject dbObject, Field field) throws Exception {
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Type[] types = pt.getActualTypeArguments();
List list = new CopyOnWriteArrayList<T>();
BasicDBList dbList = (BasicDBList)dbObject;
for (Object dbobj : dbList) {
Object obj = getObj(dbobj, (Class)types[0]);
list.add(obj);
}
return list;
}
//DBObject转换为Bean对象
public static Object dbObject2Bean(DBObject dbObject, Object bean) throws Exception {
if (bean == null) {
return null;
}
Class<?> clazz = bean.getClass();
while (clazz != null) {
Field[] fields = getFields(clazz);
for (Field field : fields) {
String varName = field.getName();
Object object = null;
if (varName.equals("id")) {
object = dbObject.get("_id");
}
// else if(varName.equals("_root")){
// ((MongoBase)bean).setRoot((MongoRoot) root);
// continue;
// }
// else if(varName.equals("bsave")){
// ((MongoRoot)bean).setBsave(true);
// continue;
// }
else {
object = dbObject.get(varName);
}
if (object == null) {
continue;
}
boolean accessFlag = field.isAccessible();
if (!accessFlag) {
field.setAccessible(true);
}
//System.out.println("varName=" + varName);
Class<?> fieldClazz = field.getType();
Object obj = getBaseObj(object, fieldClazz);
if (obj != null) {
field.set(bean, obj);
} else if (isMap(fieldClazz)) {
obj = dbObject2Map((DBObject) object, field);
} else if (isSet(fieldClazz)) {
obj = dbObject2Set((DBObject) object, field);
} else if (isArray(fieldClazz)) {
obj = dbObject2Array((DBObject) object, fieldClazz);
} else if (isList(fieldClazz)) {
obj = dbObject2List((DBObject) object, field);
} else if (isByteArray(fieldClazz)) {
obj = object;
} else {
obj = fieldClazz.newInstance();
obj = dbObject2Bean((DBObject) object, obj);
}
field.set(bean, obj);
// 恢复访问控制权限
field.setAccessible(accessFlag);
}
clazz = clazz.getSuperclass();
}
return bean;
}
public static <T> T dbObject2Bean(DBObject dbObject, Class<T> clazz) throws Exception {
if(dbObject == null){
return null;
}
T bean = clazz.newInstance();
dbObject2Bean(dbObject, bean);
return bean;
}
}

View File

@ -34,19 +34,14 @@ public class LjsdMongoTemplate {
}
public <T> List<T> findAll(String collectionName, Class<T> clazz) throws Exception {
DBCollection coll = mongoTemplate.getCollection(collectionName);
DBCursor dbCursor = coll.find();
List<T> list= new ArrayList<>(dbCursor.count());
for (Iterator<DBObject> it = dbCursor.iterator(); it.hasNext(); ) {
list.add(BeanUtil.dbObject2Bean(it.next(), clazz));
}
return list;
return mongoTemplate.findAll(clazz,collectionName);
}
public <T> T findById(String collectionName, String id, Class<T> clazz) throws Exception {
DBCollection coll = mongoTemplate.getCollection(collectionName);
return mongoTemplate.findById(id,clazz,collectionName);
/* DBCollection coll = mongoTemplate.getCollection(collectionName);
DBObject doc = coll.findOne(id);
return BeanUtil.dbObject2Bean(doc, clazz);
return BeanUtil.dbObject2Bean(doc, clazz);*/
}
public <T> T findById(int id, Class<T> clazz) throws Exception {
@ -104,26 +99,4 @@ public class LjsdMongoTemplate {
public Object convertToMongoType(Object obj){
return this.mongoTemplate.getConverter().convertToMongoType(obj);
}
public void dynamicChangeTheMongoRoot(Object obj,MongoRoot mongoRoot) throws Exception {
Class clazz = obj.getClass();
while (clazz != null && clazz != Object.class && clazz != MongoRoot.class) {
if(clazz == MongoBase.class){
Field root = clazz.getDeclaredField("_root");
root.setAccessible(true);
root.set(obj, new WeakReference<>(mongoRoot));
return;
}
Field[] declaredFields = clazz.getDeclaredFields();
for (Field tmpField : declaredFields){
Class<?> type = tmpField.getType();
if(!MongoBase.class.isAssignableFrom(type)){
continue;
}
tmpField.setAccessible(true);
dynamicChangeTheMongoRoot(tmpField.get(obj),mongoRoot);
}
clazz = clazz.getSuperclass();
}
}
}

View File

@ -34,12 +34,15 @@ public class MongoUpdateCache {
while (it.hasNext()) {
Map.Entry<String, UpdateRequest> entry = it.next();
String key1 = entry.getKey();
if (fullKey.contains(key1)) {
return;
}
if (key1.contains(fullKey)) {
it.remove();
if(!fullKey.equals(key1)){
if (fullKey.contains(key1)) {
return;
}
if (key1.contains(fullKey)) {
it.remove();
}
}
}
}
UpdateRequest request = new UpdateRequest(fullKey, value, opCode, mongoBase);

View File

@ -8,51 +8,4 @@ int mut,int#int,2 mut,int#int,1
6 null 6017#6018#6019#6020
7 null 6021#6022#6023#6024
8 null 6025#6026#6027#6028
9 null 6029#6030#6031#6032
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
0 null null
9 null 6029#6030#6031#6032

View File

@ -4,6 +4,7 @@ import com.ljsd.common.mogodb.MongoBase;
import com.ljsd.jieling.config.SWorkShopSetting;
import com.ljsd.jieling.db.mongo.MongoKey;
import com.ljsd.jieling.logic.dao.root.MailingSystem;
import com.ljsd.jieling.logic.dao.root.User;
import com.ljsd.jieling.util.MathUtils;
import java.util.ArrayList;
@ -22,7 +23,7 @@ public class WorkShopController extends MongoBase {
private int cookExp;
public WorkShopController(){
this.setRootCollection(MailingSystem._COLLECTION_NAME);
this.setRootCollection(User._COLLECTION_NAME);
this.openBlueStateMap = new HashMap<>();