博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通用超级强大的基于Oracle数据库的代码生成器
阅读量:6639 次
发布时间:2019-06-25

本文共 14722 字,大约阅读时间需要 49 分钟。

项目中使用了Oracle数据库,命名基本规范为表名和字段名全部大写,用下划线分割各个单词

如“BASE_USER_LOGON_EXTEND”这个表表示用户登录的扩展表。

基于这个规范,通用权限管理系统配套升级了代码生成器工具。

下面以Oracle数据库中的BASE_USER_LOGON_EXTEND表来使用这个工具进行实体类和业务类代码的生成。

 表结构图

 代码生成器截图

 实体层代码

1 //-----------------------------------------------------------------------  2 // 
3 // Copyright (c) 2015 , All rights reserved. 4 //
5 //----------------------------------------------------------------------- 6 7 using System; 8 using System.Collections.Generic; 9 using System.Linq; 10 using System.Data; 11 12 namespace DotNet.Business 13 { 14 using DotNet.Utilities; 15 16 /// 17 /// BaseUserLogonExtendEntity 18 /// 用户登录的扩展表,账号登录方式,登录提醒方式 19 /// 20 /// 修改纪录 21 /// 22 /// 2015-01-26 版本:1.0 JiRiGaLa 创建文件。 23 /// 24 ///
25 ///
JiRiGaLa
26 ///
2015-01-26
27 ///
28 ///
29 public partial class BaseUserLogonExtendEntity : BaseEntity 30 { 31 private Decimal id; 32 /// 33 /// 主键 用户ID 34 /// 35 public Decimal Id 36 { 37 get 38 { 39 return id; 40 } 41 set 42 { 43 id = value; 44 } 45 } 46 47 private Decimal? emailRemind = null; 48 /// 49 /// 登录邮件提醒 50 /// 51 public Decimal? EmailRemind 52 { 53 get 54 { 55 return emailRemind; 56 } 57 set 58 { 59 emailRemind = value; 60 } 61 } 62 63 private Decimal? qrCodeLogon = null; 64 /// 65 /// 二维码登录 66 /// 67 public Decimal? QrCodeLogon 68 { 69 get 70 { 71 return qrCodeLogon; 72 } 73 set 74 { 75 qrCodeLogon = value; 76 } 77 } 78 79 private Decimal? jixinRemind = null; 80 /// 81 /// 登录提醒 82 /// 83 public Decimal? JixinRemind 84 { 85 get 86 { 87 return jixinRemind; 88 } 89 set 90 { 91 jixinRemind = value; 92 } 93 } 94 95 private Decimal? wechatRemind = null; 96 /// 97 /// 登录微信提醒 98 /// 99 public Decimal? WechatRemind100 {101 get102 {103 return wechatRemind;104 }105 set106 {107 wechatRemind = value;108 }109 }110 111 private Decimal? dynamicCodeLogon = null;112 /// 113 /// 动态码登录114 /// 115 public Decimal? DynamicCodeLogon116 {117 get118 {119 return dynamicCodeLogon;120 }121 set122 {123 dynamicCodeLogon = value;124 }125 }126 127 private Decimal? mobileRemind = null;128 /// 129 /// 登录手机短信提醒130 /// 131 public Decimal? MobileRemind132 {133 get134 {135 return mobileRemind;136 }137 set138 {139 mobileRemind = value;140 }141 }142 143 private Decimal? usernamePasswordLogon = null;144 /// 145 /// 用户名密码方式登录146 /// 147 public Decimal? UsernamePasswordLogon148 {149 get150 {151 return usernamePasswordLogon;152 }153 set154 {155 usernamePasswordLogon = value;156 }157 }158 159 /// 160 /// 从数据行读取161 /// 162 /// 数据行163 protected override BaseEntity GetFrom(IDataRow dr)164 {165 GetFromExpand(dr);166 Id = BaseBusinessLogic.ConvertToDecimal(dr[BaseUserLogonExtendEntity.FieldId]);167 EmailRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldEmailRemind]);168 QrCodeLogon = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldQrCodeLogon]);169 JixinRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldJixinRemind]);170 WechatRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldWechatRemind]);171 DynamicCodeLogon = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldDynamicCodeLogon]);172 MobileRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldMobileRemind]);173 UsernamePasswordLogon = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldUsernamePasswordLogon]);174 return this;175 }176 177 ///178 /// 用户登录的扩展表,账号登录方式,登录提醒方式179 ///180 public static string TableName = "BASE_USER_LOGON_EXTEND";181 182 ///183 /// 主键 用户ID184 ///185 public static string FieldId = "Id";186 187 ///188 /// 登录邮件提醒189 ///190 public static string FieldEmailRemind = "EMAIL_REMIND";191 192 ///193 /// 二维码登录194 ///195 public static string FieldQrCodeLogon = "QR_CODE_LOGON";196 197 ///198 /// 登录其它提醒199 ///200 public static string FieldJixinRemind = "JIXIN_REMIND";201 202 ///203 /// 登录微信提醒204 ///205 public static string FieldWechatRemind = "WECHAT_REMIND";206 207 ///208 /// 动态码登录209 ///210 public static string FieldDynamicCodeLogon = "DYNAMIC_CODE_LOGON";211 212 ///213 /// 登录手机短信提醒214 ///215 public static string FieldMobileRemind = "MOBILE_REMIND";216 217 ///218 /// 用户名密码方式登录219 ///220 public static string FieldUsernamePasswordLogon = "USERNAME_PASSWORD_LOGON";221 }222 }

业务层代码

1 --------------------------------------------  2   3 using System;  4 using System.Collections.Generic;  5 using System.Linq;  6 using System.Data;  7   8 namespace DotNet.Business  9 { 10     using DotNet.Business; 11     using DotNet.Utilities; 12  13     ///  14     /// BaseUserLogonExtendManager 15     /// 用户登录的扩展表,账号登录方式,登录提醒方式 16     ///  17     /// 修改纪录 18     ///  19     /// 2015-01-26 版本:1.0 JiRiGaLa 创建文件。 20     ///  21     /// 
22 ///
JiRiGaLa
23 ///
2015-01-26
24 ///
25 ///
26 public partial class BaseUserLogonExtendManager : BaseManager, IBaseManager 27 { 28 /// 29 /// 构造函数 30 /// 31 public BaseUserLogonExtendManager() 32 { 33 if (base.dbHelper == null) 34 { 35 base.dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType, BaseSystemInfo.UserCenterDbConnection); 36 } 37 if (string.IsNullOrEmpty(base.CurrentTableName)) 38 { 39 base.CurrentTableName = BaseUserLogonExtendEntity.TableName; 40 } 41 base.PrimaryKey = "Id"; 42 } 43 44 /// 45 /// 构造函数 46 /// 指定表名 47 /// 48 public BaseUserLogonExtendManager(string tableName) 49 { 50 base.CurrentTableName = tableName; 51 } 52 53 /// 54 /// 构造函数 55 /// 56 /// 数据库连接 57 public BaseUserLogonExtendManager(IDbHelper dbHelper): this() 58 { 59 DbHelper = dbHelper; 60 } 61 62 /// 63 /// 构造函数 64 /// 65 /// 用户信息 66 public BaseUserLogonExtendManager(BaseUserInfo userInfo) : this() 67 { 68 UserInfo = userInfo; 69 } 70 71 /// 72 /// 构造函数 73 /// 74 /// 用户信息 75 /// 指定表名 76 public BaseUserLogonExtendManager(BaseUserInfo userInfo, string tableName) : this(userInfo) 77 { 78 base.CurrentTableName = tableName; 79 } 80 81 /// 82 /// 构造函数 83 /// 84 /// 数据库连接 85 /// 用户信息 86 public BaseUserLogonExtendManager(IDbHelper dbHelper, BaseUserInfo userInfo) : this(dbHelper) 87 { 88 UserInfo = userInfo; 89 } 90 91 /// 92 /// 构造函数 93 /// 94 /// 数据库连接 95 /// 用户信息 96 /// 指定表名 97 public BaseUserLogonExtendManager(IDbHelper dbHelper, BaseUserInfo userInfo, string tableName) : this(dbHelper, userInfo) 98 { 99 base.CurrentTableName = tableName;100 }101 102 /// 103 /// 添加, 这里可以人工干预,提高程序的性能104 /// 105 /// 实体106 /// 自增量方式,表主键是否采用自增的策略107 /// 返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的108 ///
主键
109 public string Add(BaseUserLogonExtendEntity entity, bool identity = false, bool returnId = false)110 {111 this.Identity = identity;112 this.ReturnId = returnId;113 return this.AddObject(entity);114 }115 116 /// 117 /// 更新118 /// 119 /// 实体120 public int Update(BaseUserLogonExtendEntity entity)121 {122 return this.UpdateObject(entity);123 }124 125 /// 126 /// 获取实体127 /// 128 /// 主键129 public BaseUserLogonExtendEntity GetObject(string id)130 {131 return BaseEntity.Create
(this.GetDataTable(new KeyValuePair
(this.PrimaryKey, id)));132 }133 134 ///
135 /// 添加实体136 /// 137 ///
实体138 public string AddObject(BaseUserLogonExtendEntity entity)139 {140 string key = string.Empty;141 if (entity.Id != null)142 {143 key = entity.Id.ToString();144 }145 SQLBuilder sqlBuilder = new SQLBuilder(DbHelper, this.Identity, this.ReturnId);146 sqlBuilder.BeginInsert(this.CurrentTableName, this.PrimaryKey);147 if (!this.Identity) 148 {149 // 这里已经是指定了主键了,所以不需要返回主键了150 sqlBuilder.ReturnId = false;151 sqlBuilder.SetValue(this.PrimaryKey, entity.Id);152 }153 else154 {155 if (!this.ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))156 {157 if (DbHelper.CurrentDbType == CurrentDbType.Oracle)158 {159 sqlBuilder.SetFormula(this.PrimaryKey, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");160 }161 if (DbHelper.CurrentDbType == CurrentDbType.DB2)162 {163 sqlBuilder.SetFormula(this.PrimaryKey, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());164 }165 }166 else167 {168 if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))169 {170 BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);171 entity.Id = int.Parse(sequenceManager.Increment(this.CurrentTableName));172 sqlBuilder.SetValue(this.PrimaryKey, entity.Id);173 }174 }175 }176 this.SetObject(sqlBuilder, entity);177 if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access))178 {179 key = sqlBuilder.EndInsert().ToString();180 }181 else182 {183 sqlBuilder.EndInsert();184 }185 if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))186 {187 return entity.Id.ToString();188 }189 return key;190 }191 192 ///
193 /// 更新实体194 /// 195 ///
实体196 public int UpdateObject(BaseUserLogonExtendEntity entity)197 {198 SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);199 sqlBuilder.BeginUpdate(this.CurrentTableName);200 this.SetObject(sqlBuilder, entity);201 sqlBuilder.SetWhere(this.PrimaryKey, entity.Id);202 return sqlBuilder.EndUpdate();203 }204 205 // 这个是声明扩展方法206 partial void SetObjectExpand(SQLBuilder sqlBuilder, BaseUserLogonExtendEntity entity);207 208 ///
209 /// 设置实体210 /// 211 ///
实体212 private void SetObject(SQLBuilder sqlBuilder, BaseUserLogonExtendEntity entity)213 {214 SetObjectExpand(sqlBuilder, entity);215 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldEmailRemind, entity.EmailRemind);216 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldQrCodeLogon, entity.QrCodeLogon);217 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldJixinRemind, entity.JixinRemind);218 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldWechatRemind, entity.WechatRemind);219 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldDynamicCodeLogon, entity.DynamicCodeLogon);220 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldMobileRemind, entity.MobileRemind);221 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldUsernamePasswordLogon, entity.UsernamePasswordLogon);222 }223 224 ///
225 /// 删除实体226 /// 227 ///
主键228 ///
影响行数
229 public int Delete(string id)230 {231 return this.Delete(new KeyValuePair
(this.PrimaryKey, id));232 }233 }234 }

 

