欢迎光临
我们一直在努力

一段简单的根据SQLServer数据库表结构生成C#实体类的Java代码-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

/*

* created on 2004-10-15

*

* title: quickcoder

* description: 一段简单的根据sqlserver数据库表结构生成c#实体类的java代码

曾经在学校听殷兆粼教授这样讲过,uml将来应该能够根据已经设计好的业务逻辑,自动生成数据库表和数据间的关联等,并生成实体代码,最好能够再生成部分业务逻辑代码。这样我们的设计就跟画画一样了,我们要做的就是画uml图。数据库结构、程序代码都自动生成。

这里是我在公司的一个项目中为了把数据库实体表生成c#实体而写的一段代码。功能不多,但是可以省敲很多代码。

* company: http://www.hakatasoft.com

* author: shuqun, zhou

*

*/

package coder;

import java.io.*;

import java.sql.*;

import java.util.*;

/**

* @author administrator

*

* todo to change the template for this generated type comment go to

* window – preferences – java – code style – code templates

*/

public class quickcoder {

public static map mssqlserverdatatypecsharptypemap = new hashmap();

static {

mssqlserverdatatypecsharptypemap.put("char", "string");

mssqlserverdatatypecsharptypemap.put("varchar", "string");

mssqlserverdatatypecsharptypemap.put("nvarchar", "string");

mssqlserverdatatypecsharptypemap.put("text", "string");

mssqlserverdatatypecsharptypemap.put("datetime", "system.datetime");

mssqlserverdatatypecsharptypemap.put("int", "int");

mssqlserverdatatypecsharptypemap.put("int identity", "float");

mssqlserverdatatypecsharptypemap.put("float", "float");

mssqlserverdatatypecsharptypemap.put("bit", "string");

mssqlserverdatatypecsharptypemap.put("numeric", "double");

mssqlserverdatatypecsharptypemap.put("money", "double");

}

public static void prt(object o) {

system.out.println(o);

}

public static void prt(int i) {

system.out.println(i);

}

public static void table2csharp(resultsetmetadata rsmd, string namespace, string classname) throws ioexception, sqlexception {

stringbuffer cs = new stringbuffer();

string line = system.getproperty("line.separator");

cs.append("using system;").append(line).append(line);

cs.append("/**").append(line);

cs.append(" * generate by quickcoder.").append(line);

cs.append(" * 使用前请修改文件名和类名(去掉`auto_前缀)").append(line);

cs.append(" */").append(line).append(line);

cs.append("namespace ").append(namespace).append(" {").append(line);

cs.append("\tpublic class ").append(classname).append(" {").append(line);

int c = rsmd.getcolumncount();

string dbtype, type, name;

object t;

for (int i = 1; i <= c; i++) {

name = rsmd.getcolumnlabel(i).replace( , _);

dbtype = rsmd.getcolumntypename(i);

t = mssqlserverdatatypecsharptypemap.get(dbtype);

if (t != null) {

type = t.tostring();

}

else {

type = "____" + dbtype;

prt(namespace + "" + classname + " " + dbtype);

}

cs.append("\t\t").append(type)

.append(" m_").append(name).append(";").append(line);

/* 由于同组一工程师说这种c#的getter, setter写法不好,我就改成下面那种写法了

不过我认为他是在用java的思想审视c#,我找不到这样写的不好之处

cs.append("\t\tpublic ").append(type).append(" ").append(name).append(" ").append("{").append(line);

cs.append("\t\t\tget {").append(line);

cs.append("\t\t\t\treturn m_").append(name).append(";").append(line);

cs.append("\t\t\t}").append(line);

cs.append("\t\t\tset {").append(line);

cs.append("\t\t\t\tm_").append(name).append(" = ").append("value;").append(line);

cs.append("\t\t\t}").append(line);

cs.append("\t\t}").append(line);

*/

cs.append(line);

cs.append("\t\tpublic ").append(type).append(" get").append(name).append("() {").append(line);

cs.append("\t\t\treturn this.m_").append(name).append(";").append(line);

cs.append("\t\t}").append(line);

cs.append(line);

cs.append("\t\tpublic void set").append(name).append("(").append(type).append(" ").append(name).append(") {").append(line);

cs.append("\t\t\tthis.m_").append(name).append(" = ").append(name).append(";").append(line);

cs.append("\t\t}").append(line);

if ("int".equals(type) || "float".equals(type) || "double".equals(type)) {

cs.append(line);

cs.append("\t\tpublic string").append(" get").append(name).append("asstring() {").append(line);

cs.append("\t\t\treturn this.m_").append(name).append(".tostring();").append(line);

cs.append("\t\t}").append(line);

cs.append(line);

cs.append("\t\tpublic void set").append(name).append("(string s) {").append(line);

cs.append("\t\t\tthis.m_").append(name).append(" = ").append(type).append(".parse(s);").append(line);

cs.append("\t\t}").append(line);

}

cs.append(line);

}

cs.append("\t}").append(line);

cs.append("}").append(line);

//prt(cs);

file file = new file("c:/tmp", classname + ".cs");

fileoutputstream fos = new fileoutputstream(file);

fos.write(cs.tostring().getbytes());

fos.close();

}

public static void main(string[] args) throws exception {

prt("start");

class.forname("sun.jdbc.odbc.jdbcodbcdriver");

connection con = drivermanager.getconnection("jdbc:odbc:sce", "sce", "sce");

connection con2 = drivermanager.getconnection("jdbc:odbc:sce", "sce", "sce");

statement stmt = con2.createstatement();

databasemetadata meta = con.getmetadata();

resultset rs = meta.gettables(null, null, null, null);

string tablename, tabletype;

resultset rs2;

while (rs.next()) {

//rsmd;

//system.out.println(rsmd.getcolumncount());

tablename = rs.getstring("table_name");

tabletype = rs.getstring("table_type");

if ("table".equals(tabletype) && tablename.startswith("tb_")) {

//prt(tablename);

rs2 = stmt.executequery("select * from [" + tablename + "]");

table2csharp(rs2.getmetadata(), "sce.action.objects", "auto_o_" + tablename.substring(3, tablename.length()).replace(-, _));

rs2.close();

}

}

rs.close();

stmt.close();

con.close();

con2.close();

prt("end");

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一段简单的根据SQLServer数据库表结构生成C#实体类的Java代码-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章