Exemplo n.º 1
0
        public static string CreateEntityClass(EntityClassInfo classInfo, string templatePath)
        {
            CustomTextTemplatingEngineHost host = new CustomTextTemplatingEngineHost();

            host.TemplateFileValue = templatePath;
            string input = File.ReadAllText(templatePath);

            host.Session = new TextTemplatingSession();
            host.Session.Add("entity", classInfo);

            string output = new Engine().ProcessTemplate(input, host);

            StringBuilder errorWarn = new StringBuilder();

            foreach (CompilerError error in host.Errors)
            {
                errorWarn.Append(error.Line).Append(":").AppendLine(error.ErrorText);
            }
            if (!File.Exists("Error.log"))
            {
                File.Create("Error.log");
            }
            File.WriteAllText("Error.log", errorWarn.ToString());
            return(output);
        }
Exemplo n.º 2
0
        public static string CreateDataAccessClass(EntityClassInfo classInfo)
        {
            string templatePath = string.Empty;

            try
            {
                templatePath = System.Configuration.ConfigurationManager.AppSettings["TemplateDataAccess"].ToString();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("读取配置文件错误!TemplateDataAccess" + ex.Message);
                return(null);
            }
            if (!File.Exists(templatePath))
            {
                //MessageBox.Show("未找到DataAccess.tt,请修改配置文件!");
                return(null);
            }
            CustomTextTemplatingEngineHost host = new CustomTextTemplatingEngineHost();

            host.TemplateFileValue = templatePath;
            string input = File.ReadAllText(templatePath);

            host.Session = new TextTemplatingSession();
            host.Session.Add("entity", classInfo);

            string output = new Engine().ProcessTemplate(input, host);

            StringBuilder errorWarn = new StringBuilder();

            foreach (CompilerError error in host.Errors)
            {
                errorWarn.Append(error.Line).Append(":").AppendLine(error.ErrorText);
            }
            if (!File.Exists("Error.log"))
            {
                File.Create("Error.log");
            }
            File.WriteAllText("Error.log", errorWarn.ToString());

            return(output);
        }
Exemplo n.º 3
0
        public static string CreateEntityClass(EntityClassInfo classInfo,string templatePath)
        {
            CustomTextTemplatingEngineHost host = new CustomTextTemplatingEngineHost();
            host.TemplateFileValue = templatePath;
            string input = File.ReadAllText(templatePath);
            host.Session = new TextTemplatingSession();
            host.Session.Add("entity", classInfo);
            
            string output = new Engine().ProcessTemplate(input, host);

            StringBuilder errorWarn = new StringBuilder();
            foreach (CompilerError error in host.Errors)
            {
                errorWarn.Append(error.Line).Append(":").AppendLine(error.ErrorText);
            }
            if (!File.Exists("Error.log"))
            {
                File.Create("Error.log");
            }
            File.WriteAllText("Error.log", errorWarn.ToString());
            return output;
        }
Exemplo n.º 4
0
        public static string CreateDataAccessClass(EntityClassInfo classInfo)
        {
            string templatePath = string.Empty;
            try
            {
                templatePath = System.Configuration.ConfigurationManager.AppSettings["TemplateDataAccess"].ToString();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("读取配置文件错误!TemplateDataAccess" + ex.Message);
                return null;
            }
            if (!File.Exists(templatePath))
            {
                //MessageBox.Show("未找到DataAccess.tt,请修改配置文件!");
                return null;
            }
            CustomTextTemplatingEngineHost host = new CustomTextTemplatingEngineHost();
            host.TemplateFileValue = templatePath;
            string input = File.ReadAllText(templatePath);
            host.Session = new TextTemplatingSession();
            host.Session.Add("entity", classInfo);

            string output = new Engine().ProcessTemplate(input, host);

            StringBuilder errorWarn = new StringBuilder();
            foreach (CompilerError error in host.Errors)
            {
                errorWarn.Append(error.Line).Append(":").AppendLine(error.ErrorText);
            }
            if (!File.Exists("Error.log"))
            {
                File.Create("Error.log");
            }
            File.WriteAllText("Error.log", errorWarn.ToString());
            
            return output;
        }
Exemplo n.º 5
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            DbHelper dbHelper = new DbHelper(connString);
            DataTable dtColumn = new DataTable();
            if (lvTabeleName.CheckedItems.Count == 0) {
                MessageBox.Show("请选择要生成代码的数据表");
                return;
            }

            lboxInfo.Items.Clear();

            lboxInfo.Items.Add("正在初始化相关包名与目录...");
            intPackageAndPath();
            lboxInfo.Items.Add("初始化完成 OK");

            string COLUMN_SQL = @"SELECT  
                Name=a.name,
                AutoIncrement=case   when   COLUMNPROPERTY(   a.id,a.name,'IsIdentity')=1   then   '是'else   '否'   end,
                IsPK=case   when   exists(SELECT   1   FROM   sysobjects   where   xtype='PK'   and   name   in   (
                SELECT   name   FROM   sysindexes   WHERE   indid   in(
                SELECT   indid   FROM   sysindexkeys   WHERE   id   =   a.id   AND   colid=a.colid
                )))   then   '是'   else   '否'   end,
                DataType=b.name,
                BitLenght=a.length,
                MaxLength=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
                Digits=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
                CanNull=case   when   a.isnullable=1   then   '是'else   '否'   end,
                DefaultValue=isnull(e.text,''),
                Label=isnull(g.[value],'')
                FROM   syscolumns   a
                left   join   systypes   b   on   a.xusertype=b.xusertype
                inner   join   sysobjects   d   on   a.id=d.id     and   d.xtype='U'   and     d.name<>'dtproperties'
                left   join   syscomments   e   on   a.cdefault=e.id
                left   join   sys.extended_properties   g   on   a.id=g.major_id   and   a.colid=g.minor_id
                left   join   sys.extended_properties   f   on   d.id=f.major_id   and   f.minor_id=0
                where   d.name='{0}' 
                order   by   a.id,a.colorder
            ";
            string selTableName = string.Empty;
            string selTableComm = string.Empty;
            string tempClassName = string.Empty;
            string className = string.Empty;
            //定义要生成的实体中需要排除的列
            //主要是业务实体继承了基实体,在业务实体中就不需要再生成这些字段的映射了
            string excludes = "CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,VERSION,ISDELETE,ORDER_INDEX,EXT_FIELD01,EXT_FIELD02,EXT_FIELD03,EXT_FIELD04,EXT_FIELD05,NODE_LAYER,NODE_INFO,NODE_TYPE,NODE_INFOTYPE";
            try
            {
                for (int i = 0; i < lvTabeleName.CheckedItems.Count; i++)
                {
                    selTableName = lvTabeleName.CheckedItems[i].Tag.ToString();
                    selTableComm = lvTabeleName.CheckedItems[i].Text.ToString();

                    lboxInfo.Items.Add("处理数据表" + selTableComm  + "的相关信息...");

                    dtColumn = dbHelper.Fill(string.Format(COLUMN_SQL, selTableName));
                    EntityClassInfo entityInfo = new EntityClassInfo();
                    entityInfo.tableName = selTableName;
                    entityInfo.tableComment = selTableComm;
                    tempClassName = ConvertHelper.SplitAndToFirstUpper(selTableName, '_');
                    className = tempClassName.Substring(0, 1).ToUpper() + tempClassName.Substring(1, tempClassName.Length - 1);
                    entityInfo.className = className;
                    entityInfo.packageName = modelPackageName;
                    entityInfo.daoPackageName = daoPackageName;
                    entityInfo.servicePackageName = servicePackageName;
                    entityInfo.controllerPackageName = controllerPackageName;
                    entityInfo.dataTable = dtColumn;
                    entityInfo.codeLanguage = codeLanguage.Java;
                    entityInfo.excludes = excludes;

                    string templatePath = ConfigurationManager.AppSettings["TemplateEntity"].ToString();
                    entityInfo.createColumnInfo();

                    //rtboxView.Clear();
                    //生成实体层代码
                    lboxInfo.Items.Add("生成数据表" + selTableComm + "的实体层代码...");
                    string codeEntity = CreateCode.CreateEntityClass(entityInfo, templatePath);
                    //rtboxView.AppendText(codeEntity);
                    if (!Directory.Exists(modelFileDir)){
                        Directory.CreateDirectory(modelFileDir);
                    }
                    File.WriteAllText(modelFileDir + className + ".java",codeEntity);

                    //生成Dao接口层代码
                    lboxInfo.Items.Add("生成数据表" + selTableComm + "的dao接口层代码...");
                    string templatePathDao = ConfigurationManager.AppSettings["TemplateDao"].ToString();
                    String codeDao = CreateCode.CreateEntityClass(entityInfo, templatePathDao);
                    //rtboxView.AppendText(codeDao);
                    if (!Directory.Exists(daoFileDir)){
                        Directory.CreateDirectory(daoFileDir);
                    }
                    File.WriteAllText(daoFileDir + className + "Dao.java",codeDao);

                    //生成Dao接口实现层代码
                    lboxInfo.Items.Add("生成数据表" + selTableComm + "的dao接口实现层代码...");
                    string TemplateDaoImpl = ConfigurationManager.AppSettings["TemplateDaoImpl"].ToString();
                    String codeDaoImpl = CreateCode.CreateEntityClass(entityInfo, TemplateDaoImpl);
                    //rtboxView.AppendText(codeDaoImpl);
                    String daoImplFileDir = daoFileDir + @"\Impl\";
                    if (!Directory.Exists(daoImplFileDir)){
                        Directory.CreateDirectory(daoImplFileDir);
                    }
                    File.WriteAllText(daoImplFileDir + className + "DaoImpl.java",codeDaoImpl);

                    //生成Service接口层代码
                    lboxInfo.Items.Add("生成数据表" + selTableComm + "的service接口层代码...");
                    String TemplateService = ConfigurationManager.AppSettings["TemplateService"].ToString();
                    String codeService = CreateCode.CreateEntityClass(entityInfo, TemplateService);
                    //rtboxView.AppendText(codeService);
                    if (!Directory.Exists(serviceFileDir))
                {
                        Directory.CreateDirectory(serviceFileDir);
                    }
                    File.WriteAllText(serviceFileDir + className + "Service.java",codeService);

                    //生成Service接口实现层代码
                    lboxInfo.Items.Add("生成数据表" + selTableComm + "的service接口实现层代码...");
                    String TemplateServiceImpl = ConfigurationManager.AppSettings["TemplateServiceImpl"].ToString();
                    String codeServiceImpl = CreateCode.CreateEntityClass(entityInfo, TemplateServiceImpl);
                    String serviceImplFileDir = serviceFileDir + @"\Impl\";
                    //rtboxView.AppendText(codeServiceImpl);

                    if (!Directory.Exists(serviceImplFileDir))
                    {
                        Directory.CreateDirectory(serviceImplFileDir);
                    }
                    File.WriteAllText(serviceImplFileDir + className + "ServiceImpl.java",codeServiceImpl);
                    
                    //生成Controller接口实现层代码
                    lboxInfo.Items.Add("生成数据表" + selTableComm + "的controller层代码...");
                    String TemplateController = ConfigurationManager.AppSettings["TemplateController"].ToString();
                    String codeController = CreateCode.CreateEntityClass(entityInfo, TemplateController);
                    //String controller = serviceFileDir + @"\Impl\";
                    //rtboxView.AppendText(codeServiceImpl);

                    if (!Directory.Exists(controllerFileDir))
                    {
                        Directory.CreateDirectory(controllerFileDir);
                    }
                    File.WriteAllText(controllerFileDir + className + "Controller.java", codeController);
                }
                lboxInfo.Items.Add("全部所选数据表的代码生成完毕!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取数据库失败!" + ex.Message);
                return;
            }
        }