MyBator: Generate column names as "public static String COLUM_NAME" ...

82 views
Skip to first unread message

Wolfgang Schele

unread,
Jan 29, 2013, 5:24:57 PM1/29/13
to mybati...@googlegroups.com
Hi,

how can I add static final fields to my class who represent the access to the getter/setter by using e.g. BeanUtils or custom Reflection...?

regards
Wolfgang



public abstract class PersonBean {

        //Database Column Names id, firstname, lastname
public static final String ID                  = "id";
public static final String FIRSTNAME  = "firstname";
public static final String LASTNAME   = "lastname";

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column person.id
     *
     * @mbggenerated Mon Jan 28 23:07:38 CET 2013
     */
    private Integer id;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column person.firstName
     *
     * @mbggenerated Mon Jan 28 23:07:38 CET 2013
     */
    private String firstname;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column person.lastName
     *
     * @mbggenerated Mon Jan 28 23:07:38 CET 2013
     */
    private String lastname;

[...]

}

Jeff Butler

unread,
Jan 30, 2013, 9:55:01 AM1/30/13
to mybati...@googlegroups.com
Here's an example for the first one...

    private void addField(TopLevelClass topLevelClass) {
        Field f = new Field();
        f.setName("ID");
        f.setType(FullyQualifiedJavaType.getStringInstance());
        f.setVisibility(JavaVisibility.PUBLIC);
        f.setFinal(true);
        f.setStatic(true);
        f.setInitializationString("\"id\";");
        
        topLevelClass.addField(f);
    }



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Wolfgang Schele

unread,
Jan 30, 2013, 6:16:51 PM1/30/13
to mybati...@googlegroups.com
/**
* Generate  public static final String ID = "id"; constants to access 
* maybe setter/getter methods e.g. via BeanUtils or Reflection.
* @param topLevelClass TopLevelClass
*/
private void executeAddConstantFields(TopLevelClass topLevelClass) {
//All fields generated representing the databse columns
List<Field> myFields = topLevelClass.getFields();
//Buffer the new fields in a List object...
List<Field> myNewFields = new ArrayList<Field>();
for (Field field : myFields) {
//Add the fields to the myNewFields list ... adding it directly via topLevelClass.addField(newfield);
//occurs in an concurrent list modification error.
myNewFields.add(addField(topLevelClass, field));
}
//Add the fields to the topLevelClass
for (Field newfield : myNewFields) {
topLevelClass.addField(newfield);
}
}
    private Field addField(TopLevelClass topLevelClass, Field field) {
   
        String upperCase = field.getName().toUpperCase();
            Field f = new Field();
            f.setName(upperCase);
            f.setType(FullyQualifiedJavaType.getStringInstance());
            f.setVisibility(JavaVisibility.PUBLIC);
            f.setFinal(true);
            f.setStatic(true);
            f.setInitializationString("\""+field.getName()+"\"");
            f.addJavaDocLine("// CONSTANT generated by MyBatorPluginAdapter.class accessing the getter/setter of the field <"+field.getName()+">");
            
            return f;

    }

....then I use it in the the method:

public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable);

Jeff, thanks a lot.







Kurth, Felix

unread,
Jan 31, 2013, 2:20:38 AM1/31/13
to mybati...@googlegroups.com

Can you please post the complete plugin? I’m interested in this as well.

 

Regards, Felix

--

Wolfgang Schele

unread,
Jan 31, 2013, 2:48:36 AM1/31/13
to mybati...@googlegroups.com
@Felix: I'm in the office now and have no access to my home server :-)

@Jeff: Is this feature not a topic for the mybator standard?

I develop Java Applications since the begining of Java ... and I'm very often confronted with this requirement. I read many articels in Java Blogs, magazines: BeanUtils, JavaFX TableView, ...etc. ... I can provide a long list.

thx
wolfgang

 

Jeff Butler

unread,
Jan 31, 2013, 9:44:37 AM1/31/13
to mybati...@googlegroups.com
I'm not sure what you mean regarding this as a common requirement.  I've never heard of doing this before.

BTW - the tool is called "MyBatis Generator", not mybator.

Jeff




 

--

Wolfgang Schele

unread,
Jan 31, 2013, 10:49:19 AM1/31/13
to mybati...@googlegroups.com
Ok, sorry..I will use MyBatis Generator in future !! Can I change the subject of my post?

Many other tools/frameworks need such fields. For example JavaFX TableView ....common BeanUtils class...etc.
When this is not possible via MyBatis Generator...I must create/maintain this field names in an other way. It would be enough when it is possible to enable it in MyBatis Generator.



    public TableView getTableView() {
        ObservableList<Person> data = FXCollections.<Person>sequence(
                new Person("Duke", "Java"),
                new Person("DukeFX", "JavaFX"));

        TableView<Person> tableView = new TableView<Person>();
        tableView.setItems(data);

        TableColumn firstNameCol = new TableColumn("First");
        firstNameCol.setProperty(Person.FIRSTNAME);

        TableColumn lastNameCol = new TableColumn("Last");
        lastNameCol.setProperty(Person.LASTNAME);

        tableView.getColumns().addAll(firstNameCol, lastNameCol);

        return tableView;
    }


    public class Person implements Bean {

public static final String FIRSTNAME  = "firstName";
public static final String LASTNAME   = "lastName";
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
 }

 //with getters and other Bean functionality
...
    }
Reply all
Reply to author
Forward
0 new messages