Redis缓存+Dapper,API方法

在这里插入图片描述

1、Dapper DAL层方法

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Model;
namespace DAL
{
public class UserDAL
{
//显示
public List Show()
{
using (SqlConnection conn = new SqlConnection(“Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True”))
{
return conn.Query(“select * from Yhxx”).ToList();
}

    }
    //添加
    public int Add(YhxxModel s)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True"))
        {
            return conn.Execute(string.Format("insert into Yhxx values('{0}','{1}','{2}','{3}','{4}')", s.LX, s.GG, s.GZZD,s.SH,s.TZ));
        }
    }
    //删除
    public int Del(int id)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True"))
        {
            return conn.Execute($"delete from Yhxx where Id={id}");
        }
    }
    //修改
    public int Upt(YhxxModel s)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True"))
        {
            return conn.Execute($"update Yhxx set LX='{s.LX}',GG='{s.GG}',GZZD='{s.GZZD}',SH='{s.SH}'");
        }
    }
    //反填
    public YhxxModel UptInfo(int id)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True"))
        {
            return conn.Query<YhxxModel>($"select * from Yhxx where Id={id}").FirstOrDefault();
        }

    }

}

}

2、控制器方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DAL;
using Model;
using WebApplication7_6_API.RedisCache;

namespace WebApplication7_6_API.Controllers
{
public class DefaultController : ApiController
{
UserDAL dal = new UserDAL();
RedisHelper redis = new RedisHelper();
[HttpGet]
[Route(“api/Show”)]
public List Show()
{
return dal.Show();
}
[HttpPost]
[Route(“api/Add”)]
public int Add(YhxxModel s)
{
return dal.Add(s);
}

    //删除
    [HttpGet]
    [Route("api/Del")]
    public int Del(int id)
    {
        return dal.Del(id);
    }
    //修改
    [HttpGet]
    [Route("api/Upt")]
    public int Upt(YhxxModel s)
    {
        return dal.Upt(s);
    }
    //反填
    [HttpGet]
    [Route("api/UptInfo")]
    public YhxxModel UptInfo(int id)
    {
        return dal.UptInfo(id);

    }

    //信息保存到Redis数据库中
    [HttpPost]
    [Route("api/AddUserInfo")]
    public int AddUserInfo(YhxxModel mod)
    {
        return redis.AddUserInfo("YhxxModel", mod);
    }

    //获取Redis缓存用户列表数据
    [HttpGet]
    [Route("api/GetUserInfos")]
    public List<YhxxModel> GetUserInfos(string userKey)
    {

        return redis.GetUserInfos(userKey);
    }

    [HttpGet]
    [Route("api/Get")]
    public YhxxModel Get(int pageSize = 3, int currentPage = 1)
    {
        if (currentPage < 1)
        {
            currentPage = 1;
        }
        var list = dal.Show();
        
        var count = list.Count();
        int page;
        if (count % pageSize == 0)
        {
            page = count / pageSize;
        }
        else
        {
            page = count / pageSize + 1;
        }

        if (currentPage > page)
        {
            currentPage = page;
        }
        list = list.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

        var m = new YhxxModel();
        m.list = list;
        m.totalCount = count;
        m.totalPage = page;
        m.currentPage = currentPage;
        return m;

    }

}

}

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页