CJCMS系列--持久层对MangoDB的支持
持久层添加对MangoDB数据库的支持
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CJCMS.Data;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using System.Linq.Expressions; namespace CJCMS.Data
{
public class MangoDBRepository<T> : IRepository<T> where T : IEntity
{ MongoCollection<T> _session = null; public MangoDBRepository()
{
//读配置
string connectionString = "mongodb://localhost"; MongoClient client = new MongoClient(connectionString); var server = client.GetServer(); //读配置
var database = server.GetDatabase("test"); //获取T的名称
_session = database.GetCollection<T>("tableName");
} public void Add(T entity)
{
_session.Insert(entity);
} public IQueryable<T> Table { get { return _session.AsQueryable<T>(); } } public void Update(T entity)
{
_session.Save(entity);
} public void Save(T entity)
{
_session.Save(entity);
} public void Delete(T entity)
{
var query = Query<T>.EQ(e => e.Id, entity.Id);
_session.Remove(query);
} public void Delete(string id)
{
var query = Query<T>.EQ(e => e.Id, id);
_session.Remove(query);
} public T GetByKey(string id)
{
var query = Query<T>.EQ(e => e.Id, id);
T t = _session.FindOneAs<T>(query);
return t;
} public int Count(Expression<Func<T, bool>> predicate)
{
return Table.Count(predicate);
} private IQueryable<T> FetchQuery(Expression<Func<T, bool>> predicate)
{
return Table.Where(predicate);
} private IQueryable<T> FetchQuery(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order, out string OrderName)
{
var orderable = new Orderable<T>(FetchQuery(predicate));
order(orderable);
OrderName = orderable.OrderName;
return orderable.Queryable;
} public IList<T> Fetch(Expression<Func<T, bool>> predicate)
{
object t = null; if (t == null)
{
t = FetchQuery(predicate).ToList();
return (IList<T>)t;
}
else
{
return (IList<T>)t;
}
} public IList<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order)
{
object t = null;
string key = string.Empty;
IQueryable<T> q = FetchQuery(predicate, order, out key);
if (t == null)
{
t = q.ToList();
return (IList<T>)t;
}
else
{
q = null;
return (IList<T>)t;
}
} public IList<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order, int index, int count)
{
object t = null;
string key = string.Empty;
IQueryable<T> q = FetchQuery(predicate, order, out key); if (t == null)
{
t = q.Skip(index * count).Take(count).ToList(); return (IList<T>)t;
}
else
{
q = null;
return (IList<T>)t;
}
} public void Persist()
{ }
}
}
CJCMS系列--持久层对MangoDB的支持的更多相关文章
- Spring集成MyBatis持久层框架
一.MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的JDBC代码和手动设置参数以及获取结果集,可以使用简单的XML ...
- MyBatis 是一款优秀的持久层框架
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...
- Mybatis详解系列(一)--持久层框架解决了什么及如何使用Mybatis
简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...
- 对spring 对持久层的支持和数据库连接池的理解
1.spring对持久层的支持在于,得到数据库连接之后操作上的封装,将操作简化了.也就是说以后操作sql语句就用XXXTemplate(就是一个工具类)对象了. 2.数据库连接池的作用只在于得到数据库 ...
- MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用 ...
- MyBatis知多少(7)持久层
持久层是适合使用MyBatis的地方.在面向对象的系统中,持久层主要关注对象(或者更精确地说应该是存储在那些对象中的数据)的存取.在企业应用程序中持久层通常用关系数据库系统来存储数据,虽然某些情况下其 ...
- .NET平台下,关于数据持久层框架
在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...
- .NET开源项目介绍及资源推荐:数据持久层
在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...
- MyBatis持久层框架学习之01 MyBatis的起源和发展
一.MyBatis的简介 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyB ...
随机推荐
- kali linux之窥看女神上网隐私(ettercap+wireshark+zenmap +dsniff)
作者:小波 http://www.cnblogs.com/xiaobo-Linux/ 无聊就玩了玩,不要干一些坏事哟~~网上也有一些文章关于kali linux的,就实战了一番.kali是用的debi ...
- OpenStack Mitaka 版本中的 domain 和 admin
OpenStack 的 Keystone V3 中引入了 Domain 的概念.引入这个概念后,关于 admin 这个role 的定义就变得复杂了起来. 本文测试环境是社区 Mitaka 版本. 1. ...
- nodejs核心模块之http
http模块包含以下5个核心类和方法及属性: 核心类 1,http.Agent 2,http.ClientRequest 3,http.Server 4,http.ServerResponse 5,h ...
- 初识NodeJS
1.JavaScript 模块化规范 浏览器环境 AMD Asynchronous Module Definition RequireJS CMD Common Module Definition S ...
- [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- AppBox_v2.0完整版免费下载,暨AppBox_v3.0正式发布!
文章更新: AppBox v6.0中实现子页面和父页面的复杂交互 AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. AppBox ...
- 如何为Surface Dial设备开发自定义交互功能
随着Surface Studio的发布,微软发布了与之相配套的外设硬件Surface Dial,用户可以将Surface Dail吸附在Surface Studio的屏幕上面,用旋转和点击的实体操作来 ...
- 在Eclipse中使用建立使用Gradle做依赖管理的Spring Boot工程
前述: Gradle存在很长时间了,以前只知道Maven和ivy ,最近才知道有这个存在,因为以后要用这个了; 所以,要先学会怎么用这个工具,就从建立一个简单工程开始! 实际上以前是见过Gradle的 ...
- bootstrop 日期控件 datepicker被弹出框dialog覆盖的解决办法
筒子们在使用bootstrap的日期控件(datepicker , 现在官网提供的名称叫 datetimepicker)时可能会遇到如上图的问题这是啥原因造成的呢? 答案很简单时输出的优先级造成的(z ...