1. 程式人生 > >mysql 表映射為java bean 手動生成。

mysql 表映射為java bean 手動生成。

uml 語句 pan ble bat 創建 不用 create 結果

---恢復內容開始---

在日常工作中,一般是先建表。後建類。
當然也有先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



---恢復內容結束---

mysql 表映射為java bean 手動生成。