


生成完毕,项目结构如下:

运行看一下:

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace API.Models
{
public class Sandata
{
///
/// Desc:
/// Default:
/// Nullable:False
///
[SugarColumn(IsPrimaryKey=true)]
public int Id {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string sex {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public int tongshuai {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public int wuli {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public int zhili {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public int zhengzhi {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public int meili {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zhuyi {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zhengce {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zhengcedengji {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string peiou {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string name {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gexing1 {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gexing2 {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gexing3 {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gexing4 {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gexing5 {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string shengnian {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string dengchang {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zunian {get;set;}
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hjqy { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string fdlm { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string eyyx { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string qxgj { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gdzz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string sgml { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string cbzz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hzzdz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string gybww { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string csb { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string ztld { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hdlb { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hszc { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hbys { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string cjfl { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zzql { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string eftx { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string jmc { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string qfzh { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string yxjj { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string yxlw { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string yyyz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string ccds { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string lbtfz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hbzz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string tgzz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hfzz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string ylzz { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zfnm { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string qfwzy { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string zszb { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string shmw { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string htdl { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string hbgs { get; set; }
///
/// Desc:
/// Default:
/// Nullable:True
///
public string xsbl { get; set; }
///
/// alias:
/// Default:
/// Nullable:True
///
public string alias { get; set; }
///
/// biography1:
/// Default:
/// Nullable:True
///
public string biography1 { get; set; }
///
/// biography2:
/// Default:
/// Nullable:True
///
public string biography2 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string biography3 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string xiangxing { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string xueyuan { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string fuqin { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string muqin { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string shidai { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string yxd { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string zx { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string zf { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like1 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like2 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like3 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like4 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like5 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like6 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like7 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string like8 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike1 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike2 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike3 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike4 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike5 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike6 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike7 { get; set; }
///
/// biography3:
/// Default:
/// Nullable:True
///
public string dislike8 { get; set; }
public string qingyi { get; set; }
public string yexin { get; set; }
public string hanchao { get; set; }
public string haozhan { get; set; }
public string waijiao { get; set; }
public string wuzi { get; set; }
public string jianshe { get; set; }
}
}
namespace API.Models
{
///
/// 通用返回信息类
///
public class MessageModel<T>
{
///
/// 状态码
///
public int status { get; set; } = 200;
///
/// 页码
///
public int page { get; set; }
///
/// 数据条数
///
public int pageCount { get; set; }
///
/// 操作是否成功
///
public bool success { get; set; } = false;
///
/// 返回信息
///
public string msg { get; set; } = "服务器异常";
///
/// 返回数据集合
///
public T response { get; set; }
}
}
程序NuGet引入两个类库sqlsugar和Newtonsoft.Json



Helper.cs全部内容如下:
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace API.Helper
{
public class dbContext
{
private static SqlSugarClient _db = null;
///
/// test是数据库名
///
public static string ConnectionString = "server=localhost;uid=xxx;pwd=xxx;database=vue_test";
public static SqlSugarClient CretClient()
{
_db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = ConnectionString, //数据库连接字符串
DbType = DbType.MySql, //必填
IsAutoCloseConnection = false, //默认false
InitKeyType = InitKeyType.Attribute
});
return _db;
}
}
}
其中,DbType = DbType.MySql表示我们要连接的数据库为mysql,当然你也可以选择其他的数据库。或者切换多个不同的数据库(具体查看官方文档,既上面的sqlsugar),本篇中只使用一个数据库。server=localhost;uid=xxx;pwd=xxx;database=xxx为连接字符串,这是Mysql的连接字符串样式,不同的数据库是不同的,把里面的xxx换为你自己的数据库用户名,密码,使用的数据库名。
首先使用Helper.cs里新建的DbContext类来连接数据库
public static SqlSugarClient DbContext = dbContext.CretClient();

添加引用。此时,当前文件完整引用为:
using Newtonsoft.Json;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Text;
using System.Threading.Tasks;
using API.Models;
using System.Collections.Generic;
using System.Web;
using SqlSugar;
using System.Diagnostics;
using System.Reflection;
using API.Helper;
然后添加接口方法,例如添加一个分页查询的方法
///
/// 获取数据(分页)
///
///当前页数
///每页显示数据条数
///查询条件
public async Task<MessageModel<object>> GetAsync(int page = 1,int intPageSize=10,string key="")
{
Expressionable<Data> exp = Expressionable.Create<SanData>();
if (!string.IsNullOrEmpty(key))
{
exp.And(it => it.Name.Contains(key));
}
var result = DbContext.Queryable<SanData>()
.Where(exp.ToExpression())
.ToPageList(page, intPageSize);
return await Task.FromResult(new MessageModel<object>()
{
status = 200,
success = true,
msg = "获取成功",
response = result
});
}
项目上右键,选择“发布”

点击发布,目标位置里的app.publish文件夹就是我们后台打的包
如果是新的服务器可能需要启用IIS服务

注意1:接口在小程序使用之前,应保证可在外网访问。(这可在服务器平台的安全组设置以及服务器的出入口规则设置端口号来实现)
注意2:正式上线的小程序如要访问外网接口,接口必须是绑定在已经备案的域名上;并且,在小程序平台上的开发管理中,将开发设置下的服务器域名配置成你备案的域名。若是在本地微信开发者工具上访问,则先直接访问IP地址加端口号的形式也可以。
这里以获取亲爱/厌恶武将A的数据接口GetAllBeLove为例:
请求url为:BASE_URL /GetAllBeLove
参数有两个,name即武将名称,type即亲爱/厌恶
在小程序中获取数据:
//获取亲爱武将A的数据
let name="刘备";
let type="like";
let result_like = await fetchData(BASE_URL + 'GetAllBeLove', { name, type });
//请求数据
async function fetchData(url, data = {}) {
return new Promise((resolve, reject) => {
wx.request({
url,
data,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: "GET",
success: resolve,
fail: reject
});
});
}
获取结果结构如下:
