新增读取文档帮助
129
Examples/website/FreeSql.Site.UI/wwwroot/file/codefirst.md
Normal file
@ -0,0 +1,129 @@
|
||||
# CodeFirst
|
||||
|
||||
| 数据库 | 支持的类型类型 |
|
||||
| - | - |
|
||||
| MySql | bool, sbyte, short, int, long, byte, ushort, uint, ulong, double, float, decimal, Guid, TimeSpan, DateTime<br>bool?, sbyte?, short?, int?, long?, byte?, ushort?, uint?, ulong?, double?, float?, decimal?, Guid?, TimeSpan?, DateTime?<br>byte[], string, Enum & FlagsEnum<br>MygisPoint, MygisLineString, MygisPolygon, MygisMultiPoint, MygisMultiLineString, MygisMultiPolygon |
|
||||
| SqlServer | bool, sbyte, short, int, long, byte, ushort, uint, ulong, double, float, decimal, Guid, TimeSpan, DateTime, DateTimeOffset<br>bool?, sbyte?, short?, int?, long?, byte?, ushort?, uint?, ulong?, double?, float?, decimal?, Guid?, TimeSpan?, DateTime?, DateTimeOffset?<br>byte[], string, Enum & FlagsEnum |
|
||||
|
||||
|
||||
```csharp
|
||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
```
|
||||
|
||||
### 自动同步实体结构【开发环境必备】
|
||||
|
||||
自动同步实体结构到数据库,程序运行中检查实体表是否存在,然后创建或修改
|
||||
|
||||
```csharp
|
||||
fsql.CodeFirst.IsAutoSyncDataStructure = true;
|
||||
```
|
||||
|
||||
> 此功能默认为开启状态,发布正式环境后,请修改此设置
|
||||
|
||||
> 虽然【自动同步实体结构】功能开发非常好用,但是有个坏处,就是数据库后面会很乱,没用的字段一大堆
|
||||
|
||||
### 手工同步实体结构
|
||||
|
||||
| 实体&表对比 | 添加 | 改名 | 删除 |
|
||||
| - | - | - | - |
|
||||
| | √ | √ | X |
|
||||
|
||||
| 实体属性&字段对比 | 添加 | 修改可空 | 修改自增 | 修改类型 | 改名 | 删除 |
|
||||
| - | - | - | - | - | - | - |
|
||||
| | √ | √ | √ | √ | √ | X |
|
||||
|
||||
> 为了保证安全,不提供删除字段
|
||||
|
||||
|
||||
1、提供方法对比实体,与数据库中的变化部分
|
||||
|
||||
```csharp
|
||||
var t1 = mysql.CodeFirst.GetComparisonDDLStatements<Topic>();
|
||||
|
||||
class Topic {
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public TestTypeInfo Type { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
public ushort fusho { get; set; }
|
||||
}
|
||||
```
|
||||
```sql
|
||||
CREATE TABLE IF NOT EXISTS `cccddd`.`Topic` (
|
||||
`Id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
`Clicks` INT(11) NOT NULL,
|
||||
`Title` VARCHAR(255),
|
||||
`CreateTime` DATETIME NOT NULL,
|
||||
`fusho` SMALLINT(5) UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`Id`)
|
||||
) Engine=InnoDB CHARACTER SET utf8;
|
||||
```
|
||||
|
||||
2、指定实体的表名
|
||||
|
||||
指定 Name 后,实体类名变化不影响数据库对应的表
|
||||
```csharp
|
||||
[Table(Name = "tb_topic111")]
|
||||
class Topic {
|
||||
//...
|
||||
}
|
||||
```
|
||||
|
||||
3、无指定实体的表名,修改实体类名
|
||||
|
||||
指定数据库旧的表名,修改实体命名时,同时设置此参数为修改之前的值,CodeFirst才可以正确修改数据库表;否则将视为【创建新表】
|
||||
|
||||
```csharp
|
||||
[Table(OldName = "Topic")]
|
||||
class Topic2 {
|
||||
//...
|
||||
}
|
||||
```
|
||||
```sql
|
||||
ALTER TABLE `cccddd`.`Topic` RENAME TO `cccddd`.`Topic2`;
|
||||
```
|
||||
|
||||
4、修改属性的类型
|
||||
|
||||
把 Id 类型改为 uint 后
|
||||
```sql
|
||||
ALTER TABLE `cccddd`.`Topic2` MODIFY `Id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;
|
||||
```
|
||||
```csharp
|
||||
[Column(DbType = "varchar(128)")]
|
||||
public string Title { get; set; }
|
||||
```
|
||||
```sql
|
||||
ALTER TABLE `cccddd`.`Topic2` MODIFY `Title2` VARCHAR(128);
|
||||
```
|
||||
|
||||
5、指定属性的字段名
|
||||
|
||||
这样指定后,修改实体的属性名不影响数据库对应的列
|
||||
```csharp
|
||||
[Column(Name = "titl2")]
|
||||
public string Title { get; set; }
|
||||
```
|
||||
|
||||
6、无指定属性的字段名,修改属性名
|
||||
|
||||
指定数据库旧的列名,修改实体属性命名时,同时设置此参数为修改之前的值,CodeFirst才可以正确修改数据库字段;否则将视为【新增字段】
|
||||
|
||||
```csharp
|
||||
[Column(OldName = "Title2")]
|
||||
public string Title { get; set; }
|
||||
```
|
||||
```sql
|
||||
ALTER TABLE `cccddd`.`Topic2` CHANGE COLUMN `Title2` `Title` VARCHAR(255);
|
||||
```
|
||||
|
||||
7、提供方法同步结构
|
||||
|
||||
```csharp
|
||||
var t2 = fsql.CodeFirst.SyncStructure<Topic>();
|
||||
//同步实体类型到数据库
|
||||
```
|
109
Examples/website/FreeSql.Site.UI/wwwroot/file/dbfirst.md
Normal file
@ -0,0 +1,109 @@
|
||||
# DbFirst
|
||||
|
||||
```csharp
|
||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
```
|
||||
|
||||
### 获取所有数据库
|
||||
|
||||
```csharp
|
||||
var t1 = fsql.DbFirst.GetDatabases();
|
||||
//返回字符串数组, ["cccddd", "test"]
|
||||
```
|
||||
|
||||
### 获取指定数据库的表信息
|
||||
|
||||
```csharp
|
||||
var t2 = fsql.DbFirst.GetTablesByDatabase(fsql.DbFirst.GetDatabases()[0]);
|
||||
//返回包括表、列详情、主键、唯一键、索引、外键
|
||||
```
|
||||
|
||||
# 生成器
|
||||
|
||||
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||
|
||||
| 模板名称 | 路径 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||
| ------------- | - | - |- | - |- | - |- |
|
||||
| simple-entity | ../Templates/MySql/simple-entity | √ | X | X | √ | X | X |
|
||||
| simple-entity-navigation-object | ../Templates/MySql/simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||
| rich-entity-navigation-object | ../Templates/MySql/rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||
|
||||
> 更多模板逐步开发中。。。
|
||||
|
||||
```csharp
|
||||
//创建模板生成类现实
|
||||
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||
gen.Build(fsql.DbFirst,
|
||||
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||
"cccddd" //数据库
|
||||
);
|
||||
```
|
||||
|
||||
## 模板语法
|
||||
|
||||
```html
|
||||
<html>
|
||||
<head>
|
||||
<title>{#title}</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<!--绑定表达式-->
|
||||
{#表达式}
|
||||
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||
|
||||
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||
{include ../header.html}
|
||||
<div @for="i 1, 101">
|
||||
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||
<p @else="">ccc {#i}</p>
|
||||
</div>
|
||||
|
||||
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||
{module module_name1 parms1, 2, 3...}
|
||||
{/module}
|
||||
{module module_name2 parms1, 2, 3...}
|
||||
{/module}
|
||||
|
||||
<!--使用模块-->
|
||||
{import ../module.html as myname}
|
||||
{#myname.module_name(parms1, 2, 3...)}
|
||||
|
||||
<!--继承-->
|
||||
{extends ../inc/layout.html}
|
||||
{block body}{/block}
|
||||
|
||||
<!--嵌入代码块-->
|
||||
{%
|
||||
for (var a = 0; a < 100; a++)
|
||||
print(a);
|
||||
%}
|
||||
|
||||
<!--条件分支-->
|
||||
{if i === 50}
|
||||
{elseif i > 60}
|
||||
{else}
|
||||
{/if}
|
||||
|
||||
<!--三种循环-->
|
||||
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||
|
||||
{for item,index in items} 可选参数称 index
|
||||
可自定义名 {for item2, index99 in 数组表达式}
|
||||
|
||||
{for key,item,index on json} 可选参数 item, index,
|
||||
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||
{/for}
|
||||
|
||||
<!--不被解析-->
|
||||
{miss}
|
||||
此块内容不被bmw.js解析
|
||||
{/miss}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
```
|
82
Examples/website/FreeSql.Site.UI/wwwroot/file/delete.md
Normal file
@ -0,0 +1,82 @@
|
||||
# 删除数据
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
| - | - | - | - |
|
||||
| Where | \<this\> | Lambda | 表达式条件,仅支持实体基础成员(不包含导航对象) |
|
||||
| Where | \<this\> | string, parms | 原生sql语法条件,Where("id = ?id", new { id = 1 }) |
|
||||
| Where | \<this\> | T1 \| IEnumerable<T1> | 传入实体或集合,将其主键作为条件 |
|
||||
| WhereExists | \<this\> | ISelect | 子查询是否存在 |
|
||||
| ToSql | string | | 返回即将执行的SQL语句 |
|
||||
| ExecuteAffrows | long | | 执行SQL语句,返回影响的行数 |
|
||||
| ExecuteDeleted | List\<T1\> | | 执行SQL语句,返回被删除的记录 |
|
||||
|
||||
### 测试代码
|
||||
|
||||
```csharp
|
||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
IDelete<Topic> delete => fsql.Delete<Topic>();
|
||||
|
||||
[Table(Name = "tb_topic")]
|
||||
class Topic {
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public TestTypeInfo Type { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
### 动态条件
|
||||
```csharp
|
||||
Delete<Topic>(object dywhere)
|
||||
```
|
||||
dywhere 支持
|
||||
|
||||
* 主键值
|
||||
* new[] { 主键值1, 主键值2 }
|
||||
* Topic对象
|
||||
* new[] { Topic对象1, Topic对象2 }
|
||||
* new { id = 1 }
|
||||
|
||||
```csharp
|
||||
var t1 = fsql.Delete<Topic>(new[] { 1, 2 }).ToSql();
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2)
|
||||
|
||||
var t2 = fsql.Delete<Topic>(new Topic { Id = 1, Title = "test" }).ToSql();
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` = 1)
|
||||
|
||||
var t3 = fsql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql();
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2)
|
||||
|
||||
var t4 = fsql.Delete<Topic>(new { id = 1 }).ToSql();
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` = 1)
|
||||
```
|
||||
|
||||
### 删除条件
|
||||
|
||||
```csharp
|
||||
var t5 = delete.Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` = 1)
|
||||
|
||||
var t6 = delete.Where("id = ?id", new { id = 1 }).ToSql().Replace("\r\n", "");
|
||||
//DELETE FROM `tb_topic` WHERE (id = ?id)
|
||||
|
||||
var item = new Topic { Id = 1, Title = "newtitle" };
|
||||
var t7 = delete.Where(item).ToSql().Replace("\r\n", "");
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` = 1)
|
||||
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
var t8 = delete.Where(items).ToSql().Replace("\r\n", "");
|
||||
//DELETE FROM `tb_topic` WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
|
||||
```
|
||||
|
||||
### 执行命令
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
| - | - | - | - |
|
||||
| ExecuteAffrows | long | | 执行SQL语句,返回影响的行数 |
|
||||
| ExecuteDeleted | List\<T1\> | | 执行SQL语句,返回被删除的记录 |
|
94
Examples/website/FreeSql.Site.UI/wwwroot/file/generator.md
Normal file
@ -0,0 +1,94 @@
|
||||
# 生成器
|
||||
|
||||
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||
|
||||
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||
| ------------- | - | - |- | - |- | - |
|
||||
| simple-entity | √ | X | X | √ | X | X |
|
||||
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||
|
||||
模板在项目目录:/Templates/MySql
|
||||
|
||||
> 更多模板逐步开发中。。。
|
||||
|
||||
```csharp
|
||||
//定义 mysql FreeSql
|
||||
var mysql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
|
||||
//创建模板生成类现实
|
||||
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||
gen.Build(mysql.DbFirst,
|
||||
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||
"cccddd" //数据库
|
||||
);
|
||||
```
|
||||
|
||||
## 模板语法
|
||||
|
||||
```html
|
||||
<html>
|
||||
<head>
|
||||
<title>{#title}</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<!--绑定表达式-->
|
||||
{#表达式}
|
||||
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||
|
||||
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||
{include ../header.html}
|
||||
<div @for="i 1, 101">
|
||||
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||
<p @else="">ccc {#i}</p>
|
||||
</div>
|
||||
|
||||
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||
{module module_name1 parms1, 2, 3...}
|
||||
{/module}
|
||||
{module module_name2 parms1, 2, 3...}
|
||||
{/module}
|
||||
|
||||
<!--使用模块-->
|
||||
{import ../module.html as myname}
|
||||
{#myname.module_name(parms1, 2, 3...)}
|
||||
|
||||
<!--继承-->
|
||||
{extends ../inc/layout.html}
|
||||
{block body}{/block}
|
||||
|
||||
<!--嵌入代码块-->
|
||||
{%
|
||||
for (var a = 0; a < 100; a++)
|
||||
print(a);
|
||||
%}
|
||||
|
||||
<!--条件分支-->
|
||||
{if i === 50}
|
||||
{elseif i > 60}
|
||||
{else}
|
||||
{/if}
|
||||
|
||||
<!--三种循环-->
|
||||
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||
|
||||
{for item,index in items} 可选参数称 index
|
||||
可自定义名 {for item2, index99 in 数组表达式}
|
||||
|
||||
{for key,item,index on json} 可选参数 item, index,
|
||||
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||
{/for}
|
||||
|
||||
<!--不被解析-->
|
||||
{miss}
|
||||
此块内容不被bmw.js解析
|
||||
{/miss}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
```
|
79
Examples/website/FreeSql.Site.UI/wwwroot/file/insert.md
Normal file
@ -0,0 +1,79 @@
|
||||
# 插入数据
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
| - | - | - | - |
|
||||
| AppendData | \<this\> | T1 \| IEnumerable<T1> | 追加准备插入的实体 |
|
||||
| InsertColumns | \<this\> | Lambda | 只插入的列 |
|
||||
| IgnoreColumns | \<this\> | Lambda | 忽略的列 |
|
||||
| ToSql | string | | 返回即将执行的SQL语句 |
|
||||
| ExecuteAffrows | long | | 执行SQL语句,返回影响的行数 |
|
||||
| ExecuteIdentity | long | | 执行SQL语句,返回自增值 |
|
||||
| ExecuteInserted | List\<T1\> | | 执行SQL语句,返回插入后的记录 |
|
||||
|
||||
### 列优先级
|
||||
|
||||
> 全部列 < 指定列(InsertColumns) < 忽略列(IgnoreColumns)
|
||||
|
||||
### 测试代码
|
||||
|
||||
```csharp
|
||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
IInsert<Topic> insert => fsql.Insert<Topic>();
|
||||
|
||||
[Table(Name = "tb_topic")]
|
||||
class Topic {
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public TestTypeInfo Type { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
```
|
||||
|
||||
### 插入
|
||||
|
||||
```csharp
|
||||
var t1 = insert.AppendData(items.First()).ToSql();
|
||||
//INSERT INTO `tb_topic`(`Clicks`, `Title`, `CreateTime`) VALUES(?Clicks0, ?Title0, ?CreateTime0)
|
||||
```
|
||||
|
||||
### 批量插入
|
||||
|
||||
```csharp
|
||||
var t2 = insert.AppendData(items).ToSql();
|
||||
//INSERT INTO `tb_topic`(`Clicks`, `Title`, `CreateTime`) VALUES(?Clicks0, ?Title0, ?CreateTime0), (?Clicks1, ?Title1, ?CreateTime1), (?Clicks2, ?Title2, ?CreateTime2), (?Clicks3, ?Title3, ?CreateTime3), (?Clicks4, ?Title4, ?CreateTime4), (?Clicks5, ?Title5, ?CreateTime5), (?Clicks6, ?Title6, ?CreateTime6), (?Clicks7, ?Title7, ?CreateTime7), (?Clicks8, ?Title8, ?CreateTime8), (?Clicks9, ?Title9, ?CreateTime9)
|
||||
```
|
||||
|
||||
### 只想插入指定的列
|
||||
|
||||
```csharp
|
||||
var t3 = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||
//INSERT INTO `tb_topic`(`Title`) VALUES(?Title0), (?Title1), (?Title2), (?Title3), (?Title4), (?Title5), (?Title6), (?Title7), (?Title8), (?Title9)
|
||||
|
||||
var t4 = insert.AppendData(items).InsertColumns(a =>new { a.Title, a.Clicks }).ToSql();
|
||||
//INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(?Clicks0, ?Title0), (?Clicks1, ?Title1), (?Clicks2, ?Title2), (?Clicks3, ?Title3), (?Clicks4, ?Title4), (?Clicks5, ?Title5), (?Clicks6, ?Title6), (?Clicks7, ?Title7), (?Clicks8, ?Title8), (?Clicks9, ?Title9)
|
||||
```
|
||||
|
||||
### 忽略列
|
||||
|
||||
```csharp
|
||||
var t5 = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||
//INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(?Clicks0, ?Title0), (?Clicks1, ?Title1), (?Clicks2, ?Title2), (?Clicks3, ?Title3), (?Clicks4, ?Title4), (?Clicks5, ?Title5), (?Clicks6, ?Title6), (?Clicks7, ?Title7), (?Clicks8, ?Title8), (?Clicks9, ?Title9)
|
||||
|
||||
var t6 = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).ToSql();
|
||||
///INSERT INTO `tb_topic`(`Clicks`) VALUES(?Clicks0), (?Clicks1), (?Clicks2), (?Clicks3), (?Clicks4), (?Clicks5), (?Clicks6), (?Clicks7), (?Clicks8), (?Clicks9)
|
||||
```
|
||||
|
||||
### 执行命令
|
||||
|
||||
| 方法 | 返回值 | 描述 |
|
||||
| - | - | - |
|
||||
| ExecuteAffrows | long | 执行SQL语句,返回影响的行数 |
|
||||
| ExecuteIdentity | long | 执行SQL语句,返回自增值 |
|
||||
| ExecuteInserted | List\<T1\> | 执行SQL语句,返回插入后的记录 |
|
211
Examples/website/FreeSql.Site.UI/wwwroot/file/select.md
Normal file
@ -0,0 +1,211 @@
|
||||
# 查询数据
|
||||
|
||||
## 测试代码
|
||||
|
||||
```csharp
|
||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
ISelect<Topic> select => fsql.Select<Topic>();
|
||||
|
||||
[Table(Name = "tb_topic")]
|
||||
class Topic {
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public int TestTypeInfoGuid { get; set; }
|
||||
public TestTypeInfo Type { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
class TestTypeInfo {
|
||||
public int Guid { get; set; }
|
||||
public int ParentId { get; set; }
|
||||
public TestTypeParentInfo Parent { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
class TestTypeParentInfo {
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public List<TestTypeInfo> Types { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
# Where
|
||||
|
||||
### 表达式函数支持
|
||||
|
||||
#### String 对象方法
|
||||
StartsWith, EndsWith, Contains, ToLower, ToUpper, Substring, Length, IndexOf, PadLeft, PadRight, Trim, TrimStart, TrimEnd, Replace, CompareTo
|
||||
|
||||
#### Math 方法
|
||||
...
|
||||
|
||||
### 单表
|
||||
```csharp
|
||||
var sql = select.Where(a => a.Id == 10).ToSql();
|
||||
///SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.`Id` = 10)
|
||||
|
||||
sql = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100).ToSql();
|
||||
///SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.`Id` = 10 AND a.`Id` > 10 OR a.`Clicks` > 100)
|
||||
```
|
||||
|
||||
### 多表,使用导航属性
|
||||
```csharp
|
||||
sql = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TestTypeInfoGuid).ToSql();
|
||||
///SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a, `TestTypeInfo` a__Type WHERE (a__Type.`Name` = 'typeTitle' AND a__Type.`Guid` = a.`TestTypeInfoGuid`)
|
||||
|
||||
sql = select.Where(a => a.Type.Parent.Name == "tparent").ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a, `TestTypeInfo` a__Type, `TestTypeParentInfo` a__Type__Parent WHERE (a__Type__Parent.`Name` = 'tparent')
|
||||
```
|
||||
|
||||
### 多表,没有导航属性
|
||||
```csharp
|
||||
sql = select.Where<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "typeTitle").ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, b.`Guid` as4, b.`ParentId` as5, b.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a, `TestTypeInfo` b WHERE (b.`Guid` = a.`TestTypeInfoGuid` AND b.`Name` = 'typeTitle')
|
||||
|
||||
sql = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent").ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a, `TestTypeParentInfo` c WHERE (c.`Name` = 'tparent')
|
||||
```
|
||||
|
||||
### 多表,任意查
|
||||
```csharp
|
||||
sql = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||
.Where(a => a.Id == 10 && c.Name == "xxx")
|
||||
.Where(a => b.ParentId == 20)).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, b.`Guid` as4, b.`ParentId` as5, b.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a, `TestTypeParentInfo` c, `TestTypeInfo` b WHERE (a.`Id` = 10 AND c.`Name` = 'xxx') AND (b.`ParentId` = 20)
|
||||
```
|
||||
|
||||
### 原生SQL
|
||||
```csharp
|
||||
sql = select.Where("a.clicks > 100 && a.id = ?id", new { id = 10 }).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.clicks > 100 && a.id = ?id)
|
||||
```
|
||||
|
||||
> 以上条件查询,支持 WhereIf
|
||||
|
||||
# 联表
|
||||
|
||||
### 使用导航属性联表
|
||||
```csharp
|
||||
sql = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid`
|
||||
|
||||
sql = select
|
||||
.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid)
|
||||
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`
|
||||
```
|
||||
|
||||
### 没有导航属性联表
|
||||
```csharp
|
||||
sql = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, b.`Guid` as4, b.`ParentId` as5, b.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TestTypeInfoGuid`
|
||||
|
||||
sql = select
|
||||
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid)
|
||||
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, b.`Guid` as4, b.`ParentId` as5, b.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TestTypeInfoGuid` LEFT JOIN `TestTypeParentInfo` c ON c.`Id` = b.`ParentId`
|
||||
```
|
||||
|
||||
### 联表任意查
|
||||
```csharp
|
||||
sql = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||
.LeftJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||
.LeftJoin(a => b.ParentId == c.Id)).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, b.`Guid` as4, b.`ParentId` as5, b.`Name` as6, a.`Title` as7, a.`CreateTime` as8 FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON a.`TestTypeInfoGuid` = b.`Guid` LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`
|
||||
```
|
||||
|
||||
### 原生SQL联表
|
||||
```csharp
|
||||
sql = select.LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname", new { bname = "xxx" }).ToSql();
|
||||
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname
|
||||
```
|
||||
|
||||
# 查询数据
|
||||
|
||||
### 返回 List
|
||||
```csharp
|
||||
List<Topic> t1 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList();
|
||||
```
|
||||
|
||||
### 返回 List + 导航属性的数据
|
||||
```csharp
|
||||
List<Topic> t2 = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToList();
|
||||
//此时会返回普通字段 + 导航对象 Type 的数据
|
||||
```
|
||||
|
||||
### 指定字段返回
|
||||
```csharp
|
||||
//返回一个字段
|
||||
List<int> t3 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList(a => a.Id);
|
||||
|
||||
//返回匿名类
|
||||
List<匿名类> t4 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList(a => new { a.Id, a.Title });
|
||||
|
||||
//返回元组
|
||||
List<(int, string)> t5 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList<(int, string)>("id, title");
|
||||
```
|
||||
|
||||
### 执行SQL返回数据
|
||||
```csharp
|
||||
class xxx {
|
||||
public int Id { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string Title2 { get; set; }
|
||||
}
|
||||
|
||||
List<xxx> t6 = fsql.Ado.Query<xxx>("select * from song");
|
||||
List<(int, string ,string)> t7 = fsql.Ado.Query<(int, string, string)>("select * from song");
|
||||
List<dynamic> t8 = fsql.Ado.Query<dynamic>("select * from song");
|
||||
```
|
||||
|
||||
# 更多文档整理中。。。
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
| ------------- | - | - | - |
|
||||
| ToSql | string | | 返回即将执行的SQL语句 |
|
||||
| ToList | List<T1> | | 执行SQL查询,返回 T1 实体所有字段的记录,若存在导航属性则一起查询返回,记录不存在时返回 Count 为 0 的列表 |
|
||||
| ToList\<T\> | List\<T\> | Lambda | 执行SQL查询,返回指定字段的记录,记录不存在时返回 Count 为 0 的列表 |
|
||||
| ToList\<T\> | List\<T\> | string field | 执行SQL查询,返回 field 指定字段的记录,并以元组或基础类型(int,string,long)接收,记录不存在时返回 Count 为 0 的列表 |
|
||||
| ToOne | T1 | | 执行SQL查询,返回 T1 实体所有字段的第一条记录,记录不存在时返回 null |
|
||||
| Any | bool | | 执行SQL查询,是否有记录 |
|
||||
| Sum | T | Lambda | 指定一个列求和 |
|
||||
| Min | T | Lambda | 指定一个列求最小值 |
|
||||
| Max | T | Lambda | 指定一个列求最大值 |
|
||||
| Avg | T | Lambda | 指定一个列求平均值 |
|
||||
| 【分页】 |
|
||||
| Count | long | | 查询的记录数量 |
|
||||
| Count | \<this\> | out long | 查询的记录数量,以参数out形式返回 |
|
||||
| Skip | \<this\> | int offset | 查询向后偏移行数 |
|
||||
| Offset | \<this\> | int offset | 查询向后偏移行数 |
|
||||
| Limit | \<this\> | int limit | 查询多少条数据 |
|
||||
| Take | \<this\> | int limit | 查询多少条数据 |
|
||||
| Page | \<this\> | int pageIndex, int pageSize | 分页 |
|
||||
| 【条件】 |
|
||||
| Where | \<this\> | Lambda | 支持多表查询表达式 |
|
||||
| WhereIf | \<this\> | bool, Lambda | 支持多表查询表达式 |
|
||||
| Where | \<this\> | string, parms | 原生sql语法条件,Where("id = ?id", new { id = 1 }) |
|
||||
| WhereIf | \<this\> | bool, string, parms | 原生sql语法条件,WhereIf(true, "id = ?id", new { id = 1 }) |
|
||||
| WhereLike | \<this\> | Lambda, string, bool | like 查询条件,where title like '%xxx%' or content like '%xxx%' |
|
||||
| 【分组】 |
|
||||
| GroupBy | \<this\> | Lambda | 按选择的列分组,GroupBy(a => a.Name) | GroupBy(a => new{a.Name,a.Time}) | GroupBy(a => new[]{"name","time"}) |
|
||||
| GroupBy | \<this\> | string, parms | 按原生sql语法分组,GroupBy("concat(name, ?cc)", new { cc = 1 }) |
|
||||
| Having | \<this\> | string, parms | 按原生sql语法聚合条件过滤,Having("count(name) = ?cc", new { cc = 1 }) |
|
||||
| 【排序】 |
|
||||
| OrderBy | \<this\> | Lambda | 按列排序,OrderBy(a => a.Time) |
|
||||
| OrderByDescending | \<this\> | Lambda | 按列倒向排序,OrderByDescending(a => a.Time) |
|
||||
| OrderBy | \<this\> | string, parms | 按原生sql语法排序,OrderBy("count(name) + ?cc", new { cc = 1 }) |
|
||||
| 【联表】 |
|
||||
| LeftJoin | \<this\> | Lambda | 左联查询,可使用导航属性,或指定关联的实体类型 |
|
||||
| InnerJoin | \<this\> | Lambda | 联接查询,可使用导航属性,或指定关联的实体类型 |
|
||||
| RightJoin | \<this\> | Lambda | 右联查询,可使用导航属性,或指定关联的实体类型 |
|
||||
| LeftJoin | \<this\> | string, parms | 左联查询,使用原生sql语法,LeftJoin("type b on b.id = a.id and b.clicks > ?clicks", new { clicks = 1 }) |
|
||||
| InnerJoin | \<this\> | string, parms | 联接查询,使用原生sql语法,InnerJoin("type b on b.id = a.id and b.clicks > ?clicks", new { clicks = 1 }) |
|
||||
| RightJoin | \<this\> | string, parms | 右联查询,使用原生sql语法,RightJoin("type b on b.id = a.id and b.clicks > ?clicks", new { clicks = 1 }) |
|
||||
| From | \<this\> | Lambda | 多表查询,3个表以上使用非常方便,目前设计最大支持10个表 |
|
||||
| 【其他】 |
|
||||
| As | \<this\> | string alias = "a" | 指定别名 |
|
||||
| Master | \<this\> | | 指定从主库查询(默认查询从库) |
|
||||
| Caching | \<this\> | int seconds, string key = null | 缓存查询结果 |
|
129
Examples/website/FreeSql.Site.UI/wwwroot/file/update.md
Normal file
@ -0,0 +1,129 @@
|
||||
# 更新数据
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
| - | - | - | - |
|
||||
| SetSource | \<this\> | T1 \| IEnumerable<T1> | 更新数据,设置更新的实体 |
|
||||
| IgnoreColumns | \<this\> | Lambda | 忽略的列 |
|
||||
| Set | \<this\> | Lambda, value | 设置列的新值,Set(a => a.Name, "newvalue") |
|
||||
| Set | \<this\> | Lambda | 设置列的的新值为基础上增加,Set(a => a.Clicks + 1),相当于 clicks=clicks+1; |
|
||||
| SetRaw | \<this\> | string, parms | 设置值,自定义SQL语法,SetRaw("title = ?title", new { title = "newtitle" }) |
|
||||
| Where | \<this\> | Lambda | 表达式条件,仅支持实体基础成员(不包含导航对象) |
|
||||
| Where | \<this\> | string, parms | 原生sql语法条件,Where("id = ?id", new { id = 1 }) |
|
||||
| Where | \<this\> | T1 \| IEnumerable<T1> | 传入实体或集合,将其主键作为条件 |
|
||||
| WhereExists | \<this\> | ISelect | 子查询是否存在 |
|
||||
| ToSql | string | | 返回即将执行的SQL语句 |
|
||||
| ExecuteAffrows | long | | 执行SQL语句,返回影响的行数 |
|
||||
| ExecuteUpdated | List\<T1\> | | 执行SQL语句,返回更新后的记录 |
|
||||
|
||||
### 列优先级
|
||||
|
||||
> 全部列 < 指定列(Set/SetRaw) < 忽略列(IgnoreColumns)
|
||||
|
||||
### 测试代码
|
||||
|
||||
```csharp
|
||||
[Table(Name = "tb_topic")]
|
||||
class Topic {
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public TestTypeInfo Type { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||
.Build();
|
||||
IUpdate<Topic> update => fsql.Update<Topic>();
|
||||
```
|
||||
|
||||
### 动态条件
|
||||
```csharp
|
||||
Update<Topic>(object dywhere)
|
||||
```
|
||||
dywhere 支持
|
||||
|
||||
* 主键值
|
||||
* new[] { 主键值1, 主键值2 }
|
||||
* Topic对象
|
||||
* new[] { Topic对象1, Topic对象2 }
|
||||
* new { id = 1 }
|
||||
|
||||
### 更新指定列
|
||||
```csharp
|
||||
var t1 = fsql.Update<Topic>(1).Set(a => a.CreateTime, DateTime.Now).ToSql();
|
||||
//UPDATE `tb_topic` SET `CreateTime` = '2018-12-08 00:04:59' WHERE (`Id` = 1)
|
||||
```
|
||||
|
||||
### 更新指定列,累加
|
||||
```csharp
|
||||
var t2 = fsql.Update<Topic>(1).Set(a => a.Clicks + 1).ToSql();
|
||||
//UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`,0) + 1 WHERE (`Id` = 1)
|
||||
```
|
||||
|
||||
### 保存实体
|
||||
```csharp
|
||||
var item = new Topic { Id = 1, Title = "newtitle" };
|
||||
var t3 = update.SetSource(item).ToSql();
|
||||
//UPDATE `tb_topic` SET `Clicks` = ?p_0, `Title` = ?p_1, `CreateTime` = ?p_2 WHERE (`Id` = 1)
|
||||
```
|
||||
|
||||
### 保存实体,忽略一些列
|
||||
```csharp
|
||||
var t4 = update.SetSource(item).IgnoreColumns(a => a.Clicks).ToSql();
|
||||
//UPDATE `tb_topic` SET `Title` = ?p_0, `CreateTime` = ?p_1 WHERE (`Id` = 1)
|
||||
var t5 = update.SetSource(item).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql();
|
||||
//UPDATE `tb_topic` SET `Title` = ?p_0 WHERE (`Id` = 1)
|
||||
```
|
||||
|
||||
### 批量保存
|
||||
```csharp
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var t6 = update.SetSource(items).ToSql();
|
||||
//UPDATE `tb_topic` SET `Clicks` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END, `Title` = CASE `Id` WHEN 1 THEN ?p_10 WHEN 2 THEN ?p_11 WHEN 3 THEN ?p_12 WHEN 4 THEN ?p_13 WHEN 5 THEN ?p_14 WHEN 6 THEN ?p_15 WHEN 7 THEN ?p_16 WHEN 8 THEN ?p_17 WHEN 9 THEN ?p_18 WHEN 10 THEN ?p_19 END, `CreateTime` = CASE `Id` WHEN 1 THEN ?p_20 WHEN 2 THEN ?p_21 WHEN 3 THEN ?p_22 WHEN 4 THEN ?p_23 WHEN 5 THEN ?p_24 WHEN 6 THEN ?p_25 WHEN 7 THEN ?p_26 WHEN 8 THEN ?p_27 WHEN 9 THEN ?p_28 WHEN 10 THEN ?p_29 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
|
||||
```
|
||||
|
||||
> 批量保存的场景,先查询20条记录,根据本地很复杂的规则把集合的值改完后
|
||||
|
||||
> 传统做法是循环20次保存,用 case when 只要一次就行
|
||||
|
||||
### 批量保存,忽略一些列
|
||||
```csharp
|
||||
var t7 = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql();
|
||||
//UPDATE `tb_topic` SET `Title` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
|
||||
```
|
||||
|
||||
### 批量更新指定列
|
||||
```csharp
|
||||
var t8 = update.SetSource(items).Set(a => a.CreateTime, DateTime.Now).ToSql();
|
||||
//UPDATE `tb_topic` SET `CreateTime` = ?p_0 WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
|
||||
```
|
||||
|
||||
> 指定列更新后,批量保存将失效
|
||||
|
||||
### 更新条件
|
||||
|
||||
> 除了顶上介绍的 dywhere 构造参数外,还支持 Where lambda/sql 方法
|
||||
|
||||
```csharp
|
||||
var t9 = update.Set(a => a.Title, "新标题").Where(a => a.Id == 1).ToSql();
|
||||
//UPDATE `tb_topic` SET `Title` = '新标题' WHERE (Id = 1)
|
||||
```
|
||||
|
||||
### 自定义SQL
|
||||
|
||||
```csharp
|
||||
var t10 = update.SetRaw("Title = {0}", "新标题").Where("Id = {0}", 1).ToSql();
|
||||
//UPDATE `tb_topic` SET Title = '新标题' WHERE (Id = 1)
|
||||
//sql语法条件,参数使用 {0},与 string.Format 保持一致,无须加单引号,错误的用法:'{0}'
|
||||
```
|
||||
|
||||
### 执行命令
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
| - | - | - | - |
|
||||
| ExecuteAffrows | long | | 执行SQL语句,返回影响的行数 |
|
||||
| ExecuteUpdated | List\<T1\> | | 执行SQL语句,返回更新后的记录 |
|
@ -1,2 +0,0 @@
|
||||
/** layui-v2.2.3 MIT License By http://www.layui.com */
|
||||
html #layuicss-skincodecss{display:none;position:absolute;width:1989px}.layui-code-h3,.layui-code-view{position:relative;font-size:12px}.layui-code-view{display:block;margin:10px 0;padding:0;border:1px solid #e2e2e2;border-left-width:6px;background-color:#F2F2F2;color:#333;font-family:Courier New}.layui-code-h3{padding:0 10px;height:32px;line-height:32px;border-bottom:1px solid #e2e2e2}.layui-code-h3 a{position:absolute;right:10px;top:0;color:#999}.layui-code-view .layui-code-ol{position:relative;overflow:auto}.layui-code-view .layui-code-ol li{position:relative;margin-left:45px;line-height:20px;padding:0 5px;border-left:1px solid #e2e2e2;list-style-type:decimal-leading-zero;*list-style-type:decimal;background-color:#fff}.layui-code-view pre{margin:0}.layui-code-notepad{border:1px solid #0C0C0C;border-left-color:#3F3F3F;background-color:#0C0C0C;color:#C2BE9E}.layui-code-notepad .layui-code-h3{border-bottom:none}.layui-code-notepad .layui-code-ol li{background-color:#3F3F3F;border-left:none}
|
Before Width: | Height: | Size: 5.8 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 701 B |
Before Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 222 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 3.3 KiB |
Before Width: | Height: | Size: 7.3 KiB |
Before Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 6.6 KiB |
Before Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 5.0 KiB |
Before Width: | Height: | Size: 5.1 KiB |
Before Width: | Height: | Size: 9.6 KiB |
Before Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 7.9 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 3.9 KiB |
Before Width: | Height: | Size: 2.5 KiB |
Before Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 3.5 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 5.6 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 5.2 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 3.3 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 777 B |
Before Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 3.5 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 3.3 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 5.2 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 4.1 KiB |
@ -1,2 +0,0 @@
|
||||
/** layui-v2.2.3 MIT License By http://www.layui.com */
|
||||
;layui.define("jquery",function(e){"use strict";var i=layui.$,n=(layui.hint(),layui.device(),{config:{},set:function(e){var n=this;return n.config=i.extend({},n.config,e),n},on:function(e,i){return layui.onevent.call(this,t,e,i)}}),t="carousel",a="layui-this",l=">*[carousel-item]>*",o="layui-carousel-left",r="layui-carousel-right",d="layui-carousel-prev",s="layui-carousel-next",u="layui-carousel-arrow",c="layui-carousel-ind",m=function(e){var t=this;t.config=i.extend({},t.config,n.config,e),t.render()};m.prototype.config={width:"600px",height:"280px",full:!1,arrow:"hover",indicator:"inside",autoplay:!0,interval:3e3,anim:"",trigger:"click",index:0},m.prototype.render=function(){var e=this,n=e.config;n.elem=i(n.elem),n.elem[0]&&(e.elemItem=n.elem.find(l),n.index<0&&(n.index=0),n.index>=e.elemItem.length&&(n.index=e.elemItem.length-1),n.interval<800&&(n.interval=800),n.full?n.elem.css({position:"fixed",width:"100%",height:"100%",zIndex:9999}):n.elem.css({width:n.width,height:n.height}),n.elem.attr("lay-anim",n.anim),e.elemItem.eq(n.index).addClass(a),e.elemItem.length<=1||(e.indicator(),e.arrow(),e.autoplay(),e.events()))},m.prototype.reload=function(e){var n=this;clearInterval(n.timer),n.config=i.extend({},n.config,e),n.render()},m.prototype.prevIndex=function(){var e=this,i=e.config,n=i.index-1;return n<0&&(n=e.elemItem.length-1),n},m.prototype.nextIndex=function(){var e=this,i=e.config,n=i.index+1;return n>=e.elemItem.length&&(n=0),n},m.prototype.addIndex=function(e){var i=this,n=i.config;e=e||1,n.index=n.index+e,n.index>=i.elemItem.length&&(n.index=0)},m.prototype.subIndex=function(e){var i=this,n=i.config;e=e||1,n.index=n.index-e,n.index<0&&(n.index=i.elemItem.length-1)},m.prototype.autoplay=function(){var e=this,i=e.config;i.autoplay&&(e.timer=setInterval(function(){e.slide()},i.interval))},m.prototype.arrow=function(){var e=this,n=e.config,t=i(['<button class="layui-icon '+u+'" lay-type="sub">'+("updown"===n.anim?"":"")+"</button>",'<button class="layui-icon '+u+'" lay-type="add">'+("updown"===n.anim?"":"")+"</button>"].join(""));n.elem.attr("lay-arrow",n.arrow),n.elem.find("."+u)[0]&&n.elem.find("."+u).remove(),n.elem.append(t),t.on("click",function(){var n=i(this),t=n.attr("lay-type");e.slide(t)})},m.prototype.indicator=function(){var e=this,n=e.config,t=e.elemInd=i(['<div class="'+c+'"><ul>',function(){var i=[];return layui.each(e.elemItem,function(e){i.push("<li"+(n.index===e?' class="layui-this"':"")+"></li>")}),i.join("")}(),"</ul></div>"].join(""));n.elem.attr("lay-indicator",n.indicator),n.elem.find("."+c)[0]&&n.elem.find("."+c).remove(),n.elem.append(t),"updown"===n.anim&&t.css("margin-top",-(t.height()/2)),t.find("li").on("hover"===n.trigger?"mouseover":n.trigger,function(){var t=i(this),a=t.index();a>n.index?e.slide("add",a-n.index):a<n.index&&e.slide("sub",n.index-a)})},m.prototype.slide=function(e,i){var n=this,l=n.elemItem,u=n.config,c=u.index,m=u.elem.attr("lay-filter");n.haveSlide||("sub"===e?(n.subIndex(i),l.eq(u.index).addClass(d),setTimeout(function(){l.eq(c).addClass(r),l.eq(u.index).addClass(r)},50)):(n.addIndex(i),l.eq(u.index).addClass(s),setTimeout(function(){l.eq(c).addClass(o),l.eq(u.index).addClass(o)},50)),setTimeout(function(){l.removeClass(a+" "+d+" "+s+" "+o+" "+r),l.eq(u.index).addClass(a),n.haveSlide=!1},300),n.elemInd.find("li").eq(u.index).addClass(a).siblings().removeClass(a),n.haveSlide=!0,layui.event.call(this,t,"change("+m+")",{index:u.index,prevIndex:c,item:l.eq(u.index)}))},m.prototype.events=function(){var e=this,i=e.config;i.elem.data("haveEvents")||(i.elem.on("mouseenter",function(){clearInterval(e.timer)}).on("mouseleave",function(){e.autoplay()}),i.elem.data("haveEvents",!0))},n.render=function(e){var i=new m(e);return i},e(t,n)});
|
@ -1,2 +0,0 @@
|
||||
/** layui-v2.2.3 MIT License By http://www.layui.com */
|
||||
;layui.define("jquery",function(e){"use strict";var a=layui.$,l="http://www.layui.com/doc/modules/code.html";e("code",function(e){var t=[];e=e||{},e.elem=a(e.elem||".layui-code"),e.about=!("about"in e)||e.about,e.elem.each(function(){t.push(this)}),layui.each(t.reverse(),function(t,i){var c=a(i),o=c.html();(c.attr("lay-encode")||e.encode)&&(o=o.replace(/&(?!#?[a-zA-Z0-9]+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/'/g,"'").replace(/"/g,""")),c.html('<ol class="layui-code-ol"><li>'+o.replace(/[\r\t\n]+/g,"</li><li>")+"</li></ol>"),c.find(">.layui-code-h3")[0]||c.prepend('<h3 class="layui-code-h3">'+(c.attr("lay-title")||e.title||"code")+(e.about?'<a href="'+l+'" target="_blank">layui.code</a>':"")+"</h3>");var d=c.find(">.layui-code-ol");c.addClass("layui-box layui-code-view"),(c.attr("lay-skin")||e.skin)&&c.addClass("layui-code-"+(c.attr("lay-skin")||e.skin)),(d.find("li").length/100|0)>0&&d.css("margin-left",(d.find("li").length/100|0)+"px"),(c.attr("lay-height")||e.height)&&d.css("max-height",c.attr("lay-height")||e.height)})})}).addcss("modules/code.css","skincodecss");
|
@ -1,2 +0,0 @@
|
||||
/** layui-v2.2.3 MIT License By http://www.layui.com */
|
||||
;layui.define("jquery",function(e){"use strict";var l=layui.$,o=function(e){},t='<i class="layui-anim layui-anim-rotate layui-anim-loop layui-icon "></i>';o.prototype.load=function(e){var o,i,n,r,a=this,c=0;e=e||{};var f=l(e.elem);if(f[0]){var m=l(e.scrollElem||document),u=e.mb||50,s=!("isAuto"in e)||e.isAuto,v=e.end||"没有更多了",y=e.scrollElem&&e.scrollElem!==document,d="<cite>加载更多</cite>",h=l('<div class="layui-flow-more"><a href="javascript:;">'+d+"</a></div>");f.find(".layui-flow-more")[0]||f.append(h);var p=function(e,t){e=l(e),h.before(e),t=0==t||null,t?h.html(v):h.find("a").html(d),i=t,o=null,n&&n()},g=function(){o=!0,h.find("a").html(t),"function"==typeof e.done&&e.done(++c,p)};if(g(),h.find("a").on("click",function(){l(this);i||o||g()}),e.isLazyimg)var n=a.lazyimg({elem:e.elem+" img",scrollElem:e.scrollElem});return s?(m.on("scroll",function(){var e=l(this),t=e.scrollTop();r&&clearTimeout(r),i||(r=setTimeout(function(){var i=y?e.height():l(window).height(),n=y?e.prop("scrollHeight"):document.documentElement.scrollHeight;n-t-i<=u&&(o||g())},100))}),a):a}},o.prototype.lazyimg=function(e){var o,t=this,i=0;e=e||{};var n=l(e.scrollElem||document),r=e.elem||"img",a=e.scrollElem&&e.scrollElem!==document,c=function(e,l){var o=n.scrollTop(),r=o+l,c=a?function(){return e.offset().top-n.offset().top+o}():e.offset().top;if(c>=o&&c<=r&&!e.attr("src")){var m=e.attr("lay-src");layui.img(m,function(){var l=t.lazyimg.elem.eq(i);e.attr("src",m).removeAttr("lay-src"),l[0]&&f(l),i++})}},f=function(e,o){var f=a?(o||n).height():l(window).height(),m=n.scrollTop(),u=m+f;if(t.lazyimg.elem=l(r),e)c(e,f);else for(var s=0;s<t.lazyimg.elem.length;s++){var v=t.lazyimg.elem.eq(s),y=a?function(){return v.offset().top-n.offset().top+m}():v.offset().top;if(c(v,f),i=s,y>u)break}};if(f(),!o){var m;n.on("scroll",function(){var e=l(this);m&&clearTimeout(m),m=setTimeout(function(){f(null,e)},50)}),o=!0}return f},e("flow",new o)});
|
@ -1,483 +0,0 @@
|
||||
/**
|
||||
|
||||
layui官网
|
||||
|
||||
*/
|
||||
|
||||
layui.define(['code', 'element', 'table', 'util'], function (exports) {
|
||||
var $ = layui.jquery
|
||||
, element = layui.element
|
||||
, layer = layui.layer
|
||||
, form = layui.form
|
||||
, util = layui.util
|
||||
, device = layui.device()
|
||||
|
||||
, $win = $(window), $body = $('body');
|
||||
|
||||
|
||||
//阻止IE7以下访问
|
||||
if (device.ie && device.ie < 8) {
|
||||
layer.alert('Layui最低支持ie8,您当前使用的是古老的 IE' + device.ie + ',你丫的肯定不是程序猿!');
|
||||
}
|
||||
|
||||
var home = $('#LAY_home');
|
||||
|
||||
|
||||
layer.ready(function () {
|
||||
var local = layui.data('layui');
|
||||
|
||||
//升级提示
|
||||
if (local.version && local.version !== layui.v) {
|
||||
layer.open({
|
||||
type: 1
|
||||
, title: '更新提示' //不显示标题栏
|
||||
, closeBtn: false
|
||||
, area: '300px;'
|
||||
, shade: false
|
||||
, offset: 'b'
|
||||
, id: 'LAY_updateNotice' //设定一个id,防止重复弹出
|
||||
, btn: ['更新日志', '朕不想升']
|
||||
, btnAlign: 'c'
|
||||
, moveType: 1 //拖拽模式,0或者1
|
||||
, content: ['<div class="layui-text">'
|
||||
, 'layui 已更新到:<strong style="padding-right: 10px; color: #fff;">v' + layui.v + '</strong> <br>请注意升级!'
|
||||
, '</div>'].join('')
|
||||
, skin: 'layui-layer-notice'
|
||||
, yes: function (index) {
|
||||
layer.close(index);
|
||||
setTimeout(function () {
|
||||
location.href = '/doc/base/changelog.html';
|
||||
}, 500);
|
||||
}
|
||||
, end: function () {
|
||||
layui.data('layui', {
|
||||
key: 'version'
|
||||
, value: layui.v
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
layui.data('layui', {
|
||||
key: 'version'
|
||||
, value: layui.v
|
||||
});
|
||||
|
||||
|
||||
//公告
|
||||
; !function () {
|
||||
return layui.data('layui', {
|
||||
key: 'notice_20180530'
|
||||
, remove: true
|
||||
});
|
||||
|
||||
if (local.notice_20180530 && new Date().getTime() - local.notice_20180530 < 1000 * 60 * 60 * 24 * 5) {
|
||||
return;
|
||||
};
|
||||
|
||||
layer.open({
|
||||
type: 1
|
||||
, title: 'layui 官方通用后台管理模板'
|
||||
, closeBtn: false
|
||||
, area: ['300px', '280px']
|
||||
, shade: false
|
||||
//,offset: 'c'
|
||||
, id: 'LAY_Notice' //设定一个id,防止重复弹出
|
||||
, btn: ['前往围观', '朕不想看']
|
||||
, btnAlign: 'b'
|
||||
, moveType: 1 //拖拽模式,0或者1
|
||||
, resize: false
|
||||
, content: ['<div style="padding: 15px; text-align: center; background-color: #e2e2e2;">'
|
||||
, '<a href="/admin/std/dist/views/" target="_blank"><img src="//cdn.layui.com/upload/2018_5/168_1527691799254_76462.jpg" alt="layuiAdmin" style="width: 100%; height:149.78px;"></a>'
|
||||
, '</div>'].join('')
|
||||
, success: function (layero, index) {
|
||||
var btn = layero.find('.layui-layer-btn');
|
||||
btn.find('.layui-layer-btn0').attr({
|
||||
href: '/admin/std/dist/views/'
|
||||
, target: '_blank'
|
||||
});
|
||||
|
||||
layero.find('a').on('click', function () {
|
||||
layer.close(index);
|
||||
});
|
||||
}
|
||||
, end: function () {
|
||||
layui.data('layui', {
|
||||
key: 'notice_20180530'
|
||||
, value: new Date().getTime()
|
||||
});
|
||||
}
|
||||
});
|
||||
}();
|
||||
|
||||
});
|
||||
|
||||
; !function () {
|
||||
var elemComponentSelect = $(['<select lay-search lay-filter="component">'
|
||||
, '<option value="">搜索组件或模块</option>'
|
||||
, '<option value="element/layout.html">grid 栅格布局</option>'
|
||||
, '<option value="element/layout.html#admin">admin 后台布局</option>'
|
||||
, '<option value="element/color.html">color 颜色</option>'
|
||||
, '<option value="element/icon.html">iconfont 字体图标</option>'
|
||||
, '<option value="element/anim.html">animation 动画</option>'
|
||||
, '<option value="element/button.html">button 按钮</option>'
|
||||
, '<option value="element/form.html">form 表单组</option>'
|
||||
, '<option value="element/form.html#input">input 输入框</option>'
|
||||
, '<option value="element/form.html#select">select 下拉选择框</option>'
|
||||
, '<option value="element/form.html#checkbox">checkbox 复选框</option>'
|
||||
, '<option value="element/form.html#switch">switch 开关</option>'
|
||||
, '<option value="element/form.html#radio">radio 单选框</option>'
|
||||
, '<option value="element/form.html#textarea">textarea 文本域</option>'
|
||||
, '<option value="element/nav.html">nav 导航菜单</option>'
|
||||
, '<option value="element/nav.html#breadcrumb">breadcrumb 面包屑</option>'
|
||||
, '<option value="element/tab.html">tabs 选项卡</option>'
|
||||
, '<option value="element/progress.html">progress 进度条</option>'
|
||||
, '<option value="element/collapse.html">collapse 折叠面板/手风琴</option>'
|
||||
, '<option value="element/table.html">table 表格元素</option>'
|
||||
, '<option value="element/badge.html">badge 徽章</option>'
|
||||
, '<option value="element/timeline.html">timeline 时间线</option>'
|
||||
, '<option value="element/auxiliar.html#blockquote">blockquote 引用块</option>'
|
||||
, '<option value="element/auxiliar.html#fieldset">fieldset 字段集</option>'
|
||||
, '<option value="element/auxiliar.html#hr">hr 分割线</option>'
|
||||
|
||||
, '<option value="modules/layer.html">layer 弹出层/弹窗综合</option>'
|
||||
, '<option value="modules/laydate.html">laydate 日期时间选择器</option>'
|
||||
, '<option value="modules/layim.html">layim 即时通讯/聊天</option>'
|
||||
, '<option value="modules/laypage.html">laypage 分页</option>'
|
||||
, '<option value="modules/laytpl.html">laytpl 模板引擎</option>'
|
||||
, '<option value="modules/form.html">form 表单模块</option>'
|
||||
, '<option value="modules/table.html">table 数据表格</option>'
|
||||
, '<option value="modules/upload.html">upload 文件/图片上传</option>'
|
||||
, '<option value="modules/element.html">element 常用元素操作</option>'
|
||||
, '<option value="modules/rate.html">rate 评分</option>'
|
||||
, '<option value="modules/colorpicker.html">colorpicker 颜色选择器</option>'
|
||||
, '<option value="modules/slider.html">slider 滑块</option>'
|
||||
, '<option value="modules/carousel.html">carousel 轮播/跑马灯</option>'
|
||||
, '<option value="modules/layedit.html">layedit 富文本编辑器</option>'
|
||||
, '<option value="modules/tree.html">tree 树形菜单</option>'
|
||||
, '<option value="modules/flow.html">flow 信息流/图片懒加载</option>'
|
||||
, '<option value="modules/util.html">util 工具集</option>'
|
||||
, '<option value="modules/code.html">code 代码修饰</option>'
|
||||
, '</select>'].join(''));
|
||||
|
||||
$('.component').append(elemComponentSelect);
|
||||
form.render('select', 'LAY-site-header-component');
|
||||
|
||||
//搜索组件
|
||||
form.on('select(component)', function (data) {
|
||||
var value = data.value;
|
||||
location.href = '/doc/' + value;
|
||||
});
|
||||
}();
|
||||
|
||||
|
||||
//点击事件
|
||||
var events = {
|
||||
//联系方式
|
||||
contactInfo: function () {
|
||||
layer.alert('<div class="layui-text">如有合作意向,可联系:<br>邮箱:xianxin@layui-inc.com</div>', {
|
||||
title: '联系'
|
||||
, btn: false
|
||||
, shadeClose: true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$body.on('click', '*[site-event]', function () {
|
||||
var othis = $(this)
|
||||
, attrEvent = othis.attr('site-event');
|
||||
events[attrEvent] && events[attrEvent].call(this, othis);
|
||||
});
|
||||
|
||||
//切换版本
|
||||
form.on('select(tabVersion)', function (data) {
|
||||
var value = data.value;
|
||||
location.href = value === 'new' ? '/' : ('/' + value + '/doc/');
|
||||
});
|
||||
|
||||
|
||||
//首页banner
|
||||
setTimeout(function () {
|
||||
$('.site-zfj').addClass('site-zfj-anim');
|
||||
setTimeout(function () {
|
||||
$('.site-desc').addClass('site-desc-anim')
|
||||
}, 5000)
|
||||
}, 100);
|
||||
|
||||
|
||||
//数字前置补零
|
||||
var digit = function (num, length, end) {
|
||||
var str = '';
|
||||
num = String(num);
|
||||
length = length || 2;
|
||||
for (var i = num.length; i < length; i++) {
|
||||
str += '0';
|
||||
}
|
||||
return num < Math.pow(10, length) ? str + (num | 0) : num;
|
||||
};
|
||||
|
||||
|
||||
//下载倒计时
|
||||
var setCountdown = $('#setCountdown');
|
||||
if ($('#setCountdown')[0]) {
|
||||
$.get('/api/getTime', function (res) {
|
||||
util.countdown(new Date(2017, 7, 21, 8, 30, 0), new Date(res.time), function (date, serverTime, timer) {
|
||||
var str = digit(date[1]) + ':' + digit(date[2]) + ':' + digit(date[3]);
|
||||
setCountdown.children('span').html(str);
|
||||
});
|
||||
}, 'jsonp');
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (var i = 0; i < $('.adsbygoogle').length; i++) {
|
||||
(adsbygoogle = window.adsbygoogle || []).push({});
|
||||
}
|
||||
|
||||
|
||||
//展示当前版本
|
||||
$('.site-showv').html(layui.v);
|
||||
|
||||
//获取下载数
|
||||
$.get('//fly.layui.com/api/handle?id=10&type=find', function (res) {
|
||||
$('.site-showdowns').html(res.number);
|
||||
}, 'jsonp');
|
||||
|
||||
//记录下载
|
||||
$('.site-down').on('click', function () {
|
||||
$.get('//fly.layui.com/api/handle?id=10', function () { }, 'jsonp');
|
||||
});
|
||||
|
||||
//获取Github数据
|
||||
var getStars = $('#getStars');
|
||||
if (getStars[0]) {
|
||||
$.get('https://api.github.com/repos/sentsin/layui', function (res) {
|
||||
getStars.html(res.stargazers_count);
|
||||
}, 'json');
|
||||
}
|
||||
|
||||
//固定Bar
|
||||
if (global.pageType !== 'demo') {
|
||||
util.fixbar({
|
||||
bar1: true
|
||||
, click: function (type) {
|
||||
if (type === 'bar1') {
|
||||
location.href = '//fly.layui.com/';
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//窗口scroll
|
||||
; !function () {
|
||||
var main = $('.site-tree').parent(), scroll = function () {
|
||||
var stop = $(window).scrollTop();
|
||||
|
||||
if ($(window).width() <= 750) return;
|
||||
var bottom = $('.footer').offset().top - $(window).height();
|
||||
if (stop > 211 && stop < bottom) {
|
||||
if (!main.hasClass('site-fix')) {
|
||||
main.addClass('site-fix');
|
||||
}
|
||||
if (main.hasClass('site-fix-footer')) {
|
||||
main.removeClass('site-fix-footer');
|
||||
}
|
||||
} else if (stop >= bottom) {
|
||||
if (!main.hasClass('site-fix-footer')) {
|
||||
main.addClass('site-fix site-fix-footer');
|
||||
}
|
||||
} else {
|
||||
if (main.hasClass('site-fix')) {
|
||||
main.removeClass('site-fix').removeClass('site-fix-footer');
|
||||
}
|
||||
}
|
||||
stop = null;
|
||||
};
|
||||
scroll();
|
||||
$(window).on('scroll', scroll);
|
||||
}();
|
||||
|
||||
//示例页面滚动
|
||||
$('.site-demo-body').on('scroll', function () {
|
||||
var elemDate = $('.layui-laydate,.layui-colorpicker-main')
|
||||
, elemTips = $('.layui-table-tips');
|
||||
if (elemDate[0]) {
|
||||
elemDate.each(function () {
|
||||
var othis = $(this);
|
||||
if (!othis.hasClass('layui-laydate-static')) {
|
||||
othis.remove();
|
||||
}
|
||||
});
|
||||
$('input').blur();
|
||||
}
|
||||
if (elemTips[0]) elemTips.remove();
|
||||
|
||||
if ($('.layui-layer')[0]) {
|
||||
layer.closeAll('tips');
|
||||
}
|
||||
});
|
||||
|
||||
//代码修饰
|
||||
layui.code({
|
||||
elem: 'pre'
|
||||
});
|
||||
|
||||
//目录
|
||||
var siteDir = $('.site-dir');
|
||||
if (siteDir[0] && $(window).width() > 750) {
|
||||
layer.ready(function () {
|
||||
layer.open({
|
||||
type: 1
|
||||
, content: siteDir
|
||||
, skin: 'layui-layer-dir'
|
||||
, area: 'auto'
|
||||
, maxHeight: $(window).height() - 300
|
||||
, title: '目录'
|
||||
//,closeBtn: false
|
||||
, offset: 'r'
|
||||
, shade: false
|
||||
, success: function (layero, index) {
|
||||
layer.style(index, {
|
||||
marginLeft: -15
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
siteDir.find('li').on('click', function () {
|
||||
var othis = $(this);
|
||||
othis.find('a').addClass('layui-this');
|
||||
othis.siblings().find('a').removeClass('layui-this');
|
||||
});
|
||||
}
|
||||
|
||||
//在textarea焦点处插入字符
|
||||
var focusInsert = function (str) {
|
||||
var start = this.selectionStart
|
||||
, end = this.selectionEnd
|
||||
, offset = start + str.length
|
||||
|
||||
this.value = this.value.substring(0, start) + str + this.value.substring(end);
|
||||
this.setSelectionRange(offset, offset);
|
||||
};
|
||||
|
||||
//演示页面
|
||||
$('body').on('keydown', '#LAY_editor, .site-demo-text', function (e) {
|
||||
var key = e.keyCode;
|
||||
if (key === 9 && window.getSelection) {
|
||||
e.preventDefault();
|
||||
focusInsert.call(this, ' ');
|
||||
}
|
||||
});
|
||||
|
||||
var editor = $('#LAY_editor')
|
||||
, iframeElem = $('#LAY_demo')
|
||||
, demoForm = $('#LAY_demoForm')[0]
|
||||
, demoCodes = $('#LAY_demoCodes')[0]
|
||||
, runCodes = function () {
|
||||
if (!iframeElem[0]) return;
|
||||
var html = editor.val();
|
||||
|
||||
html = html.replace(/=/gi, "layequalsign");
|
||||
html = html.replace(/script/gi, "layscrlayipttag");
|
||||
demoCodes.value = html.length > 100 * 1000 ? '<h1>卧槽,你的代码过长</h1>' : html;
|
||||
|
||||
demoForm.action = '/api/runHtml/';
|
||||
demoForm.submit();
|
||||
|
||||
};
|
||||
$('#LAY_demo_run').on('click', runCodes), runCodes();
|
||||
|
||||
//让导航在最佳位置
|
||||
var setScrollTop = function (thisItem, elemScroll) {
|
||||
if (thisItem[0]) {
|
||||
var itemTop = thisItem.offset().top
|
||||
, winHeight = $(window).height();
|
||||
if (itemTop > winHeight - 120) {
|
||||
elemScroll.animate({ 'scrollTop': itemTop / 2 }, 200)
|
||||
}
|
||||
}
|
||||
}
|
||||
setScrollTop($('.site-demo-nav').find('dd.layui-this'), $('.layui-side-scroll').eq(0));
|
||||
setScrollTop($('.site-demo-table-nav').find('li.layui-this'), $('.layui-side-scroll').eq(1));
|
||||
|
||||
|
||||
|
||||
//查看代码
|
||||
$(function () {
|
||||
var DemoCode = $('#LAY_democode');
|
||||
DemoCode.val([
|
||||
DemoCode.val()
|
||||
, '<body>'
|
||||
, global.preview
|
||||
, '\n<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>'
|
||||
, '\n<!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->'
|
||||
, $('#LAY_democodejs').html()
|
||||
, '\n</body>\n</html>'
|
||||
].join(''));
|
||||
});
|
||||
|
||||
//点击查看代码选项
|
||||
element.on('tab(demoTitle)', function (obj) {
|
||||
if (obj.index === 1) {
|
||||
if (device.ie && device.ie < 9) {
|
||||
layer.alert('强烈不推荐你通过ie8/9 查看代码!因为,所有的标签都会被格式成大写,且没有换行符,影响阅读');
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
//手机设备的简单适配
|
||||
var treeMobile = $('.site-tree-mobile')
|
||||
, shadeMobile = $('.site-mobile-shade')
|
||||
|
||||
treeMobile.on('click', function () {
|
||||
$('body').addClass('site-mobile');
|
||||
});
|
||||
|
||||
shadeMobile.on('click', function () {
|
||||
$('body').removeClass('site-mobile');
|
||||
});
|
||||
|
||||
|
||||
|
||||
//愚人节
|
||||
; !function () {
|
||||
if (home.data('date') === '4-1') {
|
||||
|
||||
if (local['20180401']) return;
|
||||
|
||||
home.addClass('site-out-up');
|
||||
setTimeout(function () {
|
||||
layer.photos({
|
||||
photos: {
|
||||
"data": [{
|
||||
"src": "//cdn.layui.com/upload/2018_4/168_1522515820513_397.png",
|
||||
}]
|
||||
}
|
||||
, anim: 2
|
||||
, shade: 1
|
||||
, move: false
|
||||
, end: function () {
|
||||
layer.msg('愚公,快醒醒!', {
|
||||
shade: 1
|
||||
}, function () {
|
||||
layui.data('layui', {
|
||||
key: '20180401'
|
||||
, value: true
|
||||
});
|
||||
});
|
||||
}
|
||||
, success: function (layero, index) {
|
||||
home.removeClass('site-out-up');
|
||||
|
||||
layero.find('#layui-layer-photos').on('click', function () {
|
||||
layer.close(layero.attr('times'));
|
||||
}).find('.layui-layer-imgsee').remove();
|
||||
}
|
||||
});
|
||||
}, 1000 * 3);
|
||||
}
|
||||
}();
|
||||
|
||||
|
||||
exports('global', {});
|
||||
});
|