IBatisNet的实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; using System.Data.SqlClient; using IBatisNet.Common; using IBatisNet.DataMapper; using IBatisNet.DataMapper.MappedStatements; using IBatisNet.DataMapper.Scope; using IBatisNet.DataMapper.Exceptions; using WHTR.Domain; using WHTR.DataTypes; using WHTR.OptimisticLocking; namespace WHTR.IBatisNet { /// <summary> /// 扩展类型。 /// </summary> public static class SqlMapExtension { /// <summary> /// 插入。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static object Insert(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.Insert(statementId, paramValue); } /// <summary> /// 更新。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static int Update(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.Update(statementId, paramValue); } /// <summary> /// 删除。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static int Delete(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.Delete(statementId, paramValue); } /// <summary> /// 标量查询。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static object ExecuteScalar(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.QueryForObject(statementId, paramValue); } /// <summary> /// 标量查询。 /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static T ExecuteScalar<T>(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.QueryForObject<T>(statementId, paramValue); } /// <summary> /// 单记录查询。 /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static T Get<T>(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.QueryForObject<T>(statementId, paramValue); } /// <summary> /// 多记录查询。 /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <returns></returns> public static IList<T> GetList<T>(this ISqlMapper mapper, string statementId) { return mapper.QueryForList<T>(statementId, null); } /// <summary> /// 多记录查询。 /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static IList<T> GetList<T>(this ISqlMapper mapper, string statementId, object paramValue) { return mapper.QueryForList<T>(statementId, paramValue); } /// <summary> /// 获取 SQL 字符串。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="statementId">The statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static string GetSql(this ISqlMapper mapper, string statementId, object paramValue) { IMappedStatement statement = mapper.GetMappedStatement(statementId); if (!mapper.IsSessionStarted) { mapper.OpenConnection(); } RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramValue, mapper.LocalSession); return scope.PreparedStatement.PreparedSql; } /// <summary> /// 批量提交。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="action">The action.</param> public static void Commit(this ISqlMapper mapper, Action action) { Commit(mapper, action, null); } /// <summary> /// 批量提交,可自定义抛出的异常信息。 /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="action">The action.</param> /// <param name="errorMessage">The error message.</param> /// <exception cref="System.ApplicationException"></exception> public static void Commit(this ISqlMapper mapper, Action action, string errorMessage) { try { mapper.BeginTransaction(); action(); mapper.CommitTransaction(); } catch (DataMapperException ex) { mapper.RollBackTransaction(); if (null == errorMessage) { throw ex; } else { throw new ApplicationException(errorMessage, ex); } } } /// <summary> /// Gets the list with page. /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="pagerStatementId">The pager statement identifier.</param> /// <param name="pagerCountStatementId">The pager count statement identifier.</param> /// <param name="paramValue">The parameter value.</param> /// <returns></returns> public static IPagedResult<T> GetListWithPage<T>(this ISqlMapper mapper, string pagerStatementId, string pagerCountStatementId, IPageQueryParam paramValue) { var totalCount = mapper.QueryForObject<int>(pagerCountStatementId, paramValue); var lst = mapper.QueryForList<T>(pagerStatementId, paramValue); return new PagedResult<T>(lst, paramValue.PageIndex, paramValue.PageSize, totalCount); } /// <summary> /// Gets the list with page. /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="pagerStatementId">The pager statement identifier.</param> /// <param name="pagerCountStatementId">The pager count statement identifier.</param> /// <param name="pageIndex">Index of the page.</param> /// <param name="pageSize">Size of the page.</param> /// <param name="paramValue">The parameter value.</param> /// <param name="startIndexKeyName">Start name of the index key.</param> /// <param name="endIndexKeyName">End name of the index key.</param> /// <returns></returns> public static IPagedResult<T> GetListWithPage<T>( this ISqlMapper mapper, string pagerStatementId, string pagerCountStatementId, int pageIndex, int pageSize, Hashtable paramValue, string startIndexKeyName = "StartIndex", string endIndexKeyName = "EndIndex") { Hashtable newParamValue = new Hashtable(); var pageQueryParam = new PageQueryParam(pageIndex, pageSize); newParamValue.Add(startIndexKeyName, pageQueryParam.StartIndex); newParamValue.Add(endIndexKeyName, pageQueryParam.EndIndex); if (paramValue != null) { foreach (DictionaryEntry item in paramValue) { newParamValue.Add(item.Key, item.Value); } } var totalCount = mapper.QueryForObject<int>(pagerCountStatementId, pageQueryParam); var lst = mapper.QueryForList<T>(pagerStatementId, pageQueryParam); return new PagedResult<T>(lst, pageIndex, pageSize, totalCount); } /// <summary> /// 获取乐观锁 /// </summary> /// <param name="mapper">mapper</param> /// <param name="table">表名</param> /// <param name="versionField">"版本号"的字段名</param> /// <param name="primaryKeyField">"标识主键"的字段名</param> /// <param name="primaryKeyValue">"标识主键"的值</param> /// <returns></returns> public static ILocker GetLocker( this ISqlMapper mapper, string table, string versionField, string primaryKeyField, int primaryKeyValue) { var currentVersion = 0; using (var connection = new SqlConnection(mapper.DataSource.ConnectionString)) { connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandText = string.Format("SELECT {0} FROM {1} WHERE {2} = {3}", versionField, table, primaryKeyField, primaryKeyValue); var reader = cmd.ExecuteReader(); while (reader.Read()) { currentVersion = int.Parse(reader[0].ToString()); } } return new IBatisNetLocker(mapper, table, currentVersion, versionField, primaryKeyValue, primaryKeyField); } /// <summary> /// Updates the with concurrency check. /// </summary> /// <param name="mapper">The mapper.</param> /// <param name="statementId">Name of the statement.</param> /// <param name="entity">The entity.</param> /// <exception cref="OptmisticConcurrencyException">并发异常</exception> public static void UpdateWithConcurrencyCheck(this ISqlMapper mapper, string statementId, IVersionable entity) { var result = mapper.Update(statementId, entity); if (result == 0) { throw new OptmisticConcurrencyException("并发异常"); } entity.Version++; } } }
个人主要研究:金融系统、MIS系统、人力资源管理系统、数据采集系统、权限管理系统等等系统。主攻C#开发语言,Oracle、Sql Server,WCF和Remoting通信。
如需联系可加QQ:442389681 Email:lxc880615@163.com 手机:18922735098
QQ群交流:186841119 (请注明来自博客园)
博客园地址:http://www.cnblogs.com/jara/ http://www.cnblogs.com/luoyuhao/
提示:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。
如果对文章有任何问题,都可以在评论中留言,我会尽可能的答复您,谢谢您的阅读