IBatis.Net之多表查詢:
一、定制實際對應類的方式
首先配置多表的測試數據庫,IBatis.Net之Oracle表連接查詢配置:
首先新建兩張表如下:
為兩張表建立外鍵:
ALTER TABLE Person ADD CONSTRAINT FK_COUNTRY_PERSON FOREIGN KEY(CountryId) REFERENCES Country(Id);
程序中,建立一個PersonCountry.xml
<?xml version="1.0" encoding="utf-8" ?> <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance"> <resultMaps> <resultMap id="PersonCountry" Class="Ibatis.Net.Domain.PersonCountryModel"> <!--id會被statements節點所用,Class實體類所在位置--> <result property="Id" column="ID"/> <!--property實體類的屬性名,column對應的列名--> <result property="Name" column="NAME"/> <result property="CountryName" column="COUNTRYNAME"/> </resultMap> </resultMaps> <statements> <select id="SelectAllPersonWithCountry" resultMap="PersonCountry"> SELECT p.Id,p.Name,c.CountryName FROM Person p INNER JOIN Country c ON p.CountryId = c.Id </select> </statements> </sqlMap>
再建立一個對應結果的Model類
namespace Ibatis.Net.Domain { public class PersonCountryModel { public int Id { get; set; } public string Name { get; set; } public string CountryName { get; set; } } }
建立一個Dao類:
public class PersonCountryDao { public IList<PersonCountryModel> GetList() { ISqlMapper mapper = Mapper.Instance(); IList<PersonCountryModel> ListPC = mapper.QueryForList<PersonCountryModel>("SelectAllPersonWithCountry", null); return ListPC; } }
執行代碼:
static void Main(string[] args) { PersonCountryDao dao = new PersonCountryDao(); IList<PersonCountryModel> ListPC = dao.GetList(); foreach (PersonCountryModel p in ListPC) { Console.WriteLine(p.Id + p.Name + p.CountryName); } Console.ReadKey(); }
輸出如下:
二、AS轉換的方式
下面,我們還是用同樣的例子。但是,這次我只想查Person.Id與CountryName。那么這次不用定制類的方式,應該怎樣弄呢?
Person.xml如下:
<sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance"> <resultMaps> <resultMap id="Person" Class="Ibatis.Net.Domain.PersonModel"> <!--id會被statements節點所用,Class實體類所在位置--> <result property="Id" column="Id"/> <!--property實體類的屬性名,column對應的列名--> <result property="Name" column="Name"/> </resultMap> </resultMaps> <statements> <select id="SelectPersonWithCountryName" ResultMap="Person"> SELECT Person.Id,CountryName AS Name FROM Person INNER JOIN Country ON Person.CountryId = Country.Id </select> </statements> </sqlMap>
注意此時的Person還是與Person表對應,只有兩個字段,Id和Name,不用特別定制。
輸出如下:
這里的例子較為簡單,因為CountryName和Name都是字符串類型。如果復雜點,可能在寫SQL語句的時候需要使用SQL函數轉換類型,例如要查詢的列是整型,但是能夠接收返回結果的只有浮點型或字符串類型,這個時候就只有在SQL中轉換類型了。但如果想查的是DateTime類型,能用於接收結果的屬性只有int,那就沒辦法轉了,只有定制對應類。
三、關聯實體類
其實IBatis.net也支持NHibernate的那種方式,實體類關聯的方式來支持表與表之間的關系:
如,這次我想查詢Person,Country所有的信息:
public class PersonModel { public int Id { get; set; } public string Name { get; set; } //在Person之中加入一個CountryModel public CountryModel Country { get; set; } }
配置文件:
<sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance"> <resultMaps> <resultMap id="Person" Class="Ibatis.Net.Domain.PersonModel"> <result property="Id" column="Id"/> <result property="Name" column="Name"/> <result property="Country" resultMapping="Ibatis.Country"/><!--Ibatis是關聯類所在xml文件的namespace,Country是下兩行的那個id--> </resultMap> <resultMap id="Country" Class="Ibatis.Net.Domain.CountryModel"> <result property="Id" column="Id"/> <result property="CountryName" column="CountryName"/> </resultMap> </resultMaps> <statements> <select id="SelectPersonWithCountryName" resultMap="Person"> SELECT * FROM Person INNER JOIN Country ON Person.CountryId = Country.Id </select> </statements> </sqlMap>
運行輸出如下: