如果要连接mysql,需要安装驱动:

https://cdn.mysql.com//Downloads/Connector-Net/mysql-connector-net-8.0.12.msi

连接字符串

server=172.20.102.148;port=3306;database=metis_dev;uid=root;pwd=123456;SslMode = none;

这里需要注意ssl的设置,否则会报错

model-java

<%--
Name:
Author: maomao
Created:<%=Datetime.Now.ToShortDateString() %>
Description:
--%>
<%@ Template Language="C#" TargetLanguage="Java" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Collections"%>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="package" Type="String" Description="包名" Category="包名" %> package <%=package %>.domain; import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat; public class <%=convertClassName(SourceTable.Name) %>
{
<%foreach(ColumnSchema column in SourceTable.Columns){ %>
<%if(column.DataType==DbType.DateTime){ %>
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
<%} %>
private <%=convertoJavaType(column.SystemType.ToString()) %> <%=convertCamel(column.Name) %>;
<%} %>
} <script runat="template">
private string convertoJavaType(string name){
switch(name){
case "System.Int64":
return "Long";
case "System.Int32":
return "Integer";
case "System.UInt32":
return "Integer";
case "System.Int16":
return "Integer";
case "System.SByte":
return "Integer";
case "System.String":
return "String";
case "System.DateTime":
return "Date";
default:
return "unknown";
}
}
private string convertClassName(string name){
string[] strs=name.Split(new string[1]{"_"},StringSplitOptions.RemoveEmptyEntries);
string result="";
for(int i=0;i<strs.Length;i++){
string firstLetter=strs[i].Substring(0,1);
string left=strs[i].Substring(1);
result+=firstLetter.ToUpper()+left;
}
return result;
}
private string convertCamel(string name){
string[] strs=name.Split(new string[1]{"_"},StringSplitOptions.RemoveEmptyEntries);
string result=strs[0];
for(int i=1;i<strs.Length;i++){
string firstLetter=strs[i].Substring(0,1);
string left=strs[i].Substring(1);
result+=firstLetter.ToUpper()+left;
}
return result;
}
</script>

mapper-java

<%--
Name:
Author: maomao
Created:<%=Datetime.Now.ToShortDateString() %>
Description:
--%>
<%@ Template Language="C#" TargetLanguage="Java" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Collections"%>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="package" Type="String" Description="包名" Category="包名" %> package <%=package %>.interfaces; import <%=package %>.domain.<%=convertClassName(SourceTable.Name) %>;
import <%=package %>.interfaces.provider.<%=convertClassName(SourceTable.Name) %>Provider;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map; @Mapper
public interface <%=convertClassName(SourceTable.Name) %>Mapper
{
@Insert("insert into <%=SourceTable.Name %>(<%=getInsertColumns(SourceTable) %>) values (<%=getInsertColumnValues(SourceTable) %>) ")
@Options(useGeneratedKeys = true,keyProperty = "id")
int insert(<%=convertClassName(SourceTable.Name) %> entity);
@Update("update <%=SourceTable.Name %> set <%=getUpdateColumnValues(SourceTable) %> where id=#{id}")
void update(<%=convertClassName(SourceTable.Name) %> entity);
@Delete("delete from <%=SourceTable.Name %> where id = #{id}")
void deleteById(int id);
@SelectProvider(type = <%=convertClassName(SourceTable.Name) %>Provider.class,method = "selectWithParam")
@Results({
<%foreach(ColumnSchema column in SourceTable.Columns){ %>
@Result(column = "<%=column.Name %>",property = "<%=convertCamel(column.Name) %>"),
<%} %>
})
List<<%=convertClassName(SourceTable.Name) %>> selectByMap(Map<String,Object> map);
@Select("select * from <%=SourceTable.Name %> where id=#{id}")
<%=convertClassName(SourceTable.Name) %> selectById(int id);
} <script runat="template">
private string convertoJavaType(string name){
switch(name){
case "System.Int64":
return "Long";
case "System.Int32":
return "Integer";
case "System.UInt32":
return "Integer";
case "System.Int16":
return "Integer";
case "System.SByte":
return "Integer";
case "System.String":
return "String";
case "System.DateTime":
return "Date";
default:
return "unknown";
}
}
private string convertClassName(string name){
string[] strs=name.Split(new string[1]{"_"},StringSplitOptions.RemoveEmptyEntries);
string result="";
for(int i=0;i<strs.Length;i++){
string firstLetter=strs[i].Substring(0,1);
string left=strs[i].Substring(1);
result+=firstLetter.ToUpper()+left;
}
return result;
}
private string convertCamel(string name){
string[] strs=name.Split(new string[1]{"_"},StringSplitOptions.RemoveEmptyEntries);
string result=strs[0];
for(int i=1;i<strs.Length;i++){
string firstLetter=strs[i].Substring(0,1);
string left=strs[i].Substring(1);
result+=firstLetter.ToUpper()+left;
}
return result;
}
private string getInsertColumns(TableSchema table){
List<string> list=new List<string>();
foreach(var columnSchema in table.Columns){
if(!columnSchema.IsPrimaryKeyMember){
list.Add(columnSchema.Name);
}
}
return string.Join(",",list.ToArray());
}
private string getInsertColumnValues(TableSchema table){
List<string> list=new List<string>();
foreach(var columnSchema in table.Columns){
if(!columnSchema.IsPrimaryKeyMember){
list.Add("#{"+convertCamel(columnSchema.Name)+"}");
}
}
return string.Join(",",list.ToArray());
}
private string getUpdateColumnValues(TableSchema table){
List<string> list=new List<string>();
foreach(var columnSchema in table.Columns){
if(!columnSchema.IsPrimaryKeyMember){
list.Add(columnSchema.Name+"=#{"+convertCamel(columnSchema.Name)+"}");
}
}
return string.Join(",",list.ToArray());
}
</script>

