Dapper做爲.NET生態中廣爲人知的輕量級ORM類庫在.NET Core裏仍能被有效利用,而且其不但能夠連通SQL Server數據庫還提供對其它數據庫,好比MySQL的支持。這裏試驗了一下經過Dapper鏈接MySQL的方法。mysql
能夠選擇直接安裝在原生系統中或是Docker裏。
Official
Dockersql
在MySQL中創建兩張表。docker
city表:數據庫
CREATE TABLE `city` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` char(35) NOT NULL DEFAULT '', `CountryCode` char(3) NOT NULL DEFAULT '', `District` char(20) NOT NULL DEFAULT '', `Population` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`ID`), KEY `CountryCode` (`CountryCode`), CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`code`) ) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1
country表:api
CREATE TABLE `country` ( `Code` char(3) NOT NULL DEFAULT '', `Name` char(52) NOT NULL DEFAULT '', `Continent` enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL DEFAULT 'Asia', `Region` char(26) NOT NULL DEFAULT '', `SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00', `IndepYear` smallint(6) DEFAULT NULL, `Population` int(11) NOT NULL DEFAULT '0', `LifeExpectancy` float(3,1) DEFAULT NULL, `GNP` float(10,2) DEFAULT NULL, `GNPOld` float(10,2) DEFAULT NULL, `LocalName` char(45) NOT NULL DEFAULT '', `GovernmentForm` char(45) NOT NULL DEFAULT '', `HeadOfState` char(60) DEFAULT NULL, `Capital` int(11) DEFAULT NULL, `Code2` char(2) NOT NULL DEFAULT '', PRIMARY KEY (`Code`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
應用程序工程中須要添加Dapper以及Mysql.Data類庫。app
dotnet add package Dapper
dotnet add package MySql.Dataide
編寫兩個實體類,用於映射city與country表。3d
public class CityEntity { public int ID { get; set; } public string Name { get; set; } public string CountryCode { get; set; } public string District { get; set; } public int Population { get; set; } public CountryEntity Country { get; set; } public override string ToString() { return $"ID: {ID}, Name: {Name}, CountryCode: {CountryCode}, District: {District}, Population: {Population}, Country: {Country}"; } }
public class CountryEntity { public string Code { get; set; } public string Name { get; set; } public string Continent { get; set; } public string Region { get; set; } public decimal SurfaceArea { get; set; } public int IndepYear { get; set; } public int Population { get; set; } public decimal LifeExpectancy { get; set; } public decimal GNP { get; set; } public decimal GNPOld { get; set; } public string LocalName { get; set; } public string GovernmentForm { get; set; } public string HeadOfState { get; set; } public int Capital { get; set; } public string Code2 { get; set; } public override string ToString() { return $"Code: {Code}, Name: {Name}, Continent: {Continent}, Region: {Region}, SurfaceArea: {SurfaceArea} "; } }
倉庫類中新加獲取10個城市數據的方法。這裏Dapper的Query方法有三個參數,第一個是須要執行的SQL語句,第二個是對象之間的對應關係(這個例子中city與country爲一對一關係),並肯定最終返回的對象類型,最後的SplitOn參數會告訴Dapper在結果集中兩張表之間以哪一個字段進行分界。code
public class CityRepository { public List<CityEntity> Get10Cities() { List<CityEntity> result; using (var conn = new MySqlConnection("Host=localhost;Port=3306;Database=world;Uid=admin;pwd=admin")) { var sql = "SELECT * FROM city INNER JOIN country ON city.CountryCode = country.Code LIMIT 10"; result = conn.Query<CityEntity, CountryEntity, CityEntity>(sql, (city, country) => { city.Country = country; return city; }, splitOn: "Code").ToList(); } return result; } }
static void Main(string[] args) { var repository = new CityRepository(); var cities = repository.Get10Cities(); cities.ForEach(e=>{ System.Console.WriteLine(e); }); }
程序運行的結果以下,能夠看到成功藉助Dapper的力量從MySQL數據庫裏獲取了所需的數據。
orm