从生成的代码可以看出,完全符合.net的命名规范,爽心悦目~~,注意一点的就是要遵循规范命名表和字段,自动生成的代码具有以下优点:

 1:能支持多种数据库的。

2:能生成备注。
3:能经得起大并发数据的考验。
4:生成的代码优美,符合.NET命名习惯。

后续的MVC的代码生成将会遵循以上规则,开发人员有了这个神器,就可以安心写业务层代码了,自己省时省力,为公司也节约资源,好处多多~~~

减少不必要的浪费,用更多的时间关注更重要的事。

 

转载地址:http://wqivo.baihongyu.com/

你可能感兴趣的文章
-Linux下的虚拟机安装与管理
查看>>
Servlet和JSP之有关Servlet和JSP的梳理(二)
查看>>
linux下如何制作ext4文件系统镜像
查看>>
GPIO_Mode
查看>>
iOS总结一
查看>>
SimpleDateFormat使用详解
查看>>
如何获取DedeCms相关文章的代码
查看>>
采访~
查看>>
我对接口测试的理解
查看>>
软件工程师能力自我评价
查看>>
Ubuntu下go语言开发环境搭建(GoEclipse配置)
查看>>
获取CPU系列号,硬盘系
查看>>
【Spark 深入学习 02】- 我是一个凶残的spark
查看>>
1.4 配置备份策略(Policy)
查看>>
STL - 常用顺序容器代码
查看>>
黑马程序员—10-宏定义、枚举、typedef
查看>>
[转] 驱动模拟键盘鼠标
查看>>
Makefile 常用函数表
查看>>
狐狸和兔子
查看>>
世界卫生组织十大垃圾食物
查看>>