private Degree _degree; public Degree Degree { get { return _degree; } set { _degree = value; } }
(和第三种方法一样) 然后:修改配置文件,这也是最重要的地方(PS:IBatis.Net中的配置文件真的很强) 在 resultMaps节加入: < resultMap id = " com2result " class = " Account " > < result property = " Id " column = " Account_ID " /> < result property = " FirstName " column = " Account_FirstName " /> < result property = " LastName " column = " Account_LastName " /> < result property = " EmailAddress " column = " Account_Email " nullValue = " no_email@provided.com " /> < result property = " Degree " resultMapping = " Account.Degree-result " /> </ resultMap > < resultMap id = " Degree-result " class = " Degree " > < result property = " Id " column = " Account_ID " /> < result property = " DegreeName " column = " DegreeName " /> </ resultMap >
这里最主要的就是使用了resultMapping属性, resultMapping="Account.Degree-result",其中Account是当前配置文件的namespace:<sqlMap namespace="Account" ...... 在 statements节加入: < select id = " GetCom2Tables " resultMap = " com2result " > select Accounts. * , Degree. * from Accounts,Degree where Accounts.Account_ID = Degree.Account_ID </ select >
这样就可以随心所欲的写自己需要的sql,性能也很好,不会出现第三种方法中的1+n条的查询语句了。