Ibatis.Net 输出SQL语句学习(七)

一、IBatis.net输出SQL语句

输出IBatis.net生成的SQL语句,能够方便调试。

在MapperHelper类中添加GetSql方法:

        /// <summary>
        /// 返回执行的Sql语句
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public static string GetSql(string statementName, object paramObject)
        {
            IMappedStatement statement = MapperHelper.Instance().GetMappedStatement(statementName);
            RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, MapperHelper.Instance().CreateSqlMapSession());
            return scope.PreparedStatement.PreparedSql;
        }

在执行DAO方法之前添加:

  public IList<PersonModel> SelectPersonById(Hashtable ht)
        {
            string sql = MapperHelper.GetSql("SelectPersonById", ht);
            IList<PersonModel> pList = mapper.QueryForList<PersonModel>("SelectPersonById", ht);
            return pList;
        }

在执行之前,得到执行Sql,可以记录日志方便调试等操作。

参考:http://www.cnblogs.com/caoyc/category/873268.html