【我的小工具】一个表和表生成的代码

表:习惯用前三个字符来表示字段的类型。有了这个表的结构信息,可以生成很多代码。

类:

VB.Net

vbnet 复制代码
Namespace NF
	Public Class User
		Inherits NF.BaseEntity

		Public Const TableName As String = "tblUser"

		Public Shared Function FindOne(ByVal intID As Integer) As NF.User
			Dim this As New NF.User
			this.ID=intID
			this.Fill()
			Return this
		End Function

#Region "--Propertys--"

		Public Property ID As Integer = 0
		Public Property Name As String = ""
		Public Property BirthdayDate As Date = ComMod.MinDate
		Public Property Age As Integer = 0
		Public Property Money As Decimal = 0
		Public Property Remarks As String = ""

#End Region

#Region "--Methods--"

		Private Sub boxEntity(row As DataRow, this As NF.User)
			With this
				.ID = IIf(row.IsNull(TBC.intID), 0, row.Item(TBC.intID))
				.Name = IIf(row.IsNull(TBC.strName), "", row.Item(TBC.strName))
				.BirthdayDate = IIf(row.IsNull(TBC.dtmBirthday), ComMod.MinDate, row.Item(TBC.dtmBirthday))
				.Age = IIf(row.IsNull(TBC.intAge), 0, row.Item(TBC.intAge))
				.Money = IIf(row.IsNull(TBC.decMoney), 0, row.Item(TBC.decMoney))
				.Remarks = IIf(row.IsNull(TBC.strRemarks), "", row.Item(TBC.strRemarks))
				.EntityState = eEntityState.NO_CHANGE
			End With
		End Sub

		Public Overrides Function Fill(Optional ByVal dt As Data.DataTable = Nothing) As Collection
			MyBase.Fill()
			Dim cols As New Collection
			Dim blnFromDB As Boolean = False
			Dim this As NF.User
			If dt Is Nothing Then
				Using cmd As Data.IDbCommand = DBHelper.CreateCommand()
					cmd.CommandText = "Select * From tblUser Where intID=@intID"
					DBHelper.AddCmdParameter(cmd, TBC.intID, ID)
					dt = DBHelper.GetDataTable(cmd)
				End Using
				blnFromDB = True
			End If
			For Each row As Data.DataRow In dt.Rows
				If blnFromDB Then
					this = Me
				Else
					this = New NF.User
				End If
				boxEntity(row, this)
				cols.Add(this, this.GetHashCode.ToString)
			Next
			Return cols
		End Function

		Public Function ToList(ByVal tbl As DataTable) As IEnumerable(Of NF.BaseEntity)
			Dim mList As New List(Of NF.User)
			If tbl IsNot Nothing Then
				For Each row As Data.DataRow In tbl.Rows
					Dim this As New NF.User
					boxEntity(row, this)
					mList.Add(this)
				Next
			End If
			Return mList
		End Function

		Public Overrides Function Save() As Integer
			MyBase.Save()
			Dim cmd As Data.IDbCommand = DBHelper.CreateCommand()
			Select Case Me.EntityState
				Case eEntityState.NEW_RECORD
					cmd.CommandText = "Insert into tblUser (strName,dtmBirthday,intAge,decMoney,strRemarks) Values (@strName,@dtmBirthday,@intAge,@decMoney,@strRemarks)"
					DBHelper.AddCmdParameter(cmd, TBC.strName, Name)
					DBHelper.AddCmdParameter(cmd, TBC.dtmBirthday, BirthdayDate)
					DBHelper.AddCmdParameter(cmd, TBC.intAge, Age)
					DBHelper.AddCmdParameter(cmd, TBC.decMoney, Money)
					DBHelper.AddCmdParameter(cmd, TBC.strRemarks, Remarks)
					DBHelper.ExecuteCommand(cmd)
					ID = DBHelper.GetDBIdentity("tblUser", TBC.intID)
					Me.EntityState = eEntityState.NO_CHANGE

				Case eEntityState.CHANGED
					cmd.CommandText = "Update tblUser Set strName=@strName,dtmBirthday=@dtmBirthday,intAge=@intAge,decMoney=@decMoney,strRemarks=@strRemarks Where intID=@intID"
					DBHelper.AddCmdParameter(cmd, TBC.strName, Name)
					DBHelper.AddCmdParameter(cmd, TBC.dtmBirthday, BirthdayDate)
					DBHelper.AddCmdParameter(cmd, TBC.intAge, Age)
					DBHelper.AddCmdParameter(cmd, TBC.decMoney, Money)
					DBHelper.AddCmdParameter(cmd, TBC.strRemarks, Remarks)
					DBHelper.AddCmdParameter(cmd, TBC.intID, ID)
					DBHelper.ExecuteCommand(cmd)
					Me.EntityState = eEntityState.NO_CHANGE

				Case eEntityState.DELETE
					cmd.CommandText = "Delete From tblUser Where intID=@intID"
					DBHelper.AddCmdParameter(cmd, TBC.intID, ID)
					DBHelper.ExecuteCommand(cmd)

			End Select

			cmd.Dispose()
			Return 0
		End Function

