In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the iBATIS.NET field mapping custom object, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
What does iBATIS.NET field mapping mean? In iBATIS.NET, the result of the query automatically maps each field to an attribute value in Domain, which is done through the TypeHandlerFactory class, and some relationships between system classes and type conversion classes are registered at program initialization:
Handler = new NullableBooleanTypeHandler (); this.Register (typeof (bool), handler); handler = new NullableByteTypeHandler (); this.Register (typeof (byte?), handler); handler = new NullableCharTypeHandler (); this.Register (typeof (char?), handler); handler = new NullableDateTimeTypeHandler (); this.Register (typeof (DateTime?), handler); handler = new NullableDecimalTypeHandler (); this.Register (typeof (decimal?), handler); handler = new NullableDoubleTypeHandler (); this.Register (typeof (double?), handler) Handler = new NullableGuidTypeHandler (); this.Register (typeof (Guid), handler); handler = new NullableInt16TypeHandler (); this.Register (typeof (Int16?), handler); handler = new NullableInt32TypeHandler (); this.Register (typeof (Int32?), handler); handler = new NullableInt64TypeHandler (); this.Register (typeof (Int64?), handler); handler = new NullableSingleTypeHandler (); this.Register (typeof (Single?), handler); handler = new NullableUInt16TypeHandler (); this.Register (typeof (UInt16?), handler) Handler = new NullableUInt32TypeHandler (); this.Register (typeof (UInt32), handler); handler = new NullableUInt64TypeHandler (); this.Register (typeof (UInt64?), handler); handler = new NullableSByteTypeHandler (); this.Register (typeof (SByte?), handler); handler = new NullableTimeSpanTypeHandler (); this.Register (typeof (TimeSpan?), handler)
So what if we want to map a field in the database to a class of our own and do some personalization in that class?
Originally, I wanted to write my own type handling class after the StringTypeHandler class, but by looking at the source code of iBATIS, even if I wrote my own type handling class, I couldn't seem to find the registered interface (if any brother found the interface, please let me know)
Another way is to implement the ITypeHandlerCallback interface in the registered CustomTypeHandler type, as shown below:
What I'm implementing here is a demo program that demonstrates mapping the Account_LastName and Account_Email fields in the database to custom Property types and putting them into a Hashtable.
IBATIS.NET field mapping 1,
Customize the Property class
Namespace GSpring.Common {public class Property {private string _ dataValue; public string DataValue {get {return _ dataValue;} set {_ dataValue = value;}} private string _ dataType; public string DataType {get {return _ dataType;} set {_ dataType = value }}
IBATIS.NET field mapping 2.
Classes that implement the ITypeHandlerCallback interface
Namespace GSpring.Common {public sealed class PropertyTypeHandler: ITypeHandlerCallback {public object ValueOf (string Value) {Property obj = new Property (); obj.DataValue = Value; return obj;} public object GetResult (IResultGetter getter) {Property obj = new Property () If (getter.Value! = null & & getter.Value! = System.DBNull.Value) {obj.DataValue = (string) getter.Value;} return obj;} public void SetParameter (IParameterSetter setter, object parameter) {setter.Value = ((Property) parameter) .DataValue } public object NullValue {get {return null;}}
It is mainly one of the GetResult and SetParameter methods to realize the access operation with the database.
IBATIS.NET field mapping 3.
Modify the corresponding Domain class to add two properties:
Public Hashtable ht = new Hashtable (); Property _ emailAddress1 = new Property (); public Property EmailAddress1 {get {return _ emailAddress1;} set {_ emailAddress1.DataType = "string"; _ emailAddress1.DataValue = value.DataValue; ht ["email"] = _ emailAddress1;}} Property _ lastName1 = new Property () Public Property LastName1 {get {return _ lastName1;} set {_ lastName1.DataType = "string"; _ lastName1.DataValue = value.DataValue; ht ["name"] = _ lastName1;}}
IBATIS.NET field mapping 4.
Modify the configuration file:
< resultMap id= "account-result" class= "Account" > < result property= "Id" column= "Account_ID" / > < result property= "FirstName" column= "Account_FirstName" / > < result property= "LastName1" column= "Account_LastName" typeHandler= "GSpring.Common.PropertyTypeHandler" / > < result property= "EmailAddress1" column= "Account_Email" typeHandler= "GSpring.Common.PropertyTypeHandler" / > < / resultMap >
The typeHandler attribute is mainly used to specify a type converter.
This is the answer to the question on how to analyze the iBATIS.NET field mapping custom object. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.