
Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification; i.e. a POJO should not have to
1). Extend prespecified classes, as in
1 |
public class Foo extends javax.servlet.http.HttpServlet { ... |
2). Implement prespecified interfaces, as in
1 |
public class Bar implements javax.ejb.EntityBean { ... |
3). Contain prespecified annotations, as in
1 |
@javax.persistence.Entity public class Baz { ... |
sumber: wiki
Artikel kali ini akan melanjutkan pembahasan Apache Velocity Hello World dimana kita akan men-generate POJO. berikut judul artikelnya:
Apache Velocity Generate POJO
Berikut langkah-langkah Apache Velocity Generate POJO :
1). Karena artikel ini melanjutkan pembahasan Apache Velocity Hello World maka kita buka projek “Belajar Apache Velocity” dengan editor eclipse
2). Create file “class.vm” simpan di folder “resources”
3). Buka file “class.vm” lalu ketikkan coding dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.*; public class $class.Name { #foreach($att in $class.Attributes) private $att.Type $att.Name; #end public $utility.toString($class.Name)() { } #foreach($att in $class.Attributes) public $att.Type get$utility.firstToUpperCase($att.Name)() { return this.$att.Name; } public void set$utility.firstToUpperCase($att.Name)($att.Type $att.Name) { this.$att.Name = $att.Name; } #end } |
4). Buat Class dengan nama “GeneratorUtility”, lalu tambahkan method “firstToUpperCase(String text)” & method “toString(String text)”. terlihat seperti coding dibawah ini:
1 2 3 4 5 6 7 8 9 10 |
public String firstToUpperCase(String text) { char[] textArray = text.toCharArray(); textArray[0] = Character.toUpperCase(textArray[0]); return new String(textArray); } public String toString(String text) { return text; } |
5). Buat Class dengan nama “AttributeDescriptor”. tambahkan properti/atribut seperti coding dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private String name; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } |
6). Buat Class dengan nama “ClassDescriptor”. tambahkan properti/atribut seperti coding dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private String name; private ArrayList<AttributeDescriptor> attributes = new ArrayList<AttributeDescriptor>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<AttributeDescriptor> getAttributes() { return attributes; } public void addAttributes(AttributeDescriptor attribute) { this.attributes.add(attribute); } |
7). Buat Class dengan nama “ClassGenerator” jangan lupa saat create class centang “static void main”
8). Tambahkan coding pada method main, seperti dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public static void main(String[] args) throws Exception { ArrayList<ClassDescriptor> classes = new ArrayList<ClassDescriptor>(); ClassDescriptor classDescriptor = new ClassDescriptor(); classDescriptor.setName("Customer"); AttributeDescriptor attributeDescriptor = new AttributeDescriptor(); attributeDescriptor.setName("code"); attributeDescriptor.setType("int"); classDescriptor.addAttributes(attributeDescriptor); attributeDescriptor = new AttributeDescriptor(); attributeDescriptor.setName("description"); attributeDescriptor.setType("String"); classDescriptor.addAttributes(attributeDescriptor); classes.add(classDescriptor); classDescriptor = new ClassDescriptor(); classDescriptor.setName("Order"); attributeDescriptor = new AttributeDescriptor(); attributeDescriptor.setName("number"); attributeDescriptor.setType("int"); classDescriptor.addAttributes(attributeDescriptor); attributeDescriptor = new AttributeDescriptor(); attributeDescriptor.setName("date"); attributeDescriptor.setType("Date"); classDescriptor.addAttributes(attributeDescriptor); attributeDescriptor = new AttributeDescriptor(); attributeDescriptor.setName("customer"); attributeDescriptor.setType("Customer"); classDescriptor.addAttributes(attributeDescriptor); classes.add(classDescriptor); GeneratorUtility utility = new GeneratorUtility(); for (ClassDescriptor wClass : classes) { VelocityEngine ve = new VelocityEngine(); ve.init(); Template template = ve.getTemplate("./resources/class.vm"); FileWriter writer = new FileWriter("./src/" + wClass.getName() + ".java"); VelocityContext context = new VelocityContext(); context.put("class", wClass); context.put("utility", utility); template.merge(context, writer); writer.flush(); writer.close(); } } |
9). Run & Output
Terbentuk 2 file java:
Customer.java
1234567891011121314151617181920212223242526 import java.util.*;public class Customer {private int code;private String description;public Customer() {}public int getCode() {return this.code;}public void setCode(int code) {this.code = code;}public String getDescription() {return this.description;}public void setDescription(String description) {this.description = description;}}Order.java
12345678910111213141516171819202122232425262728293031323334 import java.util.*;public class Order {private int number;private Date date;private Customer customer;public Order() {}public int getNumber() {return this.number;}public void setNumber(int number) {this.number = number;}public Date getDate() {return this.date;}public void setDate(Date date) {this.date = date;}public Customer getCustomer() {return this.customer;}public void setCustomer(Customer customer) {this.customer = customer;}}
Semoga Membantu,
Salam berbagi,
Yulianto Wijaksana