zoukankan      html  css  js  c++  java
  • mysql 表映射为java bean 手动生成。

    在日常工作中,一般是先建表。后建类。
    当然也有先UML构建类与类的层级关系,直接生成表。(建模)

    这里只针对先有表后有类的情况。不采用代码生成器的情况。

    例如:
    原表结构:


    假如这是我业务变动,添加的一个表,
    同时我也要建对应的java类时。
    一般我们是手动去复制。容易出错。而且也是体力活。这里面可以用SQL直接生成。

    -- 创建存储 通过系统表解析出表的结构。然后把表结构拼接成java类。

    CREATE
    PROCEDURE mypro(in tablename varchar(10),in dbo varchar(20)) BEGIN declare colName varchar(100); declare dataType varchar(100); declare done int ; declare temp varchar(100) default ''; declare `set` varchar(100) default ''; declare `get` varchar(100) default ''; declare resultTable cursor for select distinct column_name as name,data_type as type from information_schema.COLUMNS where table_name = tableName and table_SCHEMA = dbo; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; DROP TABLE IF EXISTS temp; create table temp( `data` varchar(100), `set` varchar(100), `get` varchar(100) )ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; open resultTable; posLoop:LOOP FETCH resultTable into colName,dataType; IF done=1 THEN LEAVE posLoop; END IF; IF dataType='bigint' then set temp = concat('private Integer ',colName,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','Integer ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public Integer get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='varchar' then set temp = concat('private String ',colName, ';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','String ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public String get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='int' then set temp = concat('private Integer ',colName ,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','Integer ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public Integer get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='date' then set temp = concat('private Date ',colName ,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','Date ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public Date get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='datetime' then set temp = concat('private Date ',colName ,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','Date ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public Date get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='decimal' then set temp = concat('private BigDecimal ',colName ,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','BigDecimal ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public BigDecimal get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='char' then set temp = concat('private String ',colName ,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','String ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public String get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); ELSEIF dataType='text' then set temp = concat('private String ',colName ,';'); set `set` = concat('public void set',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(','String ',colName,'){ this.',colName,'=',colName,';','}'); set `get` = concat('public String get',UPPER(LEFT(colName,1)),RIGHT(colName,length(colName)-1),'(){ return ',colName,';','}'); END IF ; insert into temp(`data`,`set`,`get`) values(temp,`set`,`get`); END LOOP posLoop; CLOSE resultTable; END; call mypro('person','test'); //第一个参数是表名,针对表名生成java类结构。第二个参数是dbo指定的哪个数据库 select * from temp;
    效果图:

     

    实际应用:

    直接复制就行了。

    结果:

    完全正常, 这样就不用一个个去手动建java类了。

    里面的toString() 跟类名 没有去自动生动。手动写一下就OK。这样就方便好了。

    MYsql不支持print打印 只能select 所以就插入到表中了。

    如果用mssql 就直接print 配合 CHAR(10),CHAR(13) 能直接生成一个完整的java bean 包括 toString() 方法 类名。mysql有一定的局限性。

     

    如果这里是采用mybaits的话。
    手动去复制表结构是很累人的。也容易出错。mybatis insert into tb(col1,col2) value(#{value1},#{value2});

     这里面如果也需要手动复制的话。也有相应的SQL语句,参见,另一篇博客:https://www.cnblogs.com/1-Admin/p/8447052.html

    END

    
    
  • 相关阅读:
    查windows系统开关机记录
    HDU-6278-Jsut$h$-index(主席树)
    POJ-2104-Kth Number(主席树)
    HDU-6546-Function(贪心)
    POJ-1523-SPF(求割点)
    POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)
    POJ-2552-The Bottom of a Graph 强连通分量
    POJ-1659-Frogs' Neighborhood
    POJ-1904-King‘s Quest
    POJ-1236-Network of Schools
  • 原文地址:https://www.cnblogs.com/1-Admin/p/9651507.html
Copyright © 2011-2022 走看看