Skip to content

Commit fa7f572

Browse files
committed
修复bug
1 parent d0b587d commit fa7f572

File tree

7 files changed

+159
-49
lines changed

7 files changed

+159
-49
lines changed

BLL.cst

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ namespace <%= BLLNamespace %>
3030
/// 根据传入Model,并返回是否插入成功。
3131
/// </summary>
3232
[DataObjectMethod(DataObjectMethodType.Insert)]
33-
public bool <%= GetModelClassName() %>Add(<%= GetModelClassName() %> <%= GetModelParamName() %>)
33+
public static bool <%= GetModelClassName() %>Insert(<%= GetModelClassName() %> <%= GetModelParamName() %>)
3434
{
35-
return new <%= GetDALClassName() %>().Add(<%= GetModelParamName() %>);
35+
return new <%= GetDALClassName() %>().Insert(<%= GetModelParamName() %>);
3636
}
3737
#endregion
3838

@@ -52,18 +52,28 @@ namespace <%= BLLNamespace %>
5252
/// <summary>
5353
/// 根据字段名获取数据记录
5454
/// </summary>
55-
public IEnumerable<<%= GetModelClassName() %>> GetBycolumnName(string columnName,string columnContent)
55+
public static IEnumerable<<%= GetModelClassName() %>> GetBycolumnName(string columnName,string columnContent)
5656
{
5757
return new <%= GetDALClassName() %>().GetBycolumnName(columnName,columnContent);
5858
}
5959
#endregion
6060

61+
#region 根据字段名获取数据记录
62+
/// <summary>
63+
/// 根据字段名获取数据记录
64+
/// </summary>
65+
public static IEnumerable<<%= GetModelClassName() %>> GetBycolumnNames(string[] columnNames,string[] columnContents)
66+
{
67+
return new <%= GetDALClassName() %>().GetBycolumnNames(columnNames,columnContents);
68+
}
69+
#endregion
70+
6171
#region 根据传入Model更新数据并返回更新后的Model
6272
/// <summary>
6373
/// 根据传入Model更新数据并返回更新后的Model
6474
/// </summary>
6575
[DataObjectMethod(DataObjectMethodType.Update)]
66-
public int Update(<%= GetModelClassName() %> <%= GetModelParamName() %>)
76+
public static int Update(<%= GetModelClassName() %> <%= GetModelParamName() %>)
6777
{
6878
return new <%= GetDALClassName() %>().Update(<%= GetModelParamName() %>);
6979
}
@@ -74,7 +84,7 @@ namespace <%= BLLNamespace %>
7484
/// 传入Id,获得Model实体
7585
/// </summary>
7686
[DataObjectMethod(DataObjectMethodType.Select)]
77-
public <%= GetModelClassName() %> GetBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
87+
public static <%= GetModelClassName() %> GetBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
7888
{
7989
return new <%= GetDALClassName() %>().GetBy<%= GetPKPropertyName() %>(<%= GetPKParamName() %>);
8090
}
@@ -95,7 +105,7 @@ namespace <%= BLLNamespace %>
95105
/// 获得分页记录集IEnumerable<>
96106
///</summary>
97107
[DataObjectMethod(DataObjectMethodType.Select)]
98-
public IEnumerable<<%= GetModelClassName() %>> GetPagedData(int minrownum,int maxrownum)
108+
public static IEnumerable<<%= GetModelClassName() %>> GetPagedData(int minrownum,int maxrownum)
99109
{
100110
return new <%= GetDALClassName() %>().GetPagedData(minrownum,maxrownum);
101111
}
@@ -106,7 +116,7 @@ namespace <%= BLLNamespace %>
106116
/// 获得总记录集IEnumerable<>
107117
///</summary>
108118
[DataObjectMethod(DataObjectMethodType.Select)]
109-
public IEnumerable<<%= GetModelClassName() %>> GetAll()
119+
public static IEnumerable<<%= GetModelClassName() %>> GetAll()
110120
{
111121
return new <%= GetDALClassName() %>().GetAll();
112122
}

CreatSingleTable.cst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ MergeProperties="False" ExcludeProperties="" %>
4141
Models model = this.Create<Models>();
4242
model.ModelsNamespace = this.RootNamespace+".Model";
4343
model.TargetTable = this.SourceTable;
44-
model.RenderToFile(this.OutputDirectory+"Model/"+model.GetFileName(),true);
44+
model.RenderToFile(this.OutputDirectory+"DataModels/"+model.GetFileName(),true);
4545

4646

4747
DAL dal = this.Create<DAL>();

DAL.cst

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<%@ Import Namespace="SchemaExplorer" %>
99
<%@ Import Namespace="System.Data" %>
1010
<%@ Import Namespace="System.Text.RegularExpressions" %>
11+
12+
1113
<% PrintHeader(); %>
1214
using System;
1315
using System.Collections.Generic;
@@ -25,7 +27,7 @@ namespace <%= DALNamespace %>
2527
/// <summary>
2628
/// 插入数据
2729
/// </summary>
28-
public bool Insert (<%= GetModelClassName() %> <%= GetModelParamName() %>)
30+
public bool Insert (<%= GetModelClassName() %> <%= GetModelParamName() %>)
2931
{
3032
<%if(IsIdentityPK())
3133
{%>
@@ -86,9 +88,9 @@ namespace <%= DALNamespace %>
8688
/// <summary>
8789
/// 根据Id删除数据记录
8890
/// </summary>
89-
public int DeleteBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetCamelPKName() %>)
91+
public int DeleteBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetCamelPKName() %>)
9092
{
91-
string sql = "DELETE from <%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
93+
string sql = "DELETE from <%= TargetTable.Database %>.<%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
9294

9395
MySqlParameter[] para = new MySqlParameter[]
9496
{
@@ -104,11 +106,9 @@ namespace <%= DALNamespace %>
104106
/// <summary>
105107
/// 根据传入Model的ID更新数据并返回更新后的Model
106108
/// </summary>
107-
public int Update(<%= GetModelClassName() %> <%= GetModelParamName() %>)
109+
public int Update(<%= GetModelClassName() %> <%= GetModelParamName() %>)
108110
{
109-
string sql =
110-
"UPDATE <%= TargetTable.Name %> " +
111-
"SET " +
111+
string sql ="UPDATE <%= TargetTable.Database %>.<%= TargetTable.Name %> " +"SET " +
112112
" <%= TargetTable.NonPrimaryKeyColumns[0].Name %> = @<%= TargetTable.NonPrimaryKeyColumns[0].Name %>"
113113
<%
114114
for(int i=1; i<TargetTable.NonPrimaryKeyColumns.Count; i++)
@@ -142,11 +142,11 @@ namespace <%= DALNamespace %>
142142
#endregion
143143
#region select
144144
/// <summary>
145-
/// 传入Id,获得Model实体
145+
/// 传入主键,获得Model实体
146146
/// </summary>
147-
public <%= GetModelClassName() %> GetBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetCamelPKName() %>)
147+
public <%= GetModelClassName() %> GetBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetCamelPKName() %>)
148148
{
149-
string sql = "SELECT * FROM <%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
149+
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
150150
using(MySqlDataReader reader = MyDBHelper.ExecuteDataReader(sql, new MySqlParameter("@<%= GetPKPropertyName() %>", <%= GetCamelPKName() %>)))
151151
{
152152
if (reader.Read())
@@ -163,9 +163,36 @@ namespace <%= DALNamespace %>
163163
///<summary>
164164
///根据字段名获取数据记录IEnumerable<>
165165
///</summary>
166-
public IEnumerable<<%= GetModelClassName() %>> GetBycolumnName(string columnName,string columnContent)
166+
public IEnumerable<<%= GetModelClassName() %>> GetBycolumnName(string columnName,string columnContent)
167+
{
168+
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %> where "+columnName+"='"+ @columnContent + "'";
169+
using(MySqlDataReader reader = MyDBHelper.ExecuteDataReader(sql,new MySqlParameter(columnName, columnContent)))
170+
{
171+
return ToModels(reader);
172+
}
173+
}
174+
175+
///<summary>
176+
///根据字段名获取数据记录IEnumerable<>
177+
///</summary>
178+
public IEnumerable<<%= GetModelClassName() %>> GetBycolumnNames(string[] columnNames,string[] columnContents)
167179
{
168-
string sql = "SELECT * FROM <%= TargetTable.Name %> where "+columnName+"="+columnContent;
180+
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %> where ";
181+
182+
if (columnNames.Length!= columnContents.Length)
183+
{
184+
return null;
185+
}
186+
else
187+
{
188+
for (int i = 0; i < columnNames.Length; i++)
189+
{
190+
sql += columnNames[i] +"= '"+ columnContents[i] + "' and ";
191+
}
192+
193+
sql = sql.Substring(0, sql.Length - 5);
194+
}
195+
169196
using(MySqlDataReader reader = MyDBHelper.ExecuteDataReader(sql))
170197
{
171198
return ToModels(reader);
@@ -179,7 +206,7 @@ namespace <%= DALNamespace %>
179206
/// <summary>
180207
/// 把DataRow转换成Model
181208
/// </summary>
182-
public <%= GetModelClassName() %> ToModel(MySqlDataReader dr)
209+
public <%= GetModelClassName() %> ToModel(MySqlDataReader dr)
183210
{
184211
<%= GetModelClassName() %> <%= GetModelParamName() %> = new <%= GetModelClassName() %>();
185212

@@ -195,9 +222,9 @@ namespace <%= DALNamespace %>
195222
///<summary>
196223
/// 获得总记录数
197224
///</summary>
198-
public int GetTotalCount()
225+
public int GetTotalCount()
199226
{
200-
string sql = "SELECT count(*) FROM <%= TargetTable.Name %>";
227+
string sql = "SELECT count(*) FROM <%= TargetTable.Database %>.<%= TargetTable.Name %>";
201228
return (int)MyDBHelper.ExecuteScalar(sql);
202229
}
203230

@@ -208,7 +235,7 @@ namespace <%= DALNamespace %>
208235
///</summary>
209236
public IEnumerable<<%= GetModelClassName() %>> GetPagedData(int minrownum,int maxrownum)
210237
{
211-
string sql = "SELECT * from(SELECT *,(row_number() over(order by <%=this.GetPKName()%>))-1 rownum FROM <%= TargetTable.Name %>) t where rownum>=@minrownum and rownum<=@maxrownum";
238+
string sql = "SELECT * from(SELECT *,(row_number() over(order by <%=this.GetPKName()%>))-1 rownum FROM <%= TargetTable.Database %>.<%= TargetTable.Name %>) t where rownum>=@minrownum and rownum<=@maxrownum";
212239
using(MySqlDataReader reader = MyDBHelper.ExecuteDataReader(sql,
213240
new MySqlParameter("@minrownum",minrownum),
214241
new MySqlParameter("@maxrownum",maxrownum)))
@@ -223,9 +250,9 @@ namespace <%= DALNamespace %>
223250
///<summary>
224251
/// 获得总记录集IEnumerable<>
225252
///</summary>
226-
public IEnumerable<<%= GetModelClassName() %>> GetAll()
253+
public IEnumerable<<%= GetModelClassName() %>> GetAll()
227254
{
228-
string sql = "SELECT * FROM <%= TargetTable.Name %>";
255+
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %>";
229256
using(MySqlDataReader reader = MyDBHelper.ExecuteDataReader(sql))
230257
{
231258
return ToModels(reader);
@@ -238,7 +265,7 @@ namespace <%= DALNamespace %>
238265
///<summary>
239266
/// 把MySqlDataReader转换成IEnumerable<>
240267
///</summary>
241-
protected IEnumerable<<%= GetModelClassName() %>> ToModels(MySqlDataReader reader)
268+
protected IEnumerable<<%= GetModelClassName() %>> ToModels(MySqlDataReader reader)
242269
{
243270
var list = new List<<%= GetModelClassName() %>>();
244271
while(reader.Read())
@@ -253,7 +280,7 @@ namespace <%= DALNamespace %>
253280
///<summary>
254281
/// 判断数据是否为空
255282
///</summary>
256-
protected object ToDBValue(object value)
283+
protected object ToDBValue(object value)
257284
{
258285
if(value==null)
259286
{
@@ -270,7 +297,7 @@ namespace <%= DALNamespace %>
270297
///<summary>
271298
/// 判断数据表中是否包含该字段
272299
///</summary>
273-
protected object ToModelValue(MySqlDataReader reader,string columnName)
300+
protected object ToModelValue(MySqlDataReader reader,string columnName)
274301
{
275302
if(reader.IsDBNull(reader.GetOrdinal(columnName)))
276303
{
@@ -356,7 +383,7 @@ public string GetModelParamName() {
356383
/// <returns></returns>
357384
public string GetAutoIncInsertSQLLine() {
358385
string result;
359-
result = "INSERT INTO " + TargetTable.Name + " (";
386+
result = "INSERT INTO " +TargetTable.Database+"."+TargetTable.Name + " (";
360387
foreach (ColumnSchema column in TargetTable.NonPrimaryKeyColumns) {
361388
result += column.Name + ", ";
362389
}
@@ -374,7 +401,7 @@ public string GetAutoIncInsertSQLLine() {
374401

375402
public string GetCommonInsertSQLLine() {
376403
string result;
377-
result = "INSERT INTO " + TargetTable.Name + " (";
404+
result = "INSERT INTO "+TargetTable.Database+"."+TargetTable.Name + " (";
378405
foreach (ColumnSchema column in TargetTable.Columns) {
379406
result += column.Name + ", ";
380407
}

Main.csp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">
3+
<variables>
4+
<add key="ConnectionString1" value="server=127.0.0.1;User ID=root;Password=biolab-x-51818;database=biolab;port=53306;" />
5+
</variables>
6+
<propertySets>
7+
<propertySet name="Main.txt" output="Main.txt" template="E:\潘文博\我的代码\Git\MysqlCodesmithModel\Main.cst">
8+
<property name="RootNamespace">MySqlNamesapce</property>
9+
<property name="SourceTables">
10+
<connectionString>$(ConnectionString1)</connectionString>
11+
<providerType>SchemaExplorer.MySQLSchemaProvider,SchemaExplorer.MySQLSchemaProvider</providerType>
12+
<tableList>
13+
<table>
14+
<owner />
15+
<name>datyg</name>
16+
</table>
17+
<table>
18+
<owner />
19+
<name>dev</name>
20+
</table>
21+
<table>
22+
<owner />
23+
<name>fzmaintb</name>
24+
</table>
25+
<table>
26+
<owner />
27+
<name>fzresulttb</name>
28+
</table>
29+
<table>
30+
<owner />
31+
<name>shrecord</name>
32+
</table>
33+
<table>
34+
<owner />
35+
<name>shresult</name>
36+
</table>
37+
<table>
38+
<owner />
39+
<name>sta</name>
40+
</table>
41+
</tableList>
42+
</property>
43+
<property name="OutputDirectory">C:\Users\bran\Desktop\新建文件夹\</property>
44+
</propertySet>
45+
</propertySets>
46+
</codeSmith>

Main.cst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Mysql三层架构模板"
22
Output="None"%>
33

4+
45
<%@ Property Name="SourceTables" Type="SchemaExplorer.TableSchemaCollection" Default="" Optional="False" Category=""%>
56
<%@ Register Name="CreatSingleTable" Template="CreatSingleTable.cst" MergeProperties="False" ExcludeProperties="" %>
6-
<%@ Property Name="RootNamespace" Default="MysqlNamesapce" Type="System.String" Optional="False"%>
7+
<%@ Property Name="RootNamespace" Default="MySqlMVC" Type="System.String" Optional="False"%>
78
<%@ Assembly Name="SchemaExplorer" %>
89
<%@ Assembly Name="System.Data" %>
910
<%@ Import Namespace="SchemaExplorer" %>
@@ -34,6 +35,7 @@
3435
foreach(TableSchema ts in SourceTables)
3536
{
3637
CreatSingleTable s = new CreatSingleTable();
38+
3739
s.SourceTable = ts;
3840
s.RootNamespace = RootNamespace;
3941
s.OutputDirectory = OutputDirectory;

0 commit comments

Comments
 (0)