NetCore使用Dapper查询数据
NetCore使用Dapper查询数据
- 一、Nuget包安装
- 二、Class
- 1.Student
- 2.IStudentQueries
- 3.StudentQueries
- 4.MediatorModule 使用Autofac注入
- 5.starup
- 6.connectionstring添加allowuservariables=True
- 7.Controller
- 三、效果展示
- 四、用法总结
- 1.in条件查询
一、Nuget包安装
Dapper
MySqlConnector
二、Class
1.Student
public class Student{public int ID { get; set; }public string Name { get; set; } ;public string Sex { get; set; } ;public string Phone { get; set; }public string Address { get; set; }}
2.IStudentQueries
public interface IStudentQueries{Task<List<Student>> GetStudent(int studentId);}
3.StudentQueries
public class StudentQueries :IStudentQueries{private string _connectionString = string.Empty;public StudentQueries(string constr) {_connectionString = !string.IsNullOrWhiteSpace(constr) ? constr : throw new ArgumentNullException(nameof(constr));}public async Task<List<Student>> GetStudent(int studentId){using(var dbconnection = new MySqlConnection(_connectionString)){dbconnection.Open();var sb = new StringBuilder();var sql = "" ;if (studentId >0){sql = $@" select * from student where id = @studentId";}return (await dbconnection.QueryAsync<Student>(sql, new {studentId })).ToList();}}}
4.MediatorModule 使用Autofac注入
public class MediatorModule : Autofac.Module{//构造函数,将链接字符串传入public string QueriesConnectionString { get; }public MediatorModule(string qconstr){QueriesConnectionString = qconstr;}protected override void Load(ContainerBuilder builder){builder.Register(c => new StudentQueries(QueriesConnectionString)).As<IStudentQueries>().InstancePerLifetimeScope();}
5.starup
与ConfigureServices同级
public virtual void ConfigureContainer(ContainerBuilder builder){builder.RegisterModule(new MediatorModule(Configuration.GetConnectionString("ConnectionMysql")));}
6.connectionstring添加allowuservariables=True
allowuservariables=True;允许以参数传入
"ConnectionMysql": "server=localhost;uid=root;pwd=123456;database=studenttest;allowuservariables=True; "
7.Controller
public async Task<List<Student>> GetSqlData(int studentId){return await _productQueries.GetStudent(studentId);//操纵stream,生成集合}
三、效果展示
四、用法总结
1.in条件查询
//listId 可以是一个集合,也能以数组传递参数"...WHERE detail_id IN @detailIds"await connection.QueryAsync<Account>(sql, new { detailIds = listId })).ToList();
NetCore使用Dapper查询数据相关推荐
- java查询mysql装载bean_jsp与javabean链接mysql数据库并查询数据表的简单实例源码
jsp与javabean链接mysql数据库并查询数据表的简单实例源码.这个简单的实例是给新手学习的,或者一些高手临时忘记怎么使用jsp操作mysql数据库时候查找的,包括了建立mysql数据库连接的 ...
- JUnit测试类完成后事务是默认 回滚的。只能查询数据,不能增删改。
JUnit测试类完成后事务是默认 回滚的.只能查询数据,不能增删改. 在测试类或者测试方法上面加上注解 @Rollback(false) 表示事物不回滚,这样数据就可以提交到数据库中了. 转载于:h ...
- Elasticsearch 查询数据的工作原理是什么?
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源:8rr.co/GsAa 面试题 ES 写入数据的工作原理是什 ...
- Statement接口实现查询数据、添加数据
本文介绍了Statement接口实现查询数据.添加数据.在JDBC的基本应用中,介绍了使用Statement接口查询和添加数据的步骤.重点在于使用getConnection()方法来连接数据库,创建S ...
- mysql三表查询数据重复_解决mybatis三表连接查询数据重复的问题
此问题的产生,主要是数据库的字段名一样导致 三张表 DOCTOR JOB OBJECT 有问题的查询语句和查询结果是: SELECT d.*,j.*,o.* from (select d.*,rown ...
- pandas 查询数据
文章目录 视频 概论 使用行和列查询数据 行 列 单行.单列 单行多列 多行单列 多行多列 使用数值区间进行范围查询(左闭右闭和列表不一样) 条件表达式查询数据 调用函数查询数据 视频 https:/ ...
- pythonmysql查询转list_使用Python将Mysql的查询数据导出到文件的方法
mysql官方提供了很多种connector,其中包括python的connector. 下载地址在:http://dev.mysql.com/downloads/connector/python/ ...
- PyCharm 2018 for mac 数据库实战:链接SQLite、建表、添加、查询数据
一.前言 最近开始入门python,当然是要使用PyCharm,然后在项目中遇到.db数据库文件,双击打不开?网上找到了windows版本的教程,版本也比较旧,所以有空就来一发,当备忘也好~ 二.链接 ...
- java 实现违章_基于JAVA的车辆违章查询数据调用代码实例
基于JAVA的车辆违章查询数据调用代码实例 代码描述:基于JA V A的车辆违章查询数据调用代码实例 相关平台:聚合数据 import java.io.BufferedReader; import j ...
最新文章
- 资深专家给.NET初学者的学习建议(转)
- iPad+MacBook+安卓手机的图书馆工作方案!超高效率!堪比移动工作站!
- Flash 与 C#交互 (简单按钮事件)
- 5.8 正则化和数据标准化
- toj 4612 A Shooting Game
- Storm,Spark和Samza
- 贾跃亭自觉“无辜”;《绝地求生》外挂案件逮捕 34 人;VS Code 1.29 发布! | 极客头条...
- 2个JAVA程序能放在一起吗_求JAVA大神把2程序功能组合在一起
- Spark SQL External DataSource外部数据源操作流程
- Vegas导入PSD格式文件(素材)的方法
- 基于树莓派的语音机器人
- 挖矿木马分析之肉鸡竟是我自己
- 丢失数据文件和控制文件的恢复案例(zt)
- 开心农场简易外挂的实现
- 没有卑微的工作,只有卑微的工作态度
- 真北敏捷 | 明治维新与敏捷:思想、制度和器物
- 将微信收到的特定文件自动拷贝到指定目录
- java一个对象回收的过程_一个Java对象的回忆录:垃圾回收
- root过的手机保修吗,root过的手机保修吗苹果
- RestTemplate上传图片到指定接口