provider-java

<%--
Name:
Author: maomao
Created:<%=Datetime.Now.ToShortDateString() %>
Description:
--%>
<%@ Template Language="C#" TargetLanguage="Java" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Collections"%>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="package" Type="String" Description="包名" Category="包名" %> package <%=package %>.interfaces.provider; import <%=package %>.domain.<%=convertClassName(SourceTable.Name) %>;
import org.apache.ibatis.jdbc.SQL;
import java.util.Map; public class <%=convertClassName(SourceTable.Name) %>Provider
{
public String selectWithParam(Map<String, Object> param) {
return new SQL() {
{
SELECT("*");
FROM("<%=SourceTable.Name %>");
<%foreach(ColumnSchema column in SourceTable.Columns){ %>
if (param.get("<%=convertCamel(column.Name) %>") != null) {
WHERE(" <%=column.Name %>=#{<%=convertCamel(column.Name) %>} ");
}
<%} %>
}
}.toString();
}
} <script runat="template">
private string convertoJavaType(string name){
switch(name){
case "System.Int64":
return "Long";
case "System.Int32":
return "Integer";
case "System.UInt32":
return "Integer";
case "System.Int16":
return "Integer";
case "System.SByte":
return "Integer";
case "System.String":
return "String";
case "System.DateTime":
return "Date";
default:
return "unknown";
}
}
private string convertClassName(string name){
string[] strs=name.Split(new string[1]{"_"},StringSplitOptions.RemoveEmptyEntries);
string result="";
for(int i=0;i<strs.Length;i++){
string firstLetter=strs[i].Substring(0,1);
string left=strs[i].Substring(1);
result+=firstLetter.ToUpper()+left;
}
return result;
}
private string convertCamel(string name){
string[] strs=name.Split(new string[1]{"_"},StringSplitOptions.RemoveEmptyEntries);
string result=strs[0];
for(int i=1;i<strs.Length;i++){
string firstLetter=strs[i].Substring(0,1);
string left=strs[i].Substring(1);
result+=firstLetter.ToUpper()+left;
}
return result;
}
private string getInsertColumns(TableSchema table){
List<string> list=new List<string>();
foreach(var columnSchema in table.Columns){
if(!columnSchema.IsPrimaryKeyMember){
list.Add(columnSchema.Name);
}
}
return string.Join(",",list.ToArray());
}
private string getInsertColumnValues(TableSchema table){
List<string> list=new List<string>();
foreach(var columnSchema in table.Columns){
if(!columnSchema.IsPrimaryKeyMember){
list.Add("#{"+convertCamel(columnSchema.Name)+"}");
}
}
return string.Join(",",list.ToArray());
}
private string getUpdateColumnValues(TableSchema table){
List<string> list=new List<string>();
foreach(var columnSchema in table.Columns){
if(!columnSchema.IsPrimaryKeyMember){
list.Add(columnSchema.Name+"=#{"+convertCamel(columnSchema.Name)+"}");
}
}
return string.Join(",",list.ToArray());
}
</script>

Model:

<%--
Name:
Author: maomao
Created:<%=Datetime.Now.ToShortDateString() %>
Description:
--%>
<%@ Template Language="C#" TargetLanguage="C#" Inherits="SqlCodeTemplate" %>
<%@ Assembly Name="Codesmith.BaseTemplates" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="NameSpace" Type="String" Description="命名空间" %> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data; namespace <%=NameSpace %>
{
[Serializable]
public partial class <%=ConvertTablename2Pascal(SourceTable) %>
{
#region 属性
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
/// <summary>
/// <%=col.Description %>
/// </summary>
public <%=GetCSharpVariableType(col) %> <%=col.Name %> {get;set;}
<%} %>
#endregion
public <%=ConvertTablename2Pascal(SourceTable) %>() { }
public <%=ConvertTablename2Pascal(SourceTable) %>(DataRow dr)
{
#region 属性
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
if(dr["<%=col.Name %>"]!=DBNull.Value)
{
this.<%=col.Name %>= (<%=GetCSharpVariableType(col) %>)dr["<%=col.Name %>"];
}
<%} %>
#endregion
}
}
}
<script runat="template">
public string Convert2Pascal(ColumnSchema col)
{
StringBuilder sb = new StringBuilder();
string[] strs = col.Name.Split(new char[] { '_'});
foreach (string str in strs)
{
sb.Append(str.Substring(,).ToUpper());
sb.Append(str.Substring());
}
return sb.ToString();
}
public string ConvertTablename2Pascal(TableSchema table)
{
StringBuilder sb = new StringBuilder();
string[] strs = table.Name.Split(new char[] { '_'});
int index=;
foreach (string str in strs)
{
if(index==)
{
index++;
continue;
}
sb.Append(str.Substring(,).ToUpper());
sb.Append(str.Substring());
}
return sb.ToString();
}
</script>

Model

<%--
Name:
Author: maomao
Created:<%=Datetime.Now.ToShortDateString() %>
Description:
--%>
<%@ Template Language="C#" TargetLanguage="C#" Inherits="SqlCodeTemplate" %>
<%@ Assembly Name="Codesmith.BaseTemplates" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="NameSpace" Type="String" Description="命名空间" %> using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; namespace <%=NameSpace %>
{
public partial class <%=SourceTable.Name %>Map:EntityTypeConfiguration<<%=SourceTable.Name %>>
{
public <%=SourceTable.Name %>Map()
{
this.ToTable("<%=SourceTable.Name %>");
<%if(SourceTable.HasPrimaryKey){ %>
this.HasKey(t => new {
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
<%if(col.IsPrimaryKeyMember){ %>
t.<%=col.Name %>,
<%} %>
<%} %>
});
<%} %>
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
<%if((bool)col.ExtendedProperties["CS_isIdentity"].Value){ %>
this.Property(t => t.<%=col.Name %>).HasColumnName("<%=col.Name %>").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
<%}else{ %>
<%if(GetCSharpVariableType(col)=="string"&&col.Size!=-) {%>
this.Property(t => t.<%=col.Name %>).HasColumnName("<%=col.Name %>").HasMaxLength(<%=col.Size %>);
<%}else{ %>
this.Property(t => t.<%=col.Name %>).HasColumnName("<%=col.Name %>");
<%} %>
<%} %>
<%} %>
}
}
}
<script runat="template">
public string Convert2Pascal(ColumnSchema col)
{
StringBuilder sb = new StringBuilder();
string[] strs = col.Name.Split(new char[] { '_'});
foreach (string str in strs)
{
sb.Append(str.Substring(,).ToUpper());
sb.Append(str.Substring());
}
return sb.ToString();
} </script>

Map

<%--
Name:
Author: maomao
Created:<%=Datetime.Now.ToShortDateString() %>
Description:
--%>
<%@ Template Language="C#" TargetLanguage="C#" Inherits="SqlCodeTemplate" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="NameSpace" Type="String" Description="命名空间" %> using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace <%=NameSpace %>
{
public static partial class <%=ConvertTablename2Pascal(SourceTable) %>DAL
{
public static List<<%=ConvertTablename2Pascal(SourceTable) %>> Search(string sqlStr,List<SqlParameter> pms)
{
List<<%=ConvertTablename2Pascal(SourceTable) %>> list = new List<<%=ConvertTablename2Pascal(SourceTable) %>>();
DataTable table = SqlHelper.ExecuteDataTable(sqlStr,pms.ToArray());
foreach (DataRow dr in table.Rows)
{
<%=ConvertTablename2Pascal(SourceTable) %> model = new <%=ConvertTablename2Pascal(SourceTable) %>(dr);
list.Add(model);
}
return list;
}
public static bool Insert(<%=ConvertTablename2Pascal(SourceTable) %> model)
{
string sqlStr = "";
List<string> fileds = new List<string>();
List<string> pFileds = new List<string>();
List<SqlParameter> pms = new List<SqlParameter>();
#region 添加参数
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
<%if((bool)(col.ExtendedProperties["CS_IsIdentity"].Value)==true){continue;} %>
<%if(col.SystemType==typeof(DateTime))
{ %>
if(model.<%=col.Name %>!=null&&model.<%=col.Name %>!=new DateTime())
{
fileds.Add("[<%=col.Name %>]");
pFileds.Add("@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>",SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
}
<% }else {%>
<%if(!col.SystemType.IsValueType){ %>
if(model.<%=col.Name %>!=null)
{
fileds.Add("[<%=col.Name %>]");
pFileds.Add("@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>", SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
}
<%} else{%>
{
fileds.Add("[<%=col.Name %>]");
pFileds.Add("@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>", SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
}
<%} %>
<%} %>
<%} %>
#endregion
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO <%=SourceTable.Name %> (");
sb.Append(string.Join(",", fileds));
sb.Append(") values (");
sb.Append(string.Join(",", pFileds));
sb.Append(")");
sqlStr = sb.ToString();
int i= SqlHelper.ExecuteNonQuery(sqlStr, pms.ToArray());
return i>;
} public static bool Update(<%=ConvertTablename2Pascal(SourceTable) %> model)
{
string sqlStr = "";
List<string> fileds = new List<string>();
List<string> pFileds = new List<string>();
List<SqlParameter> pms = new List<SqlParameter>();
#region 添加参数
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
<%if(col.IsPrimaryKeyMember){ %>
pFileds.Add("[<%=col.Name %>]=@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>", SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
<%} else{%>
<%if(col.SystemType==typeof(DateTime)){ %>
if(model.<%=col.Name %>!=null&&model.<%=col.Name %>!=new DateTime())
{
fileds.Add("[<%=col.Name %>]=@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>", SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
}
<% }else {%>
<%if(!col.SystemType.IsValueType){ %>
if(model.<%=col.Name %>!=null)
{
fileds.Add("[<%=col.Name %>]=@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>", SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
}
<%} else{%>
fileds.Add("[<%=col.Name %>]=@<%=col.Name %>");
pms.Add(new SqlParameter("<%=col.Name %>", SqlDbType.<%=GetSqlDbType(col) %>,<%=col.Size %>){Value=model.<%=col.Name %>});
<%} %>
<%} %>
<%} %>
<%} %>
#endregion
StringBuilder sb = new StringBuilder();
sb.Append("update <%=SourceTable.Name %> set ");
sb.Append(string.Join(",", fileds));
sb.Append(" where ");
sb.Append(string.Join(" and ", pFileds));
sqlStr = sb.ToString();
int i= SqlHelper.ExecuteNonQuery(sqlStr, pms.ToArray());
return i>;
}
}
}
<script runat="template">
public string Convert2Pascal(string name)
{
StringBuilder sb = new StringBuilder();
string[] strs = name.Split(new char[] { '_'});
foreach (string str in strs)
{
sb.Append(str.Substring(,).ToUpper());
sb.Append(str.Substring());
}
return sb.ToString();
}
public string ConvertTablename2Pascal(TableSchema table)
{
StringBuilder sb = new StringBuilder();
string[] strs = table.Name.Split(new char[] { '_'});
int index=;
foreach (string str in strs)
{
if(index==)
{
index++;
continue;
}
sb.Append(str.Substring(,).ToUpper());
sb.Append(str.Substring());
}
return sb.ToString();
}
</script>

DAL

<%@ Template Language="C#" TargetLanguage="C#" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="NameSpace" Type="String" Description="命名空间" %> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data; namespace <%=NameSpace %>
{
public partial class <%=SourceTable.Name %>
{
#region 属性
<%for(int i=;i<SourceTable.Columns.Count;i++){ %>
/// <summary>
/// <%=SourceTable.Columns[i].Description %>
/// </summary>
public <%=SourceTable.Columns[i].SystemType %> <%=SourceTable.Columns[i].Name %> {get;set;}
<%} %>
#endregion
public <%=SourceTable.Name %>() { }
public <%=SourceTable.Name %>(DataRow dr)
{
<%for(int i=;i<SourceTable.Columns.Count;i++){ %>
if(dr["<%=SourceTable.Columns[i].Name %>"]!=DBNull.Value)
{
this.<%=SourceTable.Columns[i].Name %>= (<%=SourceTable.Columns[i].SystemType %>)dr["<%=SourceTable.Columns[i].Name %>"];
}
<%} %>
}
}
}

SqlHelper:

<%@ Template Language="C#" TargetLanguage="C#" %>
<%@ Property Name="NameSpace" Type="String" Category="命名空间" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data; namespace <%=NameSpace %>
{
static class SqlHelper
{
public static readonly string connstr =
ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; public static int ExecuteNonQuery(string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
} public static object ExecuteScalar(string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
} public static DataTable ExecuteDataTable(string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters); DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
return dataset.Tables[];
}
}
} public static object FromDbValue(object value)
{
if (value == DBNull.Value)
{
return null;
}
else
{
return value;
}
} public static object ToDbValue(object value)
{
if (value == null)
{
return DBNull.Value;
}
else
{
return value;
}
}
}
}

DAL:

<%@ Template Language="C#" TargetLanguage="C#" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %>
<%@ Property Name="NameSpace" Type="String" Description="命名空间" %> using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace <%=NameSpace %>
{
public static partial class <%=SourceTable.Name %>DAL
{
public static List<<%=SourceTable.Name %>> Search(string sqlStr,List<SqlParameter> pms)
{
List<<%=SourceTable.Name %>> list = new List<<%=SourceTable.Name %>>();
DataTable table = SqlHelper.ExecuteDataTable(sqlStr,pms.ToArray());
foreach (DataRow dr in table.Rows)
{
<%=SourceTable.Name %> model = new <%=SourceTable.Name %>(dr);
list.Add(model);
}
return list;
}
public static bool Insert(<%=SourceTable.Name %> model)
{
string sqlStr = "";
List<string> fileds = new List<string>();
List<string> pFileds = new List<string>();
List<SqlParameter> pms = new List<SqlParameter>();
#region 添加字段
<%for(int i=;i<SourceTable.Columns.Count;i++){ %> <%if((bool)(SourceTable.Columns[i].ExtendedProperties["CS_IsIdentity"].Value)==true){continue;} %> <%if(SourceTable.Columns[i].SystemType==typeof(DateTime))
{ %>
if(model.<%=SourceTable.Columns[i].Name %>!=null&&model.<%=SourceTable.Columns[i].Name %>!=new DateTime())
{
fileds.Add("[<%=SourceTable.Columns[i].Name %>]");
pFileds.Add("@<%=SourceTable.Columns[i].Name %>");
pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));
}
<% }else {%>
if(model.<%=SourceTable.Columns[i].Name %>!=null)
{
fileds.Add("[<%=SourceTable.Columns[i].Name %>]");
pFileds.Add("@<%=SourceTable.Columns[i].Name %>");
pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));
}
<%} %> <%} %>
#endregion
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO <%=SourceTable.Name %> (");
sb.Append(string.Join(",", fileds));
sb.Append(") values (");
sb.Append(string.Join(",", pFileds));
sb.Append(")");
sqlStr = sb.ToString();
int i= SqlHelper.ExecuteNonQuery(sqlStr, pms.ToArray());
return i>;
} public static bool Update(<%=SourceTable.Name %> model)
{
string sqlStr = "";
List<string> fileds = new List<string>();
List<string> pFileds = new List<string>();
List<SqlParameter> pms = new List<SqlParameter>();
#region 添加字段
<%for(int i=;i<SourceTable.Columns.Count;i++){ %> <%if(SourceTable.Columns[i].IsPrimaryKeyMember){ %>
pFileds.Add("[<%=SourceTable.Columns[i].Name %>]=@<%=SourceTable.Columns[i].Name %>");
pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));
<%} else{ %> <%if(SourceTable.Columns[i].SystemType==typeof(DateTime))
{ %>
if(model.<%=SourceTable.Columns[i].Name %>!=null&&model.<%=SourceTable.Columns[i].Name %>!=new DateTime())
{
fileds.Add("[<%=SourceTable.Columns[i].Name %>]=@<%=SourceTable.Columns[i].Name %>");
pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));
}
<% }else {%>
if(model.<%=SourceTable.Columns[i].Name %>!=null)
{
fileds.Add("[<%=SourceTable.Columns[i].Name %>]=@<%=SourceTable.Columns[i].Name %>");
pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));
}
<%} %>
<%} %>
<%} %>
#endregion
StringBuilder sb = new StringBuilder();
sb.Append("update <%=SourceTable.Name %> set ");
sb.Append(string.Join(",", fileds));
sb.Append(" where ");
sb.Append(string.Join(" and ", pFileds));
sqlStr = sb.ToString();
int i= SqlHelper.ExecuteNonQuery(sqlStr, pms.ToArray());
return i>;
}
}
}

Tables:遍历库中所有表

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="List all database tables" %>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Database containing the tables." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
Tables in database "<%= SourceDatabase %>":
<% for (int i = ; i < SourceDatabase.Tables.Count; i++) { %>
        <%= SourceDatabase.Tables[i].Name %> <% } %>

分享一套简单的CodeSmith三层模板的更多相关文章

  1. 分享一套 CodeSmit 代码生成模板。

    分享一套 CodeSmit 代码生成模板. 住博客园 5 年了,以前也发过一些博文,但都在 一天后 / 几周后 / 几年后 将它删了:因为感觉代码写得不好:不清晰或侵入太大,哪怕只有一句侵入. 可是最 ...

  2. 分享一套Code Smith 搭建N层架构模板

    开篇 平常开发时,由于冗余代码过多,程序员做重复的工作过多势必会影响开发效率.倘若 对重复性代码简单的复制.粘贴,虽然也能节省时间,但也需仔细一步步替换,这无疑也是一件费力的事.这时我们急需代码生成工 ...

  3. 不再害羞,过程比结果更重要;分享一套 CodeSmit 代码生成模板。

    住博客园 5 年了,以前也发过一些博文,但都在 一天后 / 几周后 / 几年后 将它删了:因为感觉代码写得不好:不清晰或侵入太大,哪怕只有一句侵入. 可是最近重写一套 CodeSmith 代码生成模板 ...

  4. 分享几套bootstrap后台模板【TP5版】

    分享几套bootstrap后台模板[TP5版],模板来源于网络,需要的拿走.1.AdminLTE 链接: http://pan.baidu.com/s/1o7BXeCM 密码: zfhy 2.Boot ...

  5. 圣诞礼物:分享几套漂亮的圣诞节 PSD 素材

    马上就到圣诞节了,这篇文章要给大家分享几套精美的圣诞节相关的 PSD 设计素材,你可以免费下载使用,用于圣诞节相关的设计项目中.这些免费素材能够帮助你节省大量的时间,而且能有很好的效果. 您可能感兴趣 ...

  6. 分享一套精美的现代 UI PSD 工具包【免费下载】

    艾库特·耶尔马兹,是土耳其伊斯坦布尔的一位高级艺术总监,他向大家分享了一套美丽的现代 UI 工具包,你可以免费下载.所以,你可以使用这个优秀的素材设计视觉互动和有吸引力的用户界面. 此 UI 套件提供 ...

  7. Silverlight分享一套企业开发主题

    Silverlight分享一套企业开发主题 Silverlight默认主题时间长了,也视觉疲劳了,于是上网上找了下Silverlight主题.发现SL的主题并不多,下面这套JetPack主题还是SL4 ...

  8. 分享几套生成iMac相关高逼格免费mockup的素材和在线工具

    好久没有过来转, 今天姐姐我分享几套高逼格的iMac相关设计资源, 希望各位靓妹帅哥会喜欢, 最重要滴是,都是FREE,此处应有掌声~~~ , yeah!! iMac桌面效果Mockup 只需要下载后 ...

  9. 一套简单的web即时通讯——第一版

    前言 我们之前已经实现了 WebSocket+Java 私聊.群聊实例,后面我们模仿layer弹窗,封装了一个自己的web弹窗 自定义web弹窗/层:简易风格的msg与可拖放的dialog,生成博客园 ...

