Vue3和.NET实战(二)

三,WebApi访问数据库

新建类库项目Common
在这里插入图片描述

  • Sql sugar的使用

首先引入包

<PackageReference Include="SqlSugarCore" Version="5.1.3.43" />

创建如图的DbContext类

    /// <summary>
    /// 数据库连接对象
    /// </summary>
    public class DbContext
    {
        public static SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = "Data Source=.;Initial Catalog=donetweb2;Integrated Security=SSPI;TrustServerCertificate=yes",//连接符字串
            DbType = SqlSugar.DbType.SqlServer,//数据库类型
            IsAutoCloseConnection = true //不设成true要手动close
        });
    }

创建Service类库
在这里插入图片描述

  • 创建UserDto和InputUserDto,LoginDto
    public class InputUserDto
    {
        public string? QQ { get; set; }
        public string Mobile { get; set; }
        public string PassWord { get; set; }
        public string? NickName { get; set; }
        public string? UserSex { get; set; }
        public string ValidateKey { get; set; }
        public string? ValidateCode { get; set; }
    }
    public class UserDto
    {
        public string? QQ { get; set; }
        public string? Mobile { get; set; }
        public string? NickName { get; set; }
        public DateTime RegDate { get; set; }
        public DateTime? LastLoginTime { get; set; }
        public byte UserType { get; set; }
        public string? UserSex { get; set; }
        public byte Status { get; set; }
        public DateTime CreateTime { get; set; }
    }
    public class LoginDto
    {
        public string QQ { get; set; }
        public string PassWord { get; set; }
    }
  • 注册接口的实现
    public interface IUserService
    {
        // 登录
        Task<Users> CheckLogin(LoginDto login);
        //注册
        UserDto AddUser(InputUserDto input);


    }
        public class UserService : IUserService
    {
        private readonly IMapper _mapper;
        public UserService(IMapper mapper)
        {
            _mapper = mapper;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public async Task<Users> CheckLogin(LoginDto login)
        {
            return await DbContext.db.Queryable<Users>().FirstAsync(m => m.QQ.Equals(login.QQ) && m.PassWord.Equals(login.PassWord));
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UserDto AddUser(InputUserDto input)
        {
            Users user = TransInputDto(input);
            if (!DbContext.db.Queryable<Users>().Any(m => m.QQ.Equals(input.QQ) || m.Mobile.Equals(input.Mobile)))
            {
                DbContext.db.Insertable(user).ExecuteCommand();
                return _mapper.Map<UserDto>(user);
            }
            else throw new Exception("QQ 或者 手机号已存在");
        }
        private Users TransInputDto(InputUserDto input)
        {
            var user = _mapper.Map<Users>(input);
            var date = DateTime.Now;
            user.RegDate = date;
            user.CreateTime = date;
            user.LastModifyTime = date;
            user.LoginNum = 0;
            user.UserType = 1;
            user.Status = 1;
            user.CreatorId = 1;
            user.LastModifierId = 1;
            return user;
        }
      
    }
  • Automapper的作用和使用

1,引入程序包:AutoMapper、AutoMapper.Extensions.Microsoft.DependencyInjection

2,添加配置类:AutoMapperConfigs,管理映射关系

    /// <summary>
    /// 管理映射关系
    /// </summary>
    public class AutoMapperConfig:Profile
    {
        public AutoMapperConfig() 
        {
            CreateMap<Users, UserDto>();
            CreateMap<UserDto, Users>();
            CreateMap<InputUserDto, Users>();
        }
    }

Automapper的主要作用:实体类到dto的映射

注入配置Automapper映射

builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));

好,暂时就这么多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

栀梦星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值