java 建表 框架_自创Java精简建表框架

对于需要快速开发的小型项目,开发前期数据库表可能会经常改动;

由于懒于写SQL,自己就弄了个Java精简建表框架。有多精简?代码只有200行!只有一个类。复制粘贴就能使用。

当然这个框架还不完整,只能用来建一些结构稍简单的表。

以下是框架的核心也是唯一一个类的代码:

public class TB {

//表前缀

private static final String TB_PREFIX = "TS_";

//表名称

private String tb_name;

/**

* 以下是建表元素

*/

public static final String PK_SIGN = "PRIMARY KEY";

public static final String PK_AUTO = "PRIMARY KEY AUTO_INCREMENT";

public static final String NUL = "NULL";

public static final String NO_NUL = "NOT NULL";

public static final String UNIQ = "UNIQUE";

public static final String UNIQ_NO_NUL = "NOT NULL UNIQUE";

public static final String FK_NO_NUL = "FOREIGN KEY(@CN) REFERENCES @OTHER";

public static final String FK_NUL = "FOREIGN KEY(@CN) REFERENCES @OTHER NULL";

private static final String CREATE_TB_TEMPLATE = "CREATE TABLE @TB (";

private static final String DROP_TB_TEMPLATE = "DROP TABLE IF EXISTS @TB";

//最后生成的sql语言

private String sql = "";

/**

* long类型,长度14

*/

public static final long TIME = 14L;

/**

* int 类型,长度为1

*/

public static final int BOOL = 1;

/**

* 构造方法

* @param tb_name 表名

*/

public TB(String tb_name) {

this.tb_name = TB_PREFIX + tb_name;

// init sql

sql = CREATE_TB_TEMPLATE.replace("@TB", this.tb_name) + "\n";

}

/**

* 生成指定size的VARCHAR类型列

*/

public static String CHAR(int size){

return size+"";

}

/**

* 生成double类型的列

*/

public static double FLOAT = 1.1;

/**

* 生成主键列(自增)

*/

public TB pa(String name, Object val){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

sql = sql + temp + " "+PK_AUTO+",\n";

}

return this;

}

/**

* 生成主键列(自定义赋值)

*/

public TB ps(String name, Object val){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

sql = sql + temp + " "+PK_SIGN+",\n";

}

return this;

}

/**

* 生成一个可为null的列

*/

public TB ll(String name, Object val){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

sql = sql + temp + " "+NUL+",\n";

}

return this;

}

/**

* 生成一个not null列

*/

public TB nn(String name, Object val){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

sql = sql + temp + " "+NO_NUL+",\n";

}

return this;

}

/**

* 生成一个unique且可为null的列

*/

public TB ul(String name, Object val){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

sql = sql + temp + " "+UNIQ+",\n";

}

return this;

}

/**

* 生成一个unique且not null的列

*/

public TB un(String name, Object val){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

sql = sql + temp + " "+UNIQ_NO_NUL+",\n";

}

return this;

}

/**

* 生成一个可为null的外键列

*/

public TB fl(String name, Object val, String others){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

String col_type = FK_NUL;

col_type = col_type.replace("@CN", name).replace("@OTHER", others);

sql = sql + temp + " "+col_type+",\n";

}

return this;

}

/**

* 生成一个not null的外键列

*/

public TB fn(String name, Object val, String others){

String temp = createLineSql(name, val);

if (!temp.equals("")) {

String col_type = FK_NO_NUL;

col_type = col_type.replace("@CN", name).replace("@OTHER", others);

sql = sql + temp + " "+col_type+",\n";

}

return this;

}

/**

* 给表增加一个列

* @param col_name 列名

* @param col_type 列类型

* @param val 值

* @param others 外键属性

*/

public TB add(String col_name, String col_type, Object val, String others) {

String temp = createLineSql(col_name, val);

if (!temp.equals("")) {

if(col_type==null){

col_type = NUL;

}

if(col_type.equals(FK_NO_NUL) || col_type.equals(FK_NUL)){

col_type = col_type.replace("@CN", col_name).replace("@OTHER", others);

}

sql = sql + temp + " "+col_type+",\n";

}

return this;

}

/**

* 提交

* @param dropTable 是否删除表

*/

public void commit(boolean dropTable){

if(dropTable){

drop();

}

sql =sql+");";

sql = sql.replace(",\n)", ")");

sql = sql.toLowerCase();

System.out.println(sql);

}

private String createLineSql(String col_name, Object val) {

String temp = "";

if (val == null) {

return temp;

}

// val is double

if (val instanceof Double) {

temp = col_name + " double";

} else if (val instanceof Long) {

temp = col_name + " bigint(" + val + ")";

} else if (val instanceof Integer) {

temp = col_name + " int(" + val + ")";

} else if (val instanceof String) {

temp = col_name + " varchar(" + val + ")";

}

return temp;

}

public void drop() {

String sql = DROP_TB_TEMPLATE.replace("@TB", tb_name) + ";";

sql = sql.toLowerCase();

System.out.println(sql);

}

public static TB create(String tb_name) {

return new TB(tb_name);

}

}

下面是其使用方法(最简单的main方法调用):

public static void main(String[] args) {

TB tb = TB("member");

tb.pa("id", TB.TIME);

tb.nn("name",TB.CHAR(10));

tb.nn("birth", TB.TIME);

tb.nn("sex", TB.BOOL);

tb.nn("create_date", TB.TIME);

tb.fn("type_id", TB.TIME, "ts_type(id)");

tb.commit(true);

}

运行main方法,最终生成的SQL语句:

drop table if exists ts_member;

create table ts_member (

id bigint(14) primary key auto_increment,

name varchar(10) not null,

birth bigint(14) not null,

sex int(1) not null,

create_date bigint(14) not null,

type_id bigint(14) foreign key(type_id) references ts_type(id));

将生成的语句复制到各种SQL IDE运行即可~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值