随机推荐

  1. java使用filter设置跨域访问

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  2. 利用nginx打造个人简历网页

    1.下载nginx nginx官方网址:http://nginx.org/ 2.下载和解压 #下载:[root@iZwz9cl4i8oy1reej7o8pmZ soft]# wget http://n ...

  3. MATLAB二分法函数求根

    function xc = bisect(f,a,b,tol) ind = b-a; while ind > tol xx = (a+b)/; b = xx; else a = xx; end ...

  4. java基础-day20

    第09天 IO流 今日内容介绍 u  File类 u  字符流与字节流 第1章   File类 1.1      File概述 打开API,搜索File类.阅读其描述:File文件和目录路径名的抽象表 ...

  5. hdu 1130 How Many Trees? 【卡特兰数】

    题目 题意:给你一个数字n,问你将1~n这n个数字,可以组成多少棵不同的二叉搜索树. 1,2,5,14--根据输出中的规律可以看出这是一个卡特兰数的序列.于是代用卡特兰数中的一个递推式: 因为输入可取 ...

  6. hihocoder 二分·二分答案【二分搜索,最大化最小值】 (bfs)

    题目 这道题做了几个小时了都没有做出来,首先是题意搞了半天都没有弄懂,难道真的是因为我不打游戏所以连题都读不懂了? 反正今天是弄不懂了,过几天再来看看... 题意:一个人从1点出发到T点去打boss, ...

  7. 如果datanode连接不上namenode,导致datanode无法启动。

    如果datanode连接不上namenode,导致datanode无法启动. 问题:  ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: j ...

  8. js-倒计时原理

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  9. day04_雷神_函数

    #day04 1.函数 1.1函数传参 函数定义的时候是形参:函数执行的时候是实参 实参: 位置参数.关键字参数.混合参数 位置参数:位置一一对应 关键字参数: 可以位置不对应 混合参数:关键字参数要 ...

  10. oc门

    OC门电路,即集电极开路输出结构门电路,电路结构图为: