ibatis.net:第五天,QueryForObject
xml
1 <statement id="LoadOrder" parameterClass="int" resultClass="Order"> 2 SELECT Id, Customer FROM [Orders] WHERE Id = #value# 3 </statement>
代码
1 public static void LoadOrder() 2 { 3 var mapper = Mapper.Instance(); 4 5 var order1 = new Order { Customer = "段光伟" }; 6 var orderId = mapper.Insert("InsertOrder", order1); 7 8 var order2 = mapper.QueryForObject<Order>("LoadOrder", orderId); 9 var order3 = new Order(); 10 mapper.QueryForObject<Order>("LoadOrder", orderId, order3); 11 12 Console.WriteLine(order1.Customer); 13 Console.WriteLine(order2.Customer); 14 Console.WriteLine(order3.Customer); 15 Console.WriteLine(order1 == order2); // False 16 }
备注
QueryForObject 可以应对两种常见,A:重新创建一个对象;B:填充一个现有对象,可以多次调用这个方法,多次填充同一个对象。非常重要的一点是:ibatis 不支持主键映射。