spring.net +dapper 打造简易的DataAccess 工具类.
public class DBUtil
{
/// <summary>
/// 数据库连接字符串
/// </summary>
private static string DataBase_Connection = System.Configuration.ConfigurationManager.ConnectionStrings["DNT"].ToString(); /// <summary>
/// 脚本文件存放路径
/// </summary>
private static string Data_File_Path = System.Configuration.ConfigurationManager.AppSettings["Data_File_Path"].ToString(); /// <summary>
/// 全局配置文件列表
/// </summary>
public static Dictionary<string, DataConfig> DataConfigList; /// <summary>
/// 加载配置文件
/// </summary>
public static void InitConfig()
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,Data_File_Path); DirectoryInfo TheFolder = new DirectoryInfo(filePath);
foreach (FileInfo NextFile in TheFolder.GetFiles().Where(f=>f.Extension == ".config"))
{
Spring.Context.Support.XmlApplicationContext context = new Spring.Context.Support.XmlApplicationContext(NextFile.FullName);
string[] fileNames = context.ObjectFactory.GetObjectDefinitionNames(); foreach (var fileName in fileNames)
{
var mObj = context.GetObject(fileName); if (DataConfigList == null)
{
DataConfigList = new Dictionary<string, DataConfig>();
} string keyName = NextFile.Name.Split('.')[].ToString() + "." + fileName; if (!DataConfigList.ContainsKey(keyName))
{
DataConfigList.Add(keyName, mObj as DataConfig);
}
else
{
throw new ApplicationException("The existence of multiple identical configuration node,plseae check!");
}
}
} } /// <summary>
/// 获取指定名称SQL
/// </summary>
/// <param name="commandName"></param>
/// <returns></returns>
public static string GetSql(string commandName)
{
InitConfig(); if (!DataConfigList.ContainsKey(commandName))
{
throw new ApplicationException("Data file can't be found!");
}
else
{
return DataConfigList[commandName].commandText;
} } /// <summary>
/// 获取数据库连接
/// </summary>
/// <returns></returns>
public static SqlConnection GetConnection()
{
SqlConnection connection = new SqlConnection(DataBase_Connection);
connection.Open(); return connection;
} /// <summary>
/// 获取Command
/// </summary>
/// <param name="commandName"></param>
/// <returns></returns>
public static IDbCommand CreateCommand(string commandName)
{
var connection = GetConnection(); using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = GetSql(commandName);
return command;
}
} /// <summary>
/// 执行查询动作
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="command"></param>
/// <param name="pars"></param>
/// <returns></returns>
public static List<T> Query<T>(string commandName, dynamic pars)
{
using (IDbCommand command = DBUtil.CreateCommand(commandName))
{
return SqlMapper.Query<T>(command.Connection, command.CommandText, pars);
}
}
}
SQL配置文件管理
- 支持多文件 (Order.config, Item.config....), DBUtil 会在调用时加配置文件加入内存中缓存,待下次时候,直接读取内存数据.
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">
<object id="GetOrderList" type="SpringDemo.DataConfig,SpringDemo">
<property name="commandText">
<value>
<![CDATA[
SELECT TOP(@Size) * FROM dbo.Order
]]>
</value>
</property>
</object> <object id="GetOrderDetail" type="SpringDemo.DataConfig,SpringDemo">
<property name="commandText">
<value>
<![CDATA[
SELECT TOP(@Size) * FROM dbo.OrderDetail WHERE OrderID = @OrderID
]]>
</value>
</property>
</object>
</objects>
Demo代码
class Program
{
static void Main(string[] args)
{
List<Order> OrderList = GetOrderList(); OrderList.ForEach(f =>
{
Console.WriteLine(f.OrderNumber);
}); Console.ReadLine();
} public static List<Order> GetOrderLIST(int size)
{
return DBUtil.Query<Order>("Order.GetOrderList", new { Size = size }).ToList();
}
public static List<OrderDetail> GetOrderDetail(int size,int orderId)
{
return DBUtil.Query<OrderDetail>("Order.GetOrderDetail", new { Size = size,OrderID = orderId }).ToList();
}
}
spring.net +dapper 打造简易的DataAccess 工具类.的更多相关文章
- spring 中 PO与DTO相互转换的工具类
public class BeanMapper { /** * 持有Dozer单例, 避免重复创建DozerMapper消耗资源. */ private static DozerBeanMapper ...
- spring框架中的一个字符串的工具类
stringutils.hasText("字符串") 如果字符串里面的值为null, "", " ",那么返回值为false:否则为tr ...
- Spring 常用的一些工具类
学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...
- spring中常用工具类介绍
http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作 Spring 定义了一个 org.springframework.core.io ...
- Spring 的优秀工具类盘点
文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...
- spring常用的工具类
spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 而不想把一个大大的spring工程给引入 ...
- Spring 的优秀工具类盘点第 2 部分
特殊字符转义 由于 Web 应用程序需要联合使用到多种语言,每种语言都包含一些特殊的字符,对于动态语言或标签式的语言而言,如果需要动态构造语言的内容时,一个我们经常会碰到的问题就是特殊字符转义的问题. ...
- Spring的常用工具类
官方文档 http://projects.spring.io/spring-framework https://docs.spring.io/spring/docs/current/javadoc-a ...
- Spring中内置的一些工具类
学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...
随机推荐
- 为什么使用 Redis及其产品定位 (转载自http://www.infoq.com/cn/articles/tq-why-choose-redis)
传统MySQL+Memcached架构遇到的问题 实际MySQL 是适合进行海量存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不 ...
- MSSQL 所有表中模糊查询
--搜索指定数据在那个对象中存在.txt(所有表中模糊查询) CREATE PROC sp_ValueSearch @value sql_variant, --要搜索的数据 @precision bi ...
- C# 乘法口诀表的实现方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 乘法运算 ...
- C# 调用系统API 内核 简单样例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- 检测openOffice关闭 自动重启
参考http://my.oschina.net/UpBoy/blog/301193 bat解释:循环检测任务列表找到soffice.exe,没找到就调用run.bat @echo off :l ...
- 7.css盒模型
所谓的盒模型,其实就是把元素当成盒子,元素里的文本就是盒子里的东西. 而根据元素的特效,其盒模型的特效也不同,下面是一些总结: 1.块级元素(区块) 所谓块级元素,就是能够设置元素尺寸.有隔离其他元素 ...
- C# 处理csv格式的Excel文件代码
public class CSVFileHelper { /// <summary> /// 将DataTable中数据写入到CSV文件中 /// </summary> /// ...
- C#模糊查询绑定datagridview
private CollectionViewSource wgdData = new CollectionViewSource(); private DataTable Ds_wgd { get { ...
- Python pexpect出现错误‘module have no attribute "spawn" 解决办法
今天我遇到了这个错误,现在将错误总结如下: 1.首先查询一下自己的操作系统,pexpect中的spawn()和run()仅仅运行在POSIX系统上,在WINDOWS下是没有这两个东西的,在官网http ...
- SQL注入式攻击
百度百科:http://baike.baidu.com/link?url=GQbJ2amTzTahZA7XJSBDLYYkN3waQ9JCoJ0l--tCWlvKQibe0YaH4hpmgEnLyn0 ...