博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring.net +dapper 打造简易的DataAccess 工具类.
阅读量:5344 次
发布时间:2019-06-15

本文共 5189 字,大约阅读时间需要 17 分钟。

1 public class DBUtil  2     {  3         ///   4         /// 数据库连接字符串  5         ///   6         private static string DataBase_Connection = System.Configuration.ConfigurationManager.ConnectionStrings["DNT"].ToString();  7   8         ///   9         /// 脚本文件存放路径 10         ///  11         private static string Data_File_Path = System.Configuration.ConfigurationManager.AppSettings["Data_File_Path"].ToString(); 12  13         ///  14         /// 全局配置文件列表 15         ///  16         public static Dictionary
DataConfigList; 17 18 ///
19 /// 加载配置文件 20 /// 21 public static void InitConfig() 22 { 23 var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,Data_File_Path); 24 25 DirectoryInfo TheFolder = new DirectoryInfo(filePath); 26 foreach (FileInfo NextFile in TheFolder.GetFiles().Where(f=>f.Extension == ".config")) 27 { 28 Spring.Context.Support.XmlApplicationContext context = new Spring.Context.Support.XmlApplicationContext(NextFile.FullName); 29 string[] fileNames = context.ObjectFactory.GetObjectDefinitionNames(); 30 31 foreach (var fileName in fileNames) 32 { 33 var mObj = context.GetObject(fileName); 34 35 if (DataConfigList == null) 36 { 37 DataConfigList = new Dictionary
(); 38 } 39 40 string keyName = NextFile.Name.Split('.')[0].ToString() + "." + fileName; 41 42 if (!DataConfigList.ContainsKey(keyName)) 43 { 44 DataConfigList.Add(keyName, mObj as DataConfig); 45 } 46 else 47 { 48 throw new ApplicationException("The existence of multiple identical configuration node,plseae check!"); 49 } 50 } 51 } 52 53 } 54 55 ///
56 /// 获取指定名称SQL 57 /// 58 ///
59 ///
60 public static string GetSql(string commandName) 61 { 62 InitConfig(); 63 64 if (!DataConfigList.ContainsKey(commandName)) 65 { 66 throw new ApplicationException("Data file can't be found!"); 67 } 68 else 69 { 70 return DataConfigList[commandName].commandText; 71 } 72 73 } 74 75 ///
76 /// 获取数据库连接 77 /// 78 ///
79 public static SqlConnection GetConnection() 80 { 81 SqlConnection connection = new SqlConnection(DataBase_Connection); 82 connection.Open(); 83 84 return connection; 85 } 86 87 ///
88 /// 获取Command 89 /// 90 ///
91 ///
92 public static IDbCommand CreateCommand(string commandName) 93 { 94 var connection = GetConnection(); 95 96 using (IDbCommand command = connection.CreateCommand()) 97 { 98 command.CommandText = GetSql(commandName); 99 return command;100 }101 }102 103 ///
104 /// 执行查询动作105 /// 106 ///
107 ///
108 ///
109 ///
110 public static List
Query
(string commandName, dynamic pars)111 {112 using (IDbCommand command = DBUtil.CreateCommand(commandName))113 {114 return SqlMapper.Query
(command.Connection, command.CommandText, pars);115 }116 }117 }

SQL配置文件管理

  • 支持多文件 (Order.config,  Item.config....), DBUtil 会在调用时加配置文件加入内存中缓存,待下次时候,直接读取内存数据.
1 
2
6
7
8
9 12
13
14
15 16
17
18
19 22
23
24
25

 

Demo代码

class Program    {        static void Main(string[] args)        {            List
OrderList = GetOrderList(1000); OrderList.ForEach(f => { Console.WriteLine(f.OrderNumber); }); Console.ReadLine(); } public static List
GetOrderLIST(int size) { return DBUtil.Query
("Order.GetOrderList", new { Size = size }).ToList(); }
public static List
GetOrderDetail(int size,int orderId) { return DBUtil.Query
("Order.GetOrderDetail", new { Size = size,OrderID = orderId }).ToList(); }
}

 

转载于:https://www.cnblogs.com/darjuan/p/3792391.html

你可能感兴趣的文章
解决VS+QT无法生成moc文件的问题
查看>>
AngularJs练习Demo14自定义服务
查看>>
stat filename
查看>>
关于空想X
查看>>
CF1067C Knights 构造
查看>>
[BZOJ2938] 病毒
查看>>
webstorm修改文件,webpack-dev-server不会自动编译刷新
查看>>
Scikit-learn 库的使用
查看>>
CSS: caption-side 属性
查看>>
python 用数组实现队列
查看>>
认证和授权(Authentication和Authorization)
查看>>
Mac上安装Tomcat
查看>>
CSS3中box-sizing的理解
查看>>
传统企业-全渠道营销解决方案-1
查看>>
Lucene全文检索
查看>>
awk工具-解析1
查看>>
推荐一款可以直接下载浏览器sources资源的Chrome插件
查看>>
CRM product UI里assignment block的显示隐藏逻辑
查看>>
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告...
查看>>
AMH V4.5 – 基于AMH4.2的第三方开发版
查看>>