#End Region

	End Class

End Namespace

安卓Java《SQLite Bean》:

java 复制代码
package com.NF.Beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.NF.BaseBean.BaseEntity;
import com.NF.BaseBean.eEntityState;
import com.NF.utils.DateTools;
import android.content.ContentValues;
import android.database.Cursor;

public class User extends BaseEntity {

	public static final String tblName = "tblUser";

	private int ID = 0;
	private String Name = "";
	private Date BirthdayDate = ComData.MinDate;
	private int Age = 0;
	private BigDecimal Money = new BigDecimal(0);
	private String Remarks = "";

	public User() {
		super();
	}
	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		if (ID != iD) {
			ID = iD;
			super.RecordChanged();
		}
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		if (!Name.equalsIgnoreCase(name)) {
			Name = name;
			super.RecordChanged();
		}
	}

	public Date getBirthdayDate() {
		return BirthdayDate;
	}

	public void setBirthdayDate(Date birthdayDate) {
		if (null != birthdayDate && BirthdayDate.getTime() != birthdayDate.getTime()) {
			BirthdayDate = birthdayDate;
			super.RecordChanged();
		}
	}

	public int getAge() {
		return Age;
	}

	public void setAge(int age) {
		if (Age != age) {
			Age = age;
			super.RecordChanged();
		}
	}

	public BigDecimal getMoney() {
		return Money;
	}

	public void setMoney(BigDecimal money) {
		if (!Money.equals(money)) {
			Money = money;
			super.RecordChanged();
		}
	}

	public String getRemarks() {
		return Remarks;
	}

	public void setRemarks(String remarks) {
		if (!Remarks.equalsIgnoreCase(remarks)) {
			Remarks = remarks;
			super.RecordChanged();
		}
	}

	private ContentValues getContentValues() {
		ContentValues cvs = new ContentValues();
		cvs.put(TBC.intID, this.getID());
		cvs.put(TBC.strName, this.getName());
		cvs.put(TBC.dtmBirthday, DateTools.format(this.getBirthdayDate()));
		cvs.put(TBC.intAge, this.getAge());
		cvs.put(TBC.decMoney, this.getMoney());
		cvs.put(TBC.strRemarks, this.getRemarks());
		return cvs;
	}

	private User getBean(Cursor cursor) {
		User bean = new User();
		bean.setID(cursor.getInt(cursor.getColumnIndex(TBC.intID)));
		bean.setName(cursor.getString(cursor.getColumnIndex(TBC.strName)));
		bean.setBirthdayDate(DateTools.parse(cursor.getString(cursor.getColumnIndex(TBC.dtmBirthday))));
		bean.setAge(cursor.getInt(cursor.getColumnIndex(TBC.intAge)));
		bean.setRemarks(cursor.getString(cursor.getColumnIndex(TBC.strRemarks)));
		bean.setEntityState(eEntityState.NO_CHANGE);
		return bean;
	}

	@Override
	public void save() {
		ContentValues cvs;
		switch (super.getEntityState()) {
		case NEW_RECORD:
			//--注意返回ID,插入时候Key列可以省略等,索引序号增加,可以先赋值新值。
			cvs = getContentValues();
			cvs.remove(TBC.intID);
			ComData.db.insert(tblName, cvs);
			ID = ComData.db.getDBIdentity(tblName);
			super.setEntityState(eEntityState.NO_CHANGE);
			break;
		case CHANGED:
			cvs = getContentValues();
			ComData.db.update(tblName, cvs, TBC.intID + "=?", new String[] { String.valueOf(this.getID()) });
			super.setEntityState(eEntityState.NO_CHANGE);
			break;
		case DELETE:
			ComData.db.delete(tblName, TBC.intID + "=?", new String[] { String.valueOf(this.getID()) });
			break;
		default:
			break;
		}
	}

	public List<User> getAll() {
		List<User> list = new ArrayList<User>();
		Cursor cursor = ComData.db.query("Select * From " + tblName);
		while (cursor.moveToNext()) {
			list.add(getBean(cursor));
		}
		cursor.close();
		return list;
	}

	public User getThis() {
		return getThis(getID());
	}

	public User getThis(int intID) {
		User bean = null;
		String sql = String.format("Select * From %s Where %s=?", tblName, TBC.intID);
		Cursor cursor = ComData.db.query(sql, new String[]{String.valueOf(intID)});
		if (cursor.moveToNext()) {
			bean = getBean(cursor);
		}
		cursor.close();
		return bean;
	}

}

Node.Js的后台处理代码:

javascript 复制代码
//--App router post tblUser--//
router.post('/User', async(req, res, next) => {
    try {
      let results={};
      switch (req.body.type) {
          case "Add":
          case "Update":
          case "Delete":
              results = await db.saveUser(req, res);
              break;
          default:
              results = await db.selectUsers(req, res);
              break;
       }
       res.send(results);
   } catch (err) {
      res.send(getRspSysError(err));
   }
});


//--Save table tblUser--//
db.saveUser = async(req, res) => {
  let sql = "";
  let prs = [];
  let ops = req.body.type + " User";
  if (req.body.type ==="Add") {
    sql = "Insert Into tblUser ( strName,dtmBirthday,intAge,decMoney,strRemarks ) Values (  ?, ?, ?, ?, ? );";
    prs.push(req.body.strName);
    prs.push(req.body.dtmBirthday);
    prs.push(req.body.intAge);
    prs.push(req.body.decMoney);
    prs.push(req.body.strRemarks);
  }else if (req.body.type ==="Update") {
    sql = "Update tblUser Set  strName=?,dtmBirthday=?,intAge=?,decMoney=?,strRemarks=?  Where intID=? ";
    prs.push(req.body.strName);
    prs.push(req.body.dtmBirthday);
    prs.push(req.body.intAge);
    prs.push(req.body.decMoney);
    prs.push(req.body.strRemarks);
    prs.push(req.body.intID);
  }else if (req.body.type ==="Delete") {
    sql = "Delete From  tblUser  Where intID=? ";
    prs.push(req.body.intID);
  }
  return new Promise((resolve, reject) => {
    connection.query(sql, prs,
      (err, results) => {
        return getResponse(err, results, resolve, ops);
      }
    );
  });
};

db.selectUsers = async(req, res) => {
  let sql= "Select * From tblUser;";
  let prs =[];
  let ops ="Get User";
  return new Promise((resolve, reject) => {
    connection.query(sql, prs,
      (err, results) => {
        return getResponse(err, results, resolve, ops);
      }
    );
  });
};

还可以生成很多代码。

相关推荐
十叶知秋18 分钟前
【jmeter】jmeter的线程组功能的详细介绍
数据库·jmeter·性能测试
瓜牛_gn2 小时前
mysql特性
数据库·mysql
奶糖趣多多3 小时前
Redis知识点
数据库·redis·缓存
CoderIsArt4 小时前
Redis的三种模式:主从模式,哨兵与集群模式
数据库·redis·缓存
师太,答应老衲吧6 小时前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
Channing Lewis7 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce
毕业设计制作和分享8 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis
ketil278 小时前
Redis - String 字符串
数据库·redis·缓存
Hsu_kk9 小时前
MySQL 批量删除海量数据的几种方法
数据库·mysql
编程学无止境9 小时前
第02章 MySQL环境搭建
数据库·mysql