如果要连接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. mmm和mmma的区别

    m:编译整个安卓系统 makes from the top of the tree mm:编译当前目录下的模块,当前目录下需要有Android.mk这个makefile文件,否则就往上找最近的Andr ...

  2. 使用 WLST 和节点管理器来管理服务器

    使用节点管理器启动计算机上的服务器 WLST 可以连接至在任何计算机上运行的节点管理器,并能够在此计算机上启动一个或多个 WebLogic Server 实例.要通过此技术使用 WLST 和节点管理器 ...

  3. 【repost】 原生JS执行环境与作用域深入理解

    首先,我们要知道执行环境和作用域是两个完全不同的概念. 函数的每次调用都有与之紧密相关的作用域和执行环境.从根本上来说,作用域是基于函数的,而执行环境是基于对象的(例如:全局执行环境即window对象 ...

  4. mysql变更数据的捕获和入库

    问题:涉及状态的信息,mysql中是update的,缺少中间状态的记录.数据分析中需要这部分数据. 思路:后端服务通过监控某张表的某个字段,根据mysql的binlog文件,还原数据,发送到kafka ...

  5. EBS Custom Password Rules

    https://blogs.oracle.com/manojmadhusoodanan/entry/custom_password_rules Custom Password Rules By Man ...

  6. 1.mybatis入门

    一:创建表 CREATE TABLE `country` ( `id` ) NOT NULL AUTO_INCREMENT, `countryname` varchar() DEFAULT NULL, ...

  7. Android-Kotlin-递归与尾递归

    递归: 阶乘计算: /** * 阶乘: * 1的阶乘是1,因为1往下走一个楼梯 就是0了 * 2的阶乘是 2*1 * 3的继承是 3*2*1 * 4的继承是 4*3*2*1 * 5的阶乘是 5*4*2 ...

  8. Elasticsearch 系列4 --- Windows10安装Kibana

    Kibana是Elastic Stack家族内的一部分,它是一个管理网站,与ES(Elastic Search)集成可以用来管理ES的索引,除ES外它还可以跟Elastic家族的其他组件进行整合如lo ...

  9. 【图数据结构的遍历】java实现广度优先和深度优先遍历

    [图数据结构的遍历]java实现广度优先和深度优先遍历 宽度优先搜索(BFS)遍历图需要使用队列queue数据结构: 深度优先搜索(DFS, Depth First Search)的实现 需要使用到栈 ...

  10. Grid++Report报表工具C/S实战篇(五)

    一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的第五部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理 ...