java中如何根据查询出来的表结构自动生成bean
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中如何根据查询出来的表结构自动生成bean相关的知识,希望对你有一定的参考价值。
我写了一个java小题目,可以输入一条sql语句就查询出其表的表结构和表里的数据,现在我想根据查出来的字段自动创建bean,希望大家给点建议。
参考技术A 这个问题可以解决你可以查出表中的字段,根据这些字段来创建一个bean类,名字可以用System.currentTimeMillis()取得唯一的,这样就可以,还有什么问题的话联系我··
ABAP表生成Java实体Bean
项目中需要将HR模块中的表数据同步到Java系统中,向外围系统提供分发与查询服务,涉及到的表有两百多张,字段好几千上万个,如果手工一张张这些ABAP表在Java系统数据库中创建一遍的话,工作量将非常大,而且容易出错。
该ABAP代码就是将需要的表转换成Java中的实体Bean,然后就可以使用Hibernate之类的持久化框架来自动生成表了。
ABAP代码没有重构过,写得有点乱......将就着看吧
REPORT ytab2javabean.
DATA: p_table TYPE string.
START-OF-SELECTION.
DATA: lv_path TYPE string.
lv_path = \'D:\\test\'.
**配置文件:以竖线分隔,第一列:SAP表名(注:SAP表名需全大写),第二列:新建表名,第三列:JavaBean名称,第四列:生成的JavaBean所在包路径
DATA: filename TYPE string VALUE \'TableCFG.txt\'.
CONCATENATE lv_path `\\` filename INTO filename.
TYPES: BEGIN OF itab,
fl1(300) TYPE c,
END OF itab.
DATA: data_tab TYPE STANDARD TABLE OF itab,
wa_tab LIKE LINE OF data_tab.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = filename
filetype = \'ASC\'
codepage = \'8400\'
CHANGING
data_tab = data_tab
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
no_authority = 6
unknown_error = 7
bad_data_format = 8
unknown_dp_error = 12
access_denied = 13
OTHERS = 17.
DATA:lt_dd03l2 TYPE TABLE OF dd03l WITH HEADER LINE.
DATA: BEGIN OF lt_dd03l OCCURS 0,
tabname TYPE dd03l-tabname,
fieldname TYPE dd03l-fieldname,
keyflag TYPE dd03l-keyflag,
rollname TYPE dd03l-rollname,
position TYPE dd03l-position,
inttype TYPE dd03l-inttype,
leng(6),
decimals(6),
ddtext TYPE dd03t-ddtext,
methodname TYPE dd03l-fieldname,
attrname TYPE dd03l-fieldname,
END OF lt_dd03l.
DATA:BEGIN OF fieldname OCCURS 0,
fieldname TYPE dd03t-fieldname ,
END OF fieldname.
DATA:BEGIN OF rollname OCCURS 0,
rollname TYPE dd04t-rollname ,
END OF rollname.
DATA:lt_dd03t TYPE TABLE OF dd03t WITH HEADER LINE,
lt_dd04t TYPE TABLE OF dd04t WITH HEADER LINE.
DATA: clsname TYPE string.
DATA: itab TYPE TABLE OF string WITH HEADER LINE.
DATA: entity TYPE string.
DATA: package TYPE string.
DATA: package_path TYPE string.
LOOP AT data_tab INTO wa_tab.
SPLIT wa_tab AT \'|\' INTO TABLE itab.
READ TABLE itab INDEX 1 .
p_table = itab.
READ TABLE itab INDEX 2 .
entity = itab.
READ TABLE itab INDEX 3 .
clsname = itab.
READ TABLE itab INDEX 4.
package = itab.
CONDENSE package.
package_path = package.
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
REPLACE `.` WITH \'\\\' INTO package_path .
CLEAR: lt_dd03l, lt_dd03l[].
SELECT dd03l~tabname dd03l~fieldname keyflag rollname position inttype leng decimals FROM dd03l
INTO CORRESPONDING FIELDS OF TABLE lt_dd03l
WHERE as4local = \'A\' AND dd03l~tabname = p_table AND dd03l~fieldname <> \'.INCLU--AP\' AND dd03l~fieldname <> \'.INCLUDE\' ORDER BY position.
SORT lt_dd03l BY position.
LOOP AT lt_dd03l .
IF lt_dd03l-rollname = \'\'.
APPEND lt_dd03l-fieldname TO fieldname.
ELSE.
APPEND lt_dd03l-rollname TO rollname.
ENDIF.
ENDLOOP.
IF fieldname[] IS NOT INITIAL.
SELECT fieldname ddtext INTO CORRESPONDING FIELDS OF TABLE lt_dd03t
FROM dd03t
FOR ALL ENTRIES IN fieldname
WHERE tabname = p_table
AND ddlanguage = \'1\'
AND fieldname = fieldname-fieldname.
ENDIF.
IF rollname[] IS NOT INITIAL.
SELECT rollname ddtext INTO CORRESPONDING FIELDS OF TABLE lt_dd04t
FROM dd04t
FOR ALL ENTRIES IN rollname
WHERE ddlanguage = \'1\'
AND rollname = rollname-rollname.
ENDIF.
DATA: i TYPE i.
FIELD-SYMBOLS <lt_dd03l> LIKE LINE OF lt_dd03l .
LOOP AT lt_dd03l ASSIGNING <lt_dd03l>.
IF <lt_dd03l>-fieldname = \'SHORT\'.
<lt_dd03l>-fieldname = \'SHORT_\'.
ENDIF.
i = <lt_dd03l>-leng.
<lt_dd03l>-leng = i.
i = <lt_dd03l>-decimals.
<lt_dd03l>-decimals = i.
<lt_dd03l>-methodname = <lt_dd03l>-fieldname.
* IF <lt_dd03l>-inttype = \'P\' .
* <lt_dd03l>-leng = <lt_dd03l>-leng * 2 + 1.
* ENDIF.
IF <lt_dd03l>-inttype = \'I\' OR <lt_dd03l>-inttype = \'X\'.
<lt_dd03l>-leng = 11.
ENDIF.
TRANSLATE <lt_dd03l>-methodname TO LOWER CASE.
CONCATENATE <lt_dd03l>-fieldname+0(1) <lt_dd03l>-methodname+1 INTO <lt_dd03l>-methodname.
<lt_dd03l>-attrname = <lt_dd03l>-fieldname.
TRANSLATE <lt_dd03l>-attrname TO LOWER CASE.
REPLACE `/` WITH \'_\' INTO <lt_dd03l>-fieldname.
REPLACE `/` WITH \'_\' INTO <lt_dd03l>-fieldname.
REPLACE `/` WITH \'_\' INTO <lt_dd03l>-fieldname.
REPLACE `/` WITH \'_\' INTO <lt_dd03l>-fieldname.
READ TABLE lt_dd03t WITH KEY fieldname = <lt_dd03l>-fieldname.
IF sy-subrc = 0.
<lt_dd03l>-ddtext = lt_dd03t-ddtext.
ELSE.
READ TABLE lt_dd04t WITH KEY rollname = <lt_dd03l>-rollname.
IF sy-subrc = 0.
<lt_dd03l>-ddtext = lt_dd04t-ddtext.
ENDIF.
ENDIF.
ENDLOOP.
* LOOP AT lt_dd03l.
* WRITE:/ lt_dd03l-fieldname,lt_dd03l-keyflag,lt_dd03l-rollname,lt_dd03l-position,lt_dd03l-inttype,lt_dd03l-leng,lt_dd03l-decimals,lt_dd03l-ddtext.
* ENDLOOP.
DATA: lt_pk TYPE TABLE OF string WITH HEADER LINE.
CLEAR: lt_pk,lt_pk[].
CONCATENATE `package ` package `;` INTO lt_pk.
APPEND lt_pk.
APPEND `import java.io.Serializable;` TO lt_pk.
APPEND `import javax.persistence.Column;` TO lt_pk.
APPEND cl_abap_char_utilities=>cr_lf TO lt_pk.
CONCATENATE `public class ` clsname `Pk implements Serializable {` INTO lt_pk.
APPEND lt_pk.
APPEND `private static final long serialVersionUID = 1L;` TO lt_pk.
APPEND `private volatile int hashCode;` TO lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE `private java.math.BigDecimal ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_pk.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE `private Integer ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_pk.
ELSE.
CONCATENATE `private String ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_pk.
ENDIF.
APPEND lt_pk.
ENDLOOP.
APPEND cl_abap_char_utilities=>cr_lf TO lt_pk.
CONCATENATE `public ` clsname `Pk() { }` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `public ` clsname `Pk(` INTO lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
IF sy-tabix = 1.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE lt_pk `java.math.BigDecimal ` lt_dd03l-attrname INTO lt_pk.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE lt_pk `Integer ` lt_dd03l-attrname INTO lt_pk.
ELSE.
CONCATENATE lt_pk `String ` lt_dd03l-attrname INTO lt_pk.
ENDIF.
ELSE.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE lt_pk `,java.math.BigDecimal ` lt_dd03l-attrname INTO lt_pk.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE lt_pk `,Integer ` lt_dd03l-attrname INTO lt_pk.
ELSE.
CONCATENATE lt_pk `,String ` lt_dd03l-attrname INTO lt_pk.
ENDIF.
ENDIF.
ENDLOOP.
CONCATENATE lt_pk `) {` INTO lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
CONCATENATE lt_pk `this.` lt_dd03l-attrname ` = ` lt_dd03l-attrname `;` INTO lt_pk.
ENDLOOP.
CONCATENATE lt_pk `}` INTO lt_pk.
APPEND lt_pk.
DATA: len TYPE string.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE `public java.math.BigDecimal get` lt_dd03l-methodname `() {` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `return ` lt_dd03l-attrname `;}` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `public ` clsname `Pk set` lt_dd03l-methodname `(java.math.BigDecimal ` lt_dd03l-attrname `) {`INTO lt_pk.
APPEND lt_pk.
CONCATENATE `this.` lt_dd03l-attrname ` = ` lt_dd03l-attrname `;return this;}` INTO lt_pk.
APPEND lt_pk.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE `@Column(name = "` lt_dd03l-fieldname `", length = ` lt_dd03l-leng `)` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `public Integer get` lt_dd03l-methodname `() {` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `return ` lt_dd03l-attrname `;}` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `public ` clsname `Pk set` lt_dd03l-methodname `(Integer ` lt_dd03l-attrname `) {`INTO lt_pk.
APPEND lt_pk.
CONCATENATE `this.` lt_dd03l-attrname ` = ` lt_dd03l-attrname `;return this;}` INTO lt_pk.
APPEND lt_pk.
ELSE.
CONCATENATE `@Column(name = "` lt_dd03l-fieldname `", length = ` lt_dd03l-leng `)` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `public String get` lt_dd03l-methodname `() {` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `return ` lt_dd03l-attrname `;}` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `public ` clsname `Pk set` lt_dd03l-methodname `(String ` lt_dd03l-attrname `) {`INTO lt_pk.
APPEND lt_pk.
CONCATENATE `this.` lt_dd03l-attrname ` = ` lt_dd03l-attrname `;return this;}` INTO lt_pk.
APPEND lt_pk.
ENDIF.
ENDLOOP.
APPEND `@Override` TO lt_pk.
APPEND `public int hashCode() {` TO lt_pk.
APPEND `int result = hashCode;` TO lt_pk.
APPEND `if (result == 0) {` TO lt_pk.
APPEND `result = 17;` TO lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
CONCATENATE `result = 31 * result + ((` lt_dd03l-attrname ` == null) ? 0 : ` lt_dd03l-attrname `.hashCode());` INTO lt_pk.
APPEND lt_pk.
ENDLOOP.
APPEND `hashCode = result;}return result;}` TO lt_pk.
APPEND `@Override` TO lt_pk.
APPEND `public boolean equals(Object o) {if (o == this) {return true;}` TO lt_pk.
CONCATENATE `if (!(o instanceof ` clsname `Pk)) {return false;}` INTO lt_pk.
APPEND lt_pk.
CONCATENATE clsname `Pk other = (` clsname `Pk) o;` INTO lt_pk.
APPEND lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
CONCATENATE `if (` lt_dd03l-attrname ` == null && other.` lt_dd03l-attrname ` != null) {return false;` INTO lt_pk.
APPEND lt_pk.
CONCATENATE `} else if (` lt_dd03l-attrname ` != null &&` ` !` lt_dd03l-attrname `.equals(other.` lt_dd03l-attrname `)) {return false;}` INTO lt_pk.
APPEND lt_pk.
ENDLOOP.
* LOOP AT lt_dd03l WHERE keyflag = \'X\'.
* IF sy-tabix = 1.
* CONCATENATE `return this.` lt_dd03l-attrname `.equals(other.` lt_dd03l-attrname `)` INTO lt_pk.
* APPEND lt_pk.
* ELSE.
* CONCATENATE `&& this.` lt_dd03l-attrname `.equals(other.` lt_dd03l-attrname `)` INTO lt_pk.
* APPEND lt_pk.
* ENDIF.
* ENDLOOP.
APPEND `return true;}` TO lt_pk.
APPEND `@Override` TO lt_pk.
APPEND `public String toString() {` TO lt_pk.
CONCATENATE `return "` clsname `Pk [` INTO lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
IF sy-tabix = 1.
CONCATENATE lt_pk lt_dd03l-attrname `=" + ` lt_dd03l-attrname INTO lt_pk.
ELSE.
CONCATENATE lt_pk `+ ", ` lt_dd03l-attrname `=" + ` lt_dd03l-attrname INTO lt_pk.
ENDIF.
ENDLOOP.
CONCATENATE lt_pk `+ "]";}}` INTO lt_pk.
APPEND lt_pk.
**生成的Java实体Bean主键类
CONCATENATE lv_path `\\Bean\\` package_path `\\` clsname `Pk.java` INTO filename.
CALL FUNCTION \'GUI_DOWNLOAD\'
EXPORTING
filename = filename
codepage = \'4110\'
TABLES
data_tab = lt_pk[].
"================================================================================================
DATA: lt_class TYPE TABLE OF string WITH HEADER LINE.
CLEAR: lt_class,lt_class[].
CONCATENATE `package ` package `;` INTO lt_class.
APPEND lt_class.
APPEND `import com.suning.hrqz.entity.BaseEntity;` TO lt_class.
APPEND `import javax.persistence.Column;` TO lt_class.
APPEND `import javax.persistence.Entity;` TO lt_class.
APPEND `import javax.persistence.Id;` TO lt_class.
APPEND `import javax.persistence.IdClass;` TO lt_class.
APPEND `import javax.persistence.Table;` TO lt_class.
APPEND `import javax.persistence.Transient;` TO lt_class.
CONCATENATE `//对应 SAP 表名:` p_table INTO lt_class.
APPEND lt_class.
CONCATENATE `@Entity(name = "` entity `")` INTO lt_class.
APPEND lt_class.
APPEND `@Table(` TO lt_class.
APPEND `// uniqueConstraints = { @UniqueConstraint(name="Unique_Index",columnNames = {"xx","xxx"}) }` TO lt_class.
APPEND `// ,indexes={@Index(name = "index01",columnList="xx,xxx")}` TO lt_class.
APPEND `)` TO lt_class.
CONCATENATE `@IdClass(value = ` clsname `Pk.class)` INTO lt_class.
APPEND lt_class.
CONCATENATE `public class ` clsname ` extends BaseEntity {` INTO lt_class.
APPEND lt_class.
APPEND `@Transient` TO lt_class.
APPEND `private static final long serialVersionUID = 1L;` TO lt_class.
APPEND `@Transient` TO lt_class.
CONCATENATE `private ` clsname `Pk pk;` INTO lt_class.
APPEND lt_class.
APPEND cl_abap_char_utilities=>cr_lf TO lt_pk.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE `private java.math.BigDecimal ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_class.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE `private Integer ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_class.
ELSE.
CONCATENATE `private String ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_class.
ENDIF.
APPEND lt_class.
ENDLOOP.
APPEND cl_abap_char_utilities=>cr_lf TO lt_pk.
LOOP AT lt_dd03l WHERE keyflag <> \'X\'.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE `private java.math.BigDecimal ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_class.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE `private Integer ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_class.
ELSE.
CONCATENATE `private String ` lt_dd03l-attrname `;//` lt_dd03l-ddtext INTO lt_class.
ENDIF.
APPEND lt_class.
ENDLOOP.
CONCATENATE `public ` clsname `() {` INTO lt_class.
APPEND lt_class.
CONCATENATE `pk = new ` clsname `Pk();}` INTO lt_class.
APPEND lt_class.
CONCATENATE `public ` clsname `(` INTO lt_class.
LOOP AT lt_dd03l WHERE keyflag = \'X\'.
IF sy-tabix = 1.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE lt_class `java.math.BigDecimal ` lt_dd03l-attrname INTO lt_class.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE lt_class `Integer ` lt_dd03l-attrname INTO lt_class.
ELSE.
CONCATENATE lt_class `String ` lt_dd03l-attrname INTO lt_class.
ENDIF.
ELSE.
IF lt_dd03l-inttype = \'P\'.
CONCATENATE lt_class `,java.math.BigDecimal ` lt_dd03l-attrname INTO lt_class.
ELSEIF lt_dd03l-inttype = \'I\' OR lt_dd03l-inttype = \'X\'.
CONCATENATE lt_class `,Integer ` lt_dd03l-attrname INTO lt_class.
ELSE.
CONCATENATE lt_class `,String ` lt_dd03l-attrname INTO lt_class.
ENDIF.
ENDIF.
ENDLOOP以上是关于java中如何根据查询出来的表结构自动生成bean的主要内容,如果未能解决你的问题,请参考以下文章