Nhibernate实现类似 BeanPropertyRowMapper 的类型映射
来源:互联网 发布:rip软件是什么意思 编辑:程序博客网 时间:2024/05/17 00:07
Nhibernate 通过sql 查询映射到自定义类时要求,类名和sql语句完全匹配,有时候,数据库字段和属性确实不一样,比如加下划线区分的这种,为了适应情况,我实现了个接口类
来。
/// <summary>
/// 解决数据库字段与类属性大小写不一致问题
/// </summary>
public class BeanTransformerAdapter<T> : IResultTransformer
{
ILog log = LogManager.GetLogger(typeof(BeanTransformerAdapter<T>));
public BeanTransformerAdapter(Type result)
{ }
public BeanTransformerAdapter()
{
initialize(typeof(T));
}
public BeanTransformerAdapter(Type result, bool checkFullyPopulated):this(result)
{
this.checkFullyPopulated = checkFullyPopulated;
}
// protected readonly static Log Logger= LogFactory.getLog(typeof(BeanTransformerAdapter));
/** The class we are mapping to */
private Type mappedClass;
/** Whether we're strictly validating */
private bool checkFullyPopulated = false;
/** Whether we're defaulting primitives when mapping a null value */
private bool primitivesDefaultedForNullValue = false;
/** Map of the fields we provide mapping for */
private Dictionary<String, PropertyInfo> mappedFields;
/** Set of bean properties we provide mapping for */
private HashSet<String> mappedProperties;
protected void initialize(Type mappedClass)
{
this.mappedClass = mappedClass;
this.mappedFields = new Dictionary<String, PropertyInfo>();
this.mappedProperties = new HashSet<String>();
PropertyInfo[] pds = mappedClass.GetProperties().Where(p => p.GetMethod.IsVirtual).ToArray();
foreach (PropertyInfo pd in pds)
{
//if (pd.getWriteMethod() != null)
//{
this.mappedFields.Add(pd.Name.ToLowerInvariant(), pd);
String underscoredName = UnderscoreName(pd.Name);
if (!pd.Name.ToUpperInvariant().Equals(underscoredName) && !string.IsNullOrEmpty(underscoredName))
{
this.mappedFields.Add(underscoredName, pd);
}
this.mappedProperties.Add(pd.Name);
//}
}
}
private String UnderscoreName(String name)
{
if (!string.IsNullOrEmpty(name))
{
return "";
}
StringBuilder result = new StringBuilder();
result.Append(name.Substring(0, 1).ToUpperInvariant());
for (int i = 1; i < name.Length; i++)
{
String s = name.Substring(i, i + 1);
String slc = s.ToLowerInvariant();
if (!s.Equals(slc))
{
result.Append("_").Append(slc);
}
else
{
result.Append(s);
}
}
return result.ToString();
}
//public IList TransformList(IList collection)
//{
// throw new NotImplementedException();
//}
//public object TransformTuple(object[] tuple, string[] aliases)
//{
// throw new NotImplementedException();
//}
private Type result;
private List<PropertyInfo> properties = new List<PropertyInfo>();
public IList TransformList(IList collection)
{
return collection;
}
public BeanTransformerAdapter(Type result, params string[] names)
{
this.result = result;
foreach (string name in names)
{
properties.Add(result.GetProperty(name));
}
}
public object TransformTuple(object[] tuple, string[] aliases)
{
//object instance = Activator.CreateInstance(result);
//for (int i = 0; i < tuple.Length; i++)
//{
// properties[i].SetValue(instance, tuple[i], null);
//}
//return instance;
HashSet<String> populatedProperties = new HashSet<String>() ;
Object mappedObject = Activator.CreateInstance<T> ();
for (int i = 0; i < aliases.Length; i++)
{
String column = aliases[i];
var pName = column.Replace("_", "").Replace(" ", "").ToLowerInvariant();
if (!this.mappedFields.Keys.Contains(pName)) continue;
PropertyInfo pd = this.mappedFields[pName];
if (pd != null)
{
try
{
Object value = tuple[i];
try
{
pd.SetValue(mappedObject, tuple[i], null);
}
catch (TypeMismatchException e)
{
if (value == null && primitivesDefaultedForNullValue)
{
log.Debug("Intercepted TypeMismatchException for column " + column + " and column '"
+ column + "' with value " + value + " when setting property '" + pd.Name + "' of type " + pd.GetType()
+ " on object: " + mappedObject);
}
else
{
throw e;
}
}
if (populatedProperties != null)
{
populatedProperties.Add(pd.Name);
}
}
catch (Exception ex)
{
throw new Exception("Unable to map column " + column
+ " to property " + pd.Name, ex);
}
}
}
if (populatedProperties != null && !populatedProperties.Equals(this.mappedProperties))
{
log.Info(@"Given ResultSet does not contain all fields "
+ "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
//匹配不上的暂时不处理
// throw new Exception("Given ResultSet does not contain all fields "
//+ "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
}
return mappedObject;
}
}
--------------------------------------------------------------------------------------
使用:
var session = SessionFactory.OpenSession();
var queryString = "select * from T"
var rslt1 = session.CreateSQLQuery(queryString).SetString("Id", orderId).SetResultTransformer(new BeanTransformerAdapter<DaoModel.ProcessOrderModel>()).List<DaoModel.ProcessOrderModel>();
- Nhibernate实现类似 BeanPropertyRowMapper 的类型映射
- Spring2.5 BeanPropertyRowMapper ResultSet和实体类的字段进行实现自动映射
- NHibernate日期类型的映射(转)
- fluent nhibernate映射的数值类型问题
- NHibernate映射关系表(实体映射文件字段的属性对应的NHibernate类型)
- NHibernate VS .NET VS DbType 的类型映射关系
- NHibernate学习手记(6) - 实现one2many/many2one的映射
- NHibernate学习手记(6) - 实现one2many/many2one的映射
- NHibernate学习手记(6) - 实现one2many/many2one的映射
- NHibernate如何实现对象与数据库之间的映射
- NHibernate多对多关联映射的实现
- BeanPropertyRowMapper
- NHibernate Step By Step(7)-映射集合类型
- 解决NHibernate.net对Sql2005中timestamp类型映射问题
- nhibernate学习之简单组合的映射
- nhibernate学习之简单组合的映射
- Nhibernate映射类的动态绑定
- NHibernate配置文件和映射文件的详解
- 在windwos环境下用gcc编译boost动态库
- myblog 中的bug处理
- ros相关
- ERROR: Unknown command 'crunch' 的解决方法
- iOS 内购最新讲解
- Nhibernate实现类似 BeanPropertyRowMapper 的类型映射
- 快速开始,第一个量子程序
- Java 序列化Serializable详解
- css-box盒子模型
- OpenCV3 Java 机器学习使用方法汇总
- SSM参数绑定机制
- Spring思维导图,让Spring不再难懂
- 交换两个数
- 日常bug整理