最近做一个小项目,用到IBatis.net ,因为是现学现做,所以多少有点吃力,之前遇到一个问题,就是将List<T>作为parameterClass,
网上各种找,无奈.net方面的甚少,自己新手,身边又无高手指教,本来都想转换思路了,无意中却成功了。。。真是惊喜!
相关部分必要代码
实体类文件:Map.cs与UserInfo.cs
class="code_img_closed" src="/Upload/Images/2013101211/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('2d65652c-ac67-49be-9bc0-410c408c4a07',event)" src="/Upload/Images/2013101211/2B1B950FA3DF188F.gif" alt="" />1 [Serializable] 2 public class Map 3 { 4 public string UserID { get; set; } 5 public string UserGroup { get; set; } 6 }View Code
1 [Serializable] 2 public class UserInfo 3 { 4 public string UserID { get; set; } 5 6 public string CardWord { get; set; } 7 8 public string UserName { get; set; } 9 10 public string PassWord { get; set; } 11 12 public string UserPicture { get; set; } 13 14 public string UserMail { get; set; } 15 16 public DateTime RegTime { get; set; } 17 18 public string UserStatus { get; set; } 19 }View Code
xml文件中的配置,此处只给出涉及到的必要代码,数据库中的表名userinfo
1 <!--省略非必要代码--> 2 <resultMaps> 3 <resultMap id="SelectAllUser" class="UserInfo"> 4 <result property="UserID" column="ID"/> 5 <result property="CardWord" column="Card_Word"/> 6 <result property="UserName" column="User_Name"/> 7 <result property="PassWord" column="Pass_Word"/> 8 <result property="UserPicture" column="User_Picture"/> 9 <result property="UserMail" column="User_Mail"/> 10 <result property="RegTime" column="Reg_Time"/> 11 <result property="UserStatus" column="User_Status"/> 12 </resultMap> 13 </resultMaps> 14 <!--省略非必要代码--> 15 <statements> 16 <!--获取好友信息--> 17 <select id="GetAllUserByID" resultMap="SelectAllUser" parameterClass="List"> 18 select * from userinfo where ID in 19 <iterate conjunction="," open="(" close=")"> 20 #[].UserID# 21 </iterate> 22 </select> 23 </statements> 24 <!--省略非必要代码-->View Code 调用代码
1 List<Map> hashTable = new List<Map>(); 2 List<Userinfo> userList = null; 3 userList = mapper.QueryForList<UserInfo>("GetAllUserByID", hashTable).ToList();
到此,若查询成功且有记录,userList中存储的就是每一条返回的UserInfo信息,当然,本博客主要说明的是参数类为List<T>泛型,然后在
IBatis.net中如何进行遍历,所以具体注释就未一一给出,像我一样的新手,若有什么疑问,留言即可.