February 3, 2011

Part 6: The Java POJO model

If you've been following IdleWorx's Tutorial on Building a Web Application with MyBatis,MySQL,Tomcat and Eclipse , you should be now ready to setup the database layer of the ModelingAgency webapp.

In order to be able to interact with the database we setup in Part 2 we will create a simple java model made of POJOs (simple JavaBeans that is).

These Plain Old Java Objects (POJOs) will serve as the model, or in other words, a way to transfer data between the database and the ModelingAgency webapp.

At this point we're not concerned about how we'll actually retrieve the data from the database into our model classes, that is where MyBatis will come in later on and take care of that for us.

If you remember our database layout from Part 2, we have 3 business objects in our application that we need to model:

  • Client
  • Model
  • Booking

So let's add the following classes to the com.modelingagency.objects package:

Client.java


package com.modelingagency.objects;

/**
 * Copyright 2011: http://blog.idleworx.com, http://www.idleworx.com   
 */
public class Client {
 private Integer id;
 private String name;
 
 public Client(){}
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "[client] " + "(" + id + ")" + " " + name;
 }
}

Model.java

package com.modelingagency.objects;

/**
 * Copyright 2011: http://blog.idleworx.com, http://www.idleworx.com   
 */
public class Model {

 private Integer id;
 private String name;
 
 public Model() {}
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "[model] " + "(" + id + ")" + " " + name;
 }
}

Booking.java

package com.modelingagency.objects;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Copyright 2011: http://blog.idleworx.com, http://www.idleworx.com   
 */
public class Booking {

 private Integer id;
 private Client client;
 private Model model;
 private Timestamp time;
 private String location;
 
 public Booking(){}
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public Client getClient() {
  return client;
 }
 public void setClient(Client client) {
  this.client = client;
 }
 public Model getModel() {
  return model;
 }
 public void setModel(Model model) {
  this.model = model;
 }
 public Timestamp getTime() {
  return time;
 }
 public void setTime(Timestamp time) {
  this.time = time;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 }
 
 @Override
 public String toString() {
  return "[booking] " + "(" + id + ")" + " where: " + location + " when: " + new Date(time.getTime());
 }
 
}

Note that the Booking model class doesn't have getters for the booking_client_id and booking_model_id fields which are part of the booking table.

We're not going to model each field directly into our java beans, instead we're going to let MyBatis retrieve the actual Client and Model objects whenever a Booking object is returned from the database. And we're going to accomplish this through the use of MyBatis Result Maps, as you will shortly see.

Before we do that however, we need to setup MyBatis.

Part 7: Configuring MyBatis