Dapper入门教程

news2024/10/7 14:27:53

什么是Dapper

Dapper是一个简单的.NET对象映射器,在速度方面具有"King of Micro ORM"的头衔,几乎与使用原始的ADO.NET数据读取器一样快。ORM是一个对象关系映射器,它负责数据库和编程语言之间的映射。

Dapper通过扩展IDbConnection提供一些有用的扩展方法去查询您的数据库。

Dapper是如何工作的

它可以分为三个步骤:

  • 创建一个IDbConnection接口对象;
  • 编写一个查询SQL来执行CRUD操作;
  • 将查询SQL作为Execute方法的参数传递。

安装

Dapper

Install-Package Dapper

通过NuGet安装:NuGet Gallery | Dapper 2.0.123

Dapper简单使用

1、连接数据库

使用Mockaroo - Random Data Generator and API Mocking Tool | JSON / CSV / SQL / Excel提供的脚本创建数据库表:

create table Developer (
	id INT,
	DeveloperName VARCHAR(50),
	Email VARCHAR(50),
	GithubURL VARCHAR(50),
	ImageURL VARCHAR(50),
	Department VARCHAR(50),
	JoinedDate DATE
);
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (1, 'Tadeas', 'tdemchen0@craigslist.org', 'tmccarney0@dropbox.com', 'http://dummyimage.com/156x100.png/5fa2dd/ffffff', 'Support', '8/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (2, 'Abram', 'asavaage1@flavors.me', 'adowns1@yelp.com', 'http://dummyimage.com/145x100.png/cc0000/ffffff', 'Human Resources', '12/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (3, 'Rubetta', 'rgaine2@patch.com', 'rpurkins2@bbc.co.uk', 'http://dummyimage.com/186x100.png/ff4444/ffffff', 'Business Development', '8/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (4, 'Kirbie', 'kkelwaybamber3@live.com', 'kducker3@ucla.edu', 'http://dummyimage.com/197x100.png/ff4444/ffffff', 'Legal', '5/16/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (5, 'Florie', 'fdaubney4@marketwatch.com', 'fpentin4@reuters.com', 'http://dummyimage.com/113x100.png/5fa2dd/ffffff', 'Marketing', '5/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (6, 'Sloane', 'sstothard5@com.com', 'spischoff5@blogtalkradio.com', 'http://dummyimage.com/123x100.png/cc0000/ffffff', 'Legal', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (7, 'Dorena', 'deitter6@nasa.gov', 'divatts6@harvard.edu', 'http://dummyimage.com/190x100.png/dddddd/000000', 'Research and Development', '3/6/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (8, 'Kennett', 'kheeley7@slate.com', 'kdyche7@hud.gov', 'http://dummyimage.com/242x100.png/5fa2dd/ffffff', 'Services', '7/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (9, 'Iormina', 'ilawton8@answers.com', 'iskitral8@sbwire.com', 'http://dummyimage.com/105x100.png/5fa2dd/ffffff', 'Research and Development', '4/25/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (10, 'Gusty', 'gkubec9@wikispaces.com', 'gconti9@unicef.org', 'http://dummyimage.com/157x100.png/cc0000/ffffff', 'Legal', '3/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (11, 'Charissa', 'chischkea@usa.gov', 'cmenaula@typepad.com', 'http://dummyimage.com/241x100.png/cc0000/ffffff', 'Research and Development', '11/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (12, 'Kare', 'kklugmanb@usgs.gov', 'kcashamb@elegantthemes.com', 'http://dummyimage.com/233x100.png/5fa2dd/ffffff', 'Training', '12/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (13, 'Skipper', 'sradleyc@phoca.cz', 'sdonaldsonc@bizjournals.com', 'http://dummyimage.com/136x100.png/dddddd/000000', 'Sales', '10/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (14, 'Hortense', 'hhinkleyd@yale.edu', 'hcraigmyled@ehow.com', 'http://dummyimage.com/140x100.png/dddddd/000000', 'Support', '4/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (15, 'Lian', 'lmialle@google.com', 'lorteauxe@umich.edu', 'http://dummyimage.com/183x100.png/ff4444/ffffff', 'Research and Development', '1/29/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (16, 'Madison', 'mwilshawf@yahoo.com', 'mallredf@livejournal.com', 'http://dummyimage.com/231x100.png/cc0000/ffffff', 'Human Resources', '10/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (17, 'Orville', 'oattenbarrowg@unc.edu', 'omacgeffeng@ed.gov', 'http://dummyimage.com/158x100.png/dddddd/000000', 'Sales', '6/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (18, 'Dulcinea', 'dfilpih@cbslocal.com', 'dluckenh@yellowbook.com', 'http://dummyimage.com/168x100.png/dddddd/000000', 'Sales', '7/27/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (19, 'Brett', 'bperaccoi@ted.com', 'bsanperi@ft.com', 'http://dummyimage.com/241x100.png/ff4444/ffffff', 'Legal', '2/4/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (20, 'Clerissa', 'carnellj@google.cn', 'cwildtj@over-blog.com', 'http://dummyimage.com/210x100.png/ff4444/ffffff', 'Services', '2/6/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (21, 'Darla', 'dtintok@skype.com', 'dhadleighk@joomla.org', 'http://dummyimage.com/225x100.png/5fa2dd/ffffff', 'Training', '10/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (22, 'Zeke', 'zdoverl@netlog.com', 'zgrotel@deviantart.com', 'http://dummyimage.com/125x100.png/ff4444/ffffff', 'Product Management', '8/17/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (23, 'Diena', 'drapom@goo.gl', 'dfroschm@imageshack.us', 'http://dummyimage.com/135x100.png/5fa2dd/ffffff', 'Support', '10/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (24, 'Carri', 'cwillen@java.com', 'chavockn@chron.com', 'http://dummyimage.com/203x100.png/5fa2dd/ffffff', 'Product Management', '4/19/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (25, 'Dale', 'dfuttero@bbb.org', 'dturfino@youku.com', 'http://dummyimage.com/114x100.png/ff4444/ffffff', 'Engineering', '3/11/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (26, 'Allis', 'agilchristp@mediafire.com', 'acromettp@google.com', 'http://dummyimage.com/149x100.png/cc0000/ffffff', 'Sales', '8/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (27, 'Stanly', 'swaylandq@hao123.com', 'sfearbyq@state.tx.us', 'http://dummyimage.com/155x100.png/cc0000/ffffff', 'Marketing', '5/15/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (28, 'Godfry', 'ghouseleer@shinystat.com', 'gdymocker@friendfeed.com', 'http://dummyimage.com/226x100.png/ff4444/ffffff', 'Human Resources', '12/25/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (29, 'Norrie', 'nmcarts@goo.ne.jp', 'nbelones@dailymail.co.uk', 'http://dummyimage.com/135x100.png/ff4444/ffffff', 'Training', '10/16/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (30, 'Sharity', 'swisbyt@bloglovin.com', 'scubberleyt@wufoo.com', 'http://dummyimage.com/103x100.png/5fa2dd/ffffff', 'Legal', '1/12/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (31, 'Cookie', 'cmartlewu@etsy.com', 'cchaundyu@nymag.com', 'http://dummyimage.com/134x100.png/ff4444/ffffff', 'Human Resources', '1/24/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (32, 'Gwenora', 'grichtv@constantcontact.com', 'gjustisv@themeforest.net', 'http://dummyimage.com/161x100.png/cc0000/ffffff', 'Legal', '10/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (33, 'Tara', 'taxonw@mozilla.com', 'tblasdalew@arizona.edu', 'http://dummyimage.com/211x100.png/cc0000/ffffff', 'Support', '9/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (34, 'Ramon', 'rdymokex@columbia.edu', 'rtargex@yahoo.co.jp', 'http://dummyimage.com/121x100.png/cc0000/ffffff', 'Legal', '3/19/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (35, 'Renae', 'rteersy@fc2.com', 'rpittwayy@xrea.com', 'http://dummyimage.com/206x100.png/cc0000/ffffff', 'Accounting', '4/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (36, 'Ninon', 'nbeadellz@yahoo.com', 'ndumbarez@wp.com', 'http://dummyimage.com/171x100.png/ff4444/ffffff', 'Support', '3/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (37, 'Wyndham', 'wkeaton10@mlb.com', 'wnockalls10@comsenz.com', 'http://dummyimage.com/237x100.png/ff4444/ffffff', 'Services', '7/10/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (38, 'Merna', 'mflear11@stanford.edu', 'mwreath11@google.co.uk', 'http://dummyimage.com/101x100.png/cc0000/ffffff', 'Engineering', '3/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (39, 'Barney', 'bwomack12@edublogs.org', 'bkahen12@ihg.com', 'http://dummyimage.com/232x100.png/5fa2dd/ffffff', 'Business Development', '6/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (40, 'Oralee', 'ogillinghams13@youtu.be', 'oattkins13@list-manage.com', 'http://dummyimage.com/108x100.png/dddddd/000000', 'Support', '12/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (41, 'Leslie', 'lwhatley14@businessweek.com', 'lstainburn14@kickstarter.com', 'http://dummyimage.com/138x100.png/ff4444/ffffff', 'Sales', '2/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (42, 'Florance', 'fgripton15@fema.gov', 'fwingatt15@mail.ru', 'http://dummyimage.com/158x100.png/ff4444/ffffff', 'Support', '4/10/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (43, 'Valina', 'viglesia16@scribd.com', 'vmonahan16@storify.com', 'http://dummyimage.com/205x100.png/5fa2dd/ffffff', 'Sales', '10/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (44, 'Gary', 'gpantry17@fema.gov', 'gmarfell17@storify.com', 'http://dummyimage.com/246x100.png/5fa2dd/ffffff', 'Research and Development', '2/2/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (45, 'Jillian', 'jsaffle18@craigslist.org', 'joxx18@liveinternet.ru', 'http://dummyimage.com/112x100.png/ff4444/ffffff', 'Research and Development', '10/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (46, 'Dallas', 'dallchorne19@hibu.com', 'divasechko19@cornell.edu', 'http://dummyimage.com/185x100.png/cc0000/ffffff', 'Training', '7/3/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (47, 'Lalo', 'lhammarberg1a@alibaba.com', 'lcornish1a@behance.net', 'http://dummyimage.com/181x100.png/ff4444/ffffff', 'Accounting', '11/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (48, 'Haskel', 'hspatig1b@guardian.co.uk', 'htreweek1b@woothemes.com', 'http://dummyimage.com/190x100.png/5fa2dd/ffffff', 'Marketing', '9/2/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (49, 'Alison', 'agilburt1c@opensource.org', 'aolahy1c@hatena.ne.jp', 'http://dummyimage.com/232x100.png/cc0000/ffffff', 'Research and Development', '4/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (50, 'Norby', 'nkynoch1d@google.com.br', 'ncumbes1d@flavors.me', 'http://dummyimage.com/194x100.png/cc0000/ffffff', 'Marketing', '12/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (51, 'Vonny', 'vtoll1e@simplemachines.org', 'vaxell1e@vk.com', 'http://dummyimage.com/141x100.png/5fa2dd/ffffff', 'Engineering', '3/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (52, 'Janifer', 'jlambarth1f@squidoo.com', 'jpersence1f@imageshack.us', 'http://dummyimage.com/154x100.png/cc0000/ffffff', 'Legal', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (53, 'Thurston', 'tbroseman1g@histats.com', 'tsonley1g@tinypic.com', 'http://dummyimage.com/246x100.png/ff4444/ffffff', 'Product Management', '7/3/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (54, 'Arlin', 'awestpfel1h@ehow.com', 'aearney1h@biglobe.ne.jp', 'http://dummyimage.com/136x100.png/ff4444/ffffff', 'Sales', '4/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (55, 'Davina', 'dpennycuick1i@plala.or.jp', 'dwallice1i@wikia.com', 'http://dummyimage.com/197x100.png/dddddd/000000', 'Support', '5/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (56, 'Tallou', 'tpabst1j@princeton.edu', 'tbirden1j@deviantart.com', 'http://dummyimage.com/141x100.png/5fa2dd/ffffff', 'Engineering', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (57, 'Gilligan', 'glavender1k@auda.org.au', 'gyouthead1k@ihg.com', 'http://dummyimage.com/183x100.png/5fa2dd/ffffff', 'Human Resources', '8/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (58, 'Fraze', 'fcumesky1l@weather.com', 'fangerstein1l@arizona.edu', 'http://dummyimage.com/105x100.png/dddddd/000000', 'Human Resources', '10/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (59, 'Wendy', 'welijahu1m@nifty.com', 'wrude1m@pcworld.com', 'http://dummyimage.com/115x100.png/cc0000/ffffff', 'Business Development', '12/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (60, 'Shauna', 'smorigan1n@wix.com', 'smarrett1n@ebay.co.uk', 'http://dummyimage.com/176x100.png/dddddd/000000', 'Engineering', '8/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (61, 'Maximilianus', 'mhadleigh1o@123-reg.co.uk', 'mchannon1o@lulu.com', 'http://dummyimage.com/165x100.png/5fa2dd/ffffff', 'Marketing', '10/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (62, 'Edin', 'ewillgress1p@usgs.gov', 'evaudre1p@cbsnews.com', 'http://dummyimage.com/188x100.png/ff4444/ffffff', 'Engineering', '9/15/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (63, 'Jaquenetta', 'jjancar1q@harvard.edu', 'joloshin1q@mashable.com', 'http://dummyimage.com/219x100.png/ff4444/ffffff', 'Human Resources', '3/14/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (64, 'Mischa', 'mtouret1r@wiley.com', 'mballantyne1r@scientificamerican.com', 'http://dummyimage.com/162x100.png/5fa2dd/ffffff', 'Accounting', '2/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (65, 'Janet', 'jhavenhand1s@ed.gov', 'jgarnall1s@guardian.co.uk', 'http://dummyimage.com/190x100.png/dddddd/000000', 'Business Development', '8/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (66, 'Rodina', 'rshackelton1t@wired.com', 'rfrith1t@ycombinator.com', 'http://dummyimage.com/136x100.png/cc0000/ffffff', 'Training', '12/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (67, 'Hollie', 'hcastillo1u@sciencedirect.com', 'hgiacovazzo1u@narod.ru', 'http://dummyimage.com/102x100.png/dddddd/000000', 'Legal', '2/20/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (68, 'Devin', 'dgaraway1v@acquirethisname.com', 'dtremoille1v@upenn.edu', 'http://dummyimage.com/221x100.png/ff4444/ffffff', 'Legal', '9/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (69, 'Donn', 'ddurak1w@who.int', 'dwybrow1w@amazon.de', 'http://dummyimage.com/153x100.png/ff4444/ffffff', 'Legal', '10/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (70, 'Tybi', 'tklaaasen1x@wikispaces.com', 'tdougher1x@elpais.com', 'http://dummyimage.com/224x100.png/5fa2dd/ffffff', 'Product Management', '8/23/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (71, 'Melva', 'mwohler1y@istockphoto.com', 'mgoodbody1y@mtv.com', 'http://dummyimage.com/111x100.png/cc0000/ffffff', 'Training', '2/5/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (72, 'Cointon', 'cgunther1z@ebay.com', 'cphinnis1z@nbcnews.com', 'http://dummyimage.com/223x100.png/dddddd/000000', 'Product Management', '3/27/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (73, 'Melina', 'myexley20@webnode.com', 'mraynard20@patch.com', 'http://dummyimage.com/167x100.png/ff4444/ffffff', 'Accounting', '8/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (74, 'Annissa', 'amattiello21@about.me', 'alebbon21@disqus.com', 'http://dummyimage.com/248x100.png/cc0000/ffffff', 'Business Development', '12/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (75, 'Cheston', 'cdovydenas22@tmall.com', 'crhymer22@cbsnews.com', 'http://dummyimage.com/125x100.png/cc0000/ffffff', 'Accounting', '11/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (76, 'Simone', 'strimnell23@google.fr', 'stye23@vistaprint.com', 'http://dummyimage.com/115x100.png/dddddd/000000', 'Research and Development', '2/4/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (77, 'Billy', 'bcollerd24@jiathis.com', 'bbettleson24@elegantthemes.com', 'http://dummyimage.com/236x100.png/ff4444/ffffff', 'Services', '5/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (78, 'Ethan', 'edecourcy25@cam.ac.uk', 'eagates25@senate.gov', 'http://dummyimage.com/144x100.png/dddddd/000000', 'Support', '10/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (79, 'Thornton', 'tharvatt26@wikipedia.org', 'tnoice26@instagram.com', 'http://dummyimage.com/213x100.png/cc0000/ffffff', 'Services', '4/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (80, 'Ruy', 'rstrutley27@bloomberg.com', 'rpovall27@usgs.gov', 'http://dummyimage.com/182x100.png/cc0000/ffffff', 'Engineering', '10/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (81, 'Ella', 'enerne28@walmart.com', 'eschooling28@yellowpages.com', 'http://dummyimage.com/231x100.png/ff4444/ffffff', 'Accounting', '5/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (82, 'Charlie', 'cloiterton29@infoseek.co.jp', 'csplevins29@unesco.org', 'http://dummyimage.com/168x100.png/5fa2dd/ffffff', 'Services', '10/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (83, 'Tobi', 'twillers2a@sciencedaily.com', 'tnottle2a@google.com.au', 'http://dummyimage.com/212x100.png/cc0000/ffffff', 'Services', '4/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (84, 'Rani', 'rramsden2b@istockphoto.com', 'rpratton2b@w3.org', 'http://dummyimage.com/169x100.png/dddddd/000000', 'Accounting', '2/1/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (85, 'Evangeline', 'ebuttler2c@mapquest.com', 'efragino2c@pen.io', 'http://dummyimage.com/148x100.png/ff4444/ffffff', 'Services', '1/22/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (86, 'Sophie', 'stowell2d@yellowbook.com', 'sohare2d@delicious.com', 'http://dummyimage.com/139x100.png/5fa2dd/ffffff', 'Services', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (87, 'Aimee', 'abuttler2e@csmonitor.com', 'ahalgarth2e@cafepress.com', 'http://dummyimage.com/115x100.png/dddddd/000000', 'Engineering', '10/6/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (88, 'Velma', 'vsmy2f@ft.com', 'vtapton2f@a8.net', 'http://dummyimage.com/123x100.png/dddddd/000000', 'Training', '10/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (89, 'Shaylah', 'sisaq2g@squarespace.com', 'slangeren2g@drupal.org', 'http://dummyimage.com/167x100.png/dddddd/000000', 'Sales', '2/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (90, 'Lyda', 'lleal2h@deliciousdays.com', 'llidgate2h@usda.gov', 'http://dummyimage.com/155x100.png/ff4444/ffffff', 'Training', '2/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (91, 'Vassily', 'vhinken2i@elegantthemes.com', 'vbarbour2i@engadget.com', 'http://dummyimage.com/249x100.png/cc0000/ffffff', 'Human Resources', '4/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (92, 'Keane', 'kdeavenell2j@psu.edu', 'kurling2j@mac.com', 'http://dummyimage.com/176x100.png/ff4444/ffffff', 'Business Development', '11/25/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (93, 'Stephie', 'sparidge2k@jiathis.com', 'sabercromby2k@pcworld.com', 'http://dummyimage.com/119x100.png/5fa2dd/ffffff', 'Business Development', '6/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (94, 'Edmon', 'eswindley2l@slate.com', 'emeacher2l@blogs.com', 'http://dummyimage.com/197x100.png/cc0000/ffffff', 'Sales', '2/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (95, 'Golda', 'grhodef2m@sourceforge.net', 'gstapells2m@guardian.co.uk', 'http://dummyimage.com/155x100.png/cc0000/ffffff', 'Accounting', '8/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (96, 'Robinett', 'rcoch2n@theglobeandmail.com', 'rcicci2n@t-online.de', 'http://dummyimage.com/171x100.png/5fa2dd/ffffff', 'Accounting', '12/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (97, 'Daisey', 'dcridlon2o@ucoz.ru', 'dcoventon2o@blog.com', 'http://dummyimage.com/194x100.png/5fa2dd/ffffff', 'Business Development', '10/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (98, 'Maddi', 'mblunderfield2p@sakura.ne.jp', 'mhambric2p@dropbox.com', 'http://dummyimage.com/187x100.png/ff4444/ffffff', 'Sales', '5/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (99, 'Garold', 'gcolthard2q@tmall.com', 'gmcneillie2q@domainmarket.com', 'http://dummyimage.com/185x100.png/5fa2dd/ffffff', 'Marketing', '11/20/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (100, 'Purcell', 'pmilliken2r@flavors.me', 'psysland2r@chron.com', 'http://dummyimage.com/208x100.png/5fa2dd/ffffff', 'Support', '2/11/2022');

 创建.NET Core WEB API项目

 

 

添加好项目引用后,就可以添加相应的连接字符串。

2、Dapper使用

 public class DeveloperRepository : IDeveloperRepository
    {
        private readonly IConfiguration configuration;
        public DeveloperRepository(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public IDbConnection Connection
        {
            get
            {
                return new SqlConnection(configuration.GetConnectionString("Db"));
            }
        }


        public void AddDeveloper(Developer developer)
        {
            try
            {
                using(IDbConnection dbConnection=Connection)
                {
                    string Id=Guid.NewGuid().ToString("N");
                    dbConnection.Open();
                    string query = @"INSERT INTO Developer(DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate) VALUES(@DeveloperName,@Email,@GithubURL,@ImageURL,@Department,@JoinedDate)";
                    dbConnection.Execute(query, developer);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public void DeleteDeveloper(int Id)
        {
            try
            {
                using(IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = @"DELETE FROM Developer WHERE Id=@Id";
                    dbConnection.Execute(query, new {Id=Id});
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public async Task<IEnumerable<Developer>> GetAllDevelopersAsync()
        {
            try
            {
                using(IDbConnection dbConnection=Connection)
                {
                    dbConnection.Open();
                    string query = @"SELECT Id,DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate FROM Developer";
                  return   await dbConnection.QueryAsync<Developer>(query);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public async  Task<Developer> GetDeveloperByEmailAsync(string Email)
        {
            try
            {
                using (IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = "SELECT * FROM Developer WHERE Email=@Email";
                    return await dbConnection.QueryFirstOrDefaultAsync<Developer>(query, new { Email = Email });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public async  Task<Developer> GetDeveloperByIdAsync(int Id)
        {
            try
            {
                using (IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = "SELECT * FROM Developer WHERE Id=@Id";
                    return await dbConnection.QueryFirstOrDefaultAsync<Developer>(query, new { Id = Id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateDeveloper(Developer developer)
        {
            try
            {
                using (IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = @"UPDATE Developer SET DeveloperName=@DeveloperName, Email=@Email, GithubURL=@GithubURL, ImageURL=@ImageURL, Department=@Department, JoinedDate=@JoinedDate";
                    dbConnection.Execute(query, developer);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
  public class DeveloperService : IDeveloperService
    {

        protected readonly IDeveloperRepository _developerRepository;

        public DeveloperService(IDeveloperRepository developerRepository)
        {
            _developerRepository = developerRepository;
        }

        public void AddDeveloper(Developer developer)
        {
            _developerRepository.AddDeveloper(developer);
        }

        public void DeleteDeveloper(int Id)
        {
            _developerRepository.DeleteDeveloper(Id);
        }

        public Task<IEnumerable<Developer>> GetAllDevelopers()
        {
            return _developerRepository.GetAllDevelopersAsync();
        }

        public Task<Developer> GetDeveloperByEmail(string Email)
        {
            return _developerRepository.GetDeveloperByEmailAsync(Email);
        }

        public Task<Developer> GetDeveloperById(int Id)
        {
            return _developerRepository.GetDeveloperByIdAsync(Id);
        }

        public void UpdateDeveloper(Developer developer)
        {
            _developerRepository.UpdateDeveloper(developer);
        }
    }

 3、WEB API调用

先添加服务,然后就可以调用。

 [Route("api/[controller]/[action]")]
    [ApiController]
    public class DevelopersController : ControllerBase
    {
        protected readonly IDeveloperService _developerService;

        public DevelopersController(IDeveloperService developerService)
        {
            _developerService = developerService;
        }


        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<IActionResult> GetAllDevelopers()
        {
            var developers = await _developerService.GetAllDevelopers();
            return Ok(developers);
        }

        [Route("[action]")]
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<IActionResult> GetDeveloperById(int Id)
        {
            var developer = await _developerService.GetDeveloperById(Id);
            return Ok(developer);
        }

        [Route("[action]")]
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<IActionResult> GetDeveloperByEmail(string Email)
        {
            var developer = await _developerService.GetDeveloperByEmail(Email);
            return Ok(developer);
        }

        [HttpPost]
        [ProducesResponseType(StatusCodes.Status201Created)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult AddDeveloper([FromBody] Developer developer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            _developerService.AddDeveloper(developer);
            return CreatedAtAction(nameof(GetDeveloperById), new { Id = developer.Id }, developer);

        }

        [HttpPut]
        [ProducesResponseType(StatusCodes.Status201Created)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult UpdateDeveloper([FromBody] Developer developer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            _developerService.UpdateDeveloper(developer);
            return Ok();
        }

        [HttpDelete]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult DeleteDeveloper(int Id)
        {
            _developerService.DeleteDeveloper(Id);
            return Ok();
        }
    }

调用结果如下

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/951896.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

PSP - 蛋白质结构预测 OpenFold Multimer 模型训练参数与配置

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/132575709 OpenFold Multimer 是用于预测蛋白质多聚体结构的计算方法。基于OpenFold 的单体预测框架&#xff0c;利用深度学习技术&#xff0c;结…

指针与空间按钮的交互

文章目录 原理案例&#xff1a;“直线指针”和“点击按钮”的交互1、效果2、步骤 原理 指针不能直接和空间按钮交互&#xff0c;得借助一个中间层——分发器——它分发指针的进入、退出、选择事件&#xff0c;空间按钮自动监听这些事件 案例&#xff1a;“直线指针”和“点击…

SQLServer审计功能配置

一. SQL Server审计功能介绍 SQL Server审计功能&#xff08;Audit&#xff09;是SQL Server 2008之后才有的功能&#xff0c;审计(Audit)用于追踪和记录SQL Server实例&#xff0c;或者单个数据库中发生的事件(Event)&#xff0c;审计运作的机制是通过捕获事件(Event)&#x…

Vector 动态数组(迭代器)

C数据结构与算法 目录 本文前驱课程 1 C自学精简教程 目录(必读) 2 Vector<T> 动态数组&#xff08;模板语法&#xff09; 本文目标 1 熟悉迭代器设计模式&#xff1b; 2 实现数组的迭代器&#xff1b; 3 基于迭代器的容器遍历&#xff1b; 迭代器语法介绍 对迭…

3分钟上手Python的命令行参数解析

1 简介 处理命令行参数对许多程序来说都是必不可少的功能。今天为大家介绍 Python 中的 argparse 模块&#xff0c;它是 Python 标准库中的一个模块&#xff0c;它可以让开发者非常简单地为程序添加命令行参数接口&#xff0c;包括位置参数、可选参数、标志等&#xff0c;并自…

测试左移——代码审计SonarQube 平台搭建

一、sonarqube代码分析技术体系 1、代码分析工具 IDE 辅助功能 xcode、android studio阿里巴巴 java 开发手册 ide 插件支持 独立的静态分析工具 spotbugs、findbugs、androidlint、scan-build、Checkstyle、FindSecBugspmd 阿里巴巴 java 开发手册 pmd 插件 综合性的代码…

说说FLINK细粒度滑动窗口如何处理

分析&回答 Flink的窗口机制是其底层核心之一&#xff0c;也是高效流处理的关键。Flink窗口分配的基类是WindowAssigner抽象类&#xff0c;下面的类图示出了Flink能够提供的所有窗口类型。 Flink窗口分为滚动&#xff08;tumbling&#xff09;、滑动&#xff08;sliding&am…

DWA算法学习

一、DWA概念  DWA(动态窗口法)属于局部路径规划方法&#xff0c;为ROS中主要采用的方法。其原理主要是在速度空间&#xff08;v,w&#xff09;中采样多组速度&#xff0c;并模拟这些速度在一定时间内的运动轨迹&#xff0c;再通过一个评价函数对这些轨迹打分&#xff0c;最优的…

2023年全国职业院校技能大赛网络系统管理赛项 模块B:服务部署 卷II

2023年全国职业院校技能大赛 GZ073网络系统管理赛项 模块B&#xff1a;服务部署 卷II 目 录 一、Windows项目任务描述 1 &#xff08;一&#xff09;拓扑图 1 &#xff08;二&#xff09;网络地址规划 1 二、Windows项目任务清单 2 &#xff08;一&#xff09;服务器IspSrver…

数据库(一) 基础知识

概述 数据库是按照数据结构来组织,存储和管理数据的仓库 数据模型 数据库系统的核心和基础是数据模型&#xff0c;数据模型是严格定义的一组概念的集合。因此数据模型一般由数据结构、数据操作和完整性约束三部分组成。数据模型主要分为三种:层次模型&#xff0c;网状模型和关…

【云原生】Ansible自动化批量操作工具playbook剧本

目录 1.playbook相关知识 1.1 playbook 的简介 1.2 playbook的 各部分组成 2. 基础的playbook剧本编写实例 2.1 playbook编写Apache安装剧本&#xff08;yum方式安装&#xff09; 报错集&#xff1a; 实例2&#xff1a;playbook编写nginx 的yum安装并且能修改其监听端口的…

QT基础教程之四QMainWindow

QT基础教程之四QMainWindow QMainWindow是一个为用户提供主窗口程序的类&#xff0c;包含一个菜单栏&#xff08;menu bar&#xff09;、多个工具栏(tool bars)、多个锚接部件(dock widgets)、一个状态栏(status bar)及一个中心部件(central widget)&#xff0c;是许多应用程序…

13、监测数据采集物联网应用开发步骤(9.2)

监测数据采集物联网应用开发步骤(9.1) TCP/IP Server开发 新建TCP/IP Server线程类com.zxy.tcp.ServerThread.py #! python3 # -*- coding: utf-8 -Created on 2017年05月10日 author: zxyong 13738196011 import socket,threading,time from com.zxy.tcp.TcpServer import …

业务流程与逻辑编排的低代码平台,一文全方位了解它的轻应用信息

JVS低代码开发平台提供了大量的可配置组件和预先集成的功能&#xff0c;开发人员可以通过拖拽和设置属性的方式&#xff0c;快速搭建应用程序的前端界面和交互逻辑。同时&#xff0c;低代码平台也提供了丰富的后端服务和集成能力&#xff0c;可以轻松地与现有的系统和第三方服务…

vulnhub靶机02-Breakout

主机发现 arp-scan -l 扫描端口 nmap --min-rate 10000 -p- 192.168.21.143 扫描端口信息 nmap -sV -sT -O -p80,139,445,10000,20000 192.168.21.143 漏洞扫描 nmap --scriptvuln -p80,139,445,10000,20000 192.168.21.143 先看网站 什么都没有看看f12 找到点好东西 解码…

被遗弃的多重继承

问题 C 是否允许一个类继承自多个父类&#xff1f; C 支持编写多重继承的代码 一个子类可以拥有多个父类 子类拥有所有父类的成员变量 子类继承所有父类的成员函数 子类对象可以当作任意父类对象使用 多重继承的语法规则 多重继承的本质与单继承相同&#xff01; 通过多重…

ChatRWKV 学习笔记和使用指南

0x0. 前言 Receptance Weighted Key Value&#xff08;RWKV&#xff09;是pengbo提出的一个新的语言模型架构&#xff0c;它使用了线性的注意力机制&#xff0c;把Transformer的高效并行训练与RNN的高效推理相结合&#xff0c;使得模型在训练期间可以并行&#xff0c;并在推理…

基于Java的代驾管理系统 springboot+vue,mysql数据库,前台用户、商户+后台管理员,有一万五千字报告,完美运行

基于Java的代驾管理系统 springbootvue&#xff0c;mysql数据库&#xff0c;前台用户、商户后台管理员&#xff0c;有一万五千字报告&#xff0c;完美运行。 系统完美实现用户下单叫车、商户接单、管理员管理系统&#xff0c;页面良好&#xff0c;系统流畅。 各角色功能&#x…

GPT能否辅助数学学习

GPT4.0的数学能力怎么样&#xff1f;我们使用镜像站进行实验&#xff0c;通过不同水平的数学看看GPT4.0的数学能力得到进步没有。镜像站的地址我们放在了最后&#xff0c;各位读者也可以自行去测试。 笔者在ChatGPT镜像站进行测试&#xff0c;我们的实验是让GPT4.0自行出数学题…

记本地新建一个gradle方式springboot项目过程

打算使用gradle在idea新建个springboot项目&#xff0c;然后坑很多&#xff0c;记录一下 原来我的idea应该是社区版&#xff0c;新建项目时候没有可以选择spring相关配置&#xff0c;然后卸载了重装&#xff0c;之前问题是启动是启动起来了&#xff0c;但是状态栏那边一直显示…