当前位置: 移动技术网 > IT编程>开发语言>.net > .NET Core开发日志——Dapper与MySQL

.NET Core开发日志——Dapper与MySQL

2018年10月02日  | 移动技术网IT编程  | 我要评论

马克沁水冷重机枪,性感美女视频,p.pin6.biz

dapper作为.net生态中广为人知的轻量级orm类库在.net core里仍能被有效利用,并且其不但可以连通sql server数据库还提供对其它数据库,比如mysql的支持。这里试验了一下通过dapper连接mysql的方法。

mysql

可以选择直接安装在原生系统中或是docker里。
official
docker

table

在mysql中建立两张表。

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表:

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

package

应用程序工程中需要添加dapper以及mysql.data类库。

dotnet add package dapper
dotnet add package mysql.data

entity

编写两个实体类,用于映射city与country表。

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} ";
    }
}

repository

仓库类中新加获取10个城市数据的方法。这里dapper的query方法有三个参数,第一个是需要执行的sql语句,第二个是对象之间的对应关系(这个例子中city与country为一对一关系),并确定最终返回的对象类型,最后的spliton参数会告诉dapper在结果集中两张表之间以哪个字段进行分界。

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;
    }
}

test

static void main(string[] args)
{
    var repository = new cityrepository();
    var cities = repository.get10cities();
    cities.foreach(e=>{
        system.console.writeline(e);
    });
}

程序运行的结果如下,可以看到成功借助dapper的力量从mysql数据库里获取了所需的数据。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网