MongoDB入门(5)- 我们自己封装的MongoDB-Java版本
用法
实体定义
package com.wisdombud.mongotool;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "Project")
public class Project extends MongoDbEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String ProjectId;
private String weituodanwei;
private String weituodanweidizhi;
private String post;
private String yangpinzhizaodanwei;
private String zhizaodanweidizhi;
private String phone;
private String contact;
private String yangpinjiazhi;
public String getProjectId() {
return ProjectId;
}
public void setProjectId(String projectId) {
ProjectId = projectId;
}
public String getWeituodanwei() {
return weituodanwei;
}
public void setWeituodanwei(String weituodanwei) {
this.weituodanwei = weituodanwei;
}
public String getWeituodanweidizhi() {
return weituodanweidizhi;
}
public void setWeituodanweidizhi(String weituodanweidizhi) {
this.weituodanweidizhi = weituodanweidizhi;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getYangpinzhizaodanwei() {
return yangpinzhizaodanwei;
}
public void setYangpinzhizaodanwei(String yangpinzhizaodanwei) {
this.yangpinzhizaodanwei = yangpinzhizaodanwei;
}
public String getZhizaodanweidizhi() {
return zhizaodanweidizhi;
}
public void setZhizaodanweidizhi(String zhizaodanweidizhi) {
this.zhizaodanweidizhi = zhizaodanweidizhi;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getYangpinjiazhi() {
return yangpinjiazhi;
}
public void setYangpinjiazhi(String yangpinjiazhi) {
this.yangpinjiazhi = yangpinjiazhi;
}
public String getShenqingleixing() {
return shenqingleixing;
}
public void setShenqingleixing(String shenqingleixing) {
this.shenqingleixing = shenqingleixing;
}
public String getYangpinmingcheng() {
return yangpinmingcheng;
}
public void setYangpinmingcheng(String yangpinmingcheng) {
this.yangpinmingcheng = yangpinmingcheng;
}
public String getXinghao() {
return xinghao;
}
public void setXinghao(String xinghao) {
this.xinghao = xinghao;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getJianceyiju() {
return jianceyiju;
}
public void setJianceyiju(String jianceyiju) {
this.jianceyiju = jianceyiju;
}
public List<String> getJiancexiangmu() {
return jiancexiangmu;
}
public void setJiancexiangmu(List<String> jiancexiangmu) {
this.jiancexiangmu = jiancexiangmu;
}
public String getYangpinzhuangkuang() {
return yangpinzhuangkuang;
}
public void setYangpinzhuangkuang(String yangpinzhuangkuang) {
this.yangpinzhuangkuang = yangpinzhuangkuang;
}
public String getYangpinxuhao() {
return yangpinxuhao;
}
public void setYangpinxuhao(String yangpinxuhao) {
this.yangpinxuhao = yangpinxuhao;
}
public String getYangpinfujian() {
return yangpinfujian;
}
public void setYangpinfujian(String yangpinfujian) {
this.yangpinfujian = yangpinfujian;
}
public String getYangpintijiaofangshi() {
return yangpintijiaofangshi;
}
public void setYangpintijiaofangshi(String yangpintijiaofangshi) {
this.yangpintijiaofangshi = yangpintijiaofangshi;
}
public String getYangpinbaogaotijiaofangshi() {
return yangpinbaogaotijiaofangshi;
}
public void setYangpinbaogaotijiaofangshi(String yangpinbaogaotijiaofangshi) {
this.yangpinbaogaotijiaofangshi = yangpinbaogaotijiaofangshi;
}
public String getYangpinchuzhifangshi() {
return yangpinchuzhifangshi;
}
public void setYangpinchuzhifangshi(String yangpinchuzhifangshi) {
this.yangpinchuzhifangshi = yangpinchuzhifangshi;
}
public Date getCreateDay() {
return CreateDay;
}
public void setCreateDay(Date createDay) {
CreateDay = createDay;
}
private String shenqingleixing;
private String yangpinmingcheng;
private String xinghao;
private String number;
private String jianceyiju;
private List<String> jiancexiangmu;
private String yangpinzhuangkuang;
private String yangpinxuhao;
private String yangpinfujian;
private String yangpintijiaofangshi;
private String yangpinbaogaotijiaofangshi;
private String yangpinchuzhifangshi;
private Date CreateDay;
}
DAO实现
package com.wisdombud.mongotool;
import org.springframework.data.mongodb.core.MongoOperations;
public class ProjectDaoImpl extends SimpleMongoDaoImpl<Project, String> implements ProjectDao {
public ProjectDaoImpl(MongoOperations mongoOper) {
super(mongoOper);
}
}
核心源码
package com.wisdombud.mongotool;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import common.toolkit.java.entity.PageEntity;
import common.toolkit.java.orm.hibernate.GeneralQuery;
import common.toolkit.java.util.ReflectUtils;
public class SimpleMongoDaoImpl<T, PK extends Serializable> implements SimpleMongoDao<T, PK> {
@Autowired
protected MongoOperations mongoOper;
protected Class<T> entityClazz;
/**
* 字符串分隔的标志符号,现在用:表示,在关联查询的时候用。
***/
private final String splitMarker = "::";
public SimpleMongoDaoImpl(MongoOperations mongoOper) {
this.mongoOper = mongoOper;
this.entityClazz = ReflectUtils.getSuperClassGenricType(getClass());
}
@Override
public PK save(T entity) {
return null;
}
@Override
public void update(T entity) {
mongoOper.save(entity);
}
@Override
public void saveOrUpdate(T entity) {
mongoOper.save(entity);
}
@Override
public void saveOrUpdate(Collection<T> entities) {
for (T entity : entities) {
mongoOper.save(entity);
}
}
@Override
public void delete(T entity) {
mongoOper.remove(entity);
}
@Override
public void deleteAll(Collection<T> entities) {
for (T entity : entities) {
mongoOper.remove(entity);
}
}
@Override
public void deleteById(PK id) {
mongoOper.remove(new Query(Criteria.where("_id").is(id)), entityClazz);
}
@Override
public T get(PK id) {
return mongoOper.findById(id, entityClazz);
}
@Override
public List<T> getAll() {
return this.mongoOper.findAll(entityClazz);
}
@Override
public T load(PK id) {
// TODO Auto-generated method stub
return null;
}
@Override
public void refresh(T entity) {
// TODO Auto-generated method stub
}
@Override
public void merge(T entity) {
// TODO Auto-generated method stub
}
@Override
public void merge(String entityName, T entity) {
// TODO Auto-generated method stub
}
@Override
public void evict(T entity) {
// TODO Auto-generated method stub
}
@Override
public boolean contains(Object entity) {
// TODO Auto-generated method stub
return false;
}
@Override
public List<T> findBy(String propertyName, Object value) {
HashMap<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put(propertyName, value);
return this.findBy(paramMap, null);
}
@Override
public List<T> findBy(Map<String, Object> paramMap, List<Order> orderList) {
Query query = this.buildQuery(paramMap, orderList);
return mongoOper.find(query, entityClazz);
}
@Override
public T findUniqueBy(String propertyName, Object value) {
HashMap<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put(propertyName, value);
Query query = this.buildQuery(paramMap, null);
return mongoOper.findOne(query, entityClazz);
}
@Override
public T findUniqueBy(Map<String, Object> paramMap, List<Order> orderList) {
Query query = this.buildQuery(paramMap, orderList);
return mongoOper.findOne(query, entityClazz);
}
@Override
public void pageList(Map<String, Object> paramMap, List<Order> orderList, PageEntity<T> pageEntity) {
Query query = this.buildQuery(paramMap, orderList);
pageEntity.setTotalResults(mongoOper.count(query, entityClazz));// 总记录数
pageEntity.setTotalPages();// 总页数
query.skip(pageEntity.getStart());
query.limit(pageEntity.getPageSize());
pageEntity.setResults(mongoOper.find(query, entityClazz));// 分页数据
pageEntity.setCurrentPageLength(pageEntity.getResults().size());// 当前页面记录数
}
public Class<T> getEntityClazz() {
return entityClazz;
}
public void setEntityClazz(Class<T> entityClazz) {
this.entityClazz = entityClazz;
}
protected void buildQuery(String key, Object value, Query query) {
// 如果为数字
if (key.contains("_id")) {
query.addCriteria(Criteria.where(key).is(new ObjectId(value.toString())));
} else {
if (value instanceof Integer || value instanceof Float || value instanceof Double
|| value instanceof Boolean) {
query.addCriteria(Criteria.where(key).is(value));
} else {
// 字符串 模糊查询
// i为查询大小写不敏感
query.addCriteria(Criteria.where(key).regex(value.toString(), "i"));
}
}
}
protected Query buildQuery(Map<String, Object> paramMap, List<Order> orderList) {
Query query = new Query();
if (paramMap != null && paramMap.size() > 0) {
for (String key : paramMap.keySet()) {
Object value = paramMap.get(key);
if (key.contains(this.splitMarker)) {// 处理关联查询的情况
String[] arr = key.split(this.splitMarker);
if (arr.length != 2) {
continue;
}
String p1 = arr[0];
String p2 = arr[1];
Class<T> clazz = ReflectUtils.getSuperClassGenricType(getClass());
Field[] fs = this.getAllFields(clazz);
// ReflectUtils.getAllFields(clazz);
Class<?> clazz1 = null;
for (Field f : fs) {
if (f.getName().equals(p1)) {
clazz1 = f.getType();
break;
}
}
Query query0 = new Query();
this.buildQuery(p2, value, query0);
query.addCriteria(Criteria.where(p1).in(mongoOper.find(query0, clazz1)));
} else {
this.buildQuery(key, value, query);
}
}
}
if (orderList != null && orderList.size() > 0) {
Sort sort = new Sort(orderList);
query.with(sort);
}
return query;
}
private Field[] getAllFields(Class<? super T> clazz) {
Class<?> originClazz = clazz;
Field[] resultField = clazz.getDeclaredFields();
for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
if (clazz == originClazz) {
continue;
}
try {
Field[] superFields = clazz.getDeclaredFields();
resultField = (Field[]) ArrayUtils.addAll(resultField, superFields);
} catch (Exception e) {
System.out.println("error");
}
}
return resultField;
}
@Override
public List<T> findBy(Map<String, Object> paramsMap) {
// TODO Auto-generated method stub
return null;
}
@Override
public GeneralQuery<T> createGeneralQuery() {
// TODO Auto-generated method stub
return null;
}
}
MongoDB入门(5)- 我们自己封装的MongoDB-Java版本的更多相关文章
- 算法入门 - 链表的实现及应用(Java版本)
之前我们学习了动态数组,虽然比原始数组的功能强大了不少,但还不是完全纯动态的(基于静态数组实现的).这回要讲的链表则是正儿八经的动态结构,是一种非常灵活的数据结构. 链表的基本结构 链表由一系列单一的 ...
- [MongoDB知识体系] 一文全面总结MongoDB知识体系
MongoDB教程 - Mongo知识体系详解 本系列将给大家构建MongoDB全局知识体系.@pdai MongoDB教程 - Mongo知识体系详解 知识体系 学习要点 学习资料 官网资料 入门系 ...
- .Net Core ORM选择之路,哪个才适合你 通用查询类封装之Mongodb篇 Snowflake(雪花算法)的JavaScript实现 【开发记录】如何在B/S项目中使用中国天气的实时天气功能 【开发记录】微信小游戏开发入门——俄罗斯方块
.Net Core ORM选择之路,哪个才适合你 因为老板的一句话公司项目需要迁移到.Net Core ,但是以前同事用的ORM不支持.Net Core 开发过程也遇到了各种坑,插入条数多了也特别 ...
- Nodejs学习笔记(十)--- 与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
目录 简介 MongoDB安装(windows) MongoDB基本语法和操作入门(mongo.exe客户端操作) 库操作 插入 查询 修改 删除 存储过程 nodejs操作MongoDB 插入 查询 ...
- MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshell.c ...
- Nodejs学习笔记(十)—与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
简介 MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshel ...
- MongoDB入门实践
MongoDB入门实践 简单介绍MongoDB,包括MongoDB的使用场景.和MySQL的对比.安装部署.Java客户端访问及总结 MongoDB? 我们遵循需求驱动技术的原则,通过一个场景来引入M ...
- MongoDB入门(7)- SpringDataMongoDB
入门 本文介绍如何应用SpringDataMongoDB操作实体和数据库,本文只介绍最基本的例子,复杂的例子在后面的文章中介绍. SpringDataMongoDB简介 SpringDataMongo ...
- mongodb入门学习小记
Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...
随机推荐
- LVS+Keepalive+Nginx实现负载均衡
本文参考:http://blog.csdn.net/yinwenjie/article/details/47211551 简单粗暴写一下,做备忘,刚刚搭好没做优化呢,后期补充 一.机器准备 LVS-M ...
- Python字符串格式化表达式和格式化方法
Python格式化字符串由两种方式可以选择:一种是格式化表达式(Formatting Expression),一种是格式化方法(Formatting Method).其中格式化表达式在全Python版 ...
- 下载 编译 Android源代码 和 Android kernel源代码
下载Android源码简要流程 : a. 获取repo文件: curl http://commondatastorage.googleapis.com/git-repo-downloads/repo ...
- iOS开发GCD的简单使用
- (void)viewDidLoad { [super viewDidLoad]; // gcd 可以充分调用设备的 cpu 发挥最大性能,在 C 语言基础之上封装的 // dispatch_que ...
- iOS-addSubView时给UIView添加效果
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; ...
- 数据库集群之路二 MYCAT
windows下安装配置并使用mycat 参考:http://www.cnblogs.com/parryyang/p/5758087.html 一 下载windows版本 https://github ...
- 老生常谈-从输入url到页面展示到底发生了什么
来自:咸鱼老弟 - 博客园 链接:http://www.cnblogs.com/xianyulaodi/p/6547807.html
- 简介Kafka Streams
本文从以下几个方面介绍Kafka Streams: 一. Kafka Streams 背景 二. Kafka Streams 架构 三. Kafka Streams 并行模型 四. Kafka Str ...
- 关于如何解决PHPCMS V9内容搜索显示不全问题解决方案
站长朋友们都晓得只要是开源的PHP程序都会有漏洞存在.如果想完美的建站就需要自己去研究打补丁了.最近很多站长联系小编咨询用phpcms建站当在首页搜索内容的时候有的居然搜索不到.小编感到很是奇怪于是就 ...
- Maven 3-Maven依赖版本冲突的分析及解决小结 (阿里,美团,京东面试)
举例A依赖于B及C,而B又依赖于X.Y,而C依赖于X.M,则A除引B及C的依赖包下,还会引入X,Y,M的依赖包(一般情况下了,Maven可通过<scope>等若干种方式控制传递依赖).这里 ...