Modeling Data with POJOs4:26 with Chris Ramacciotti
In Java, a POJO is a Plain Old Java Object. In this video we discuss POJOs and our use of them to model our underlying GIF data.
[SOUND] You've been introduced to the Spring framework and 0:00 you've even created a Web app that serves Web content. 0:04 That's awesome. 0:09 What I'd like to do now is start looking at one of the most subtle, 0:12 but powerful features of Spring and that is its 0:15 ability to let you use plain old Java objects to control your application. 0:18 What are these Plain Old Java Objects or POJOs for short? 0:23 A POJO is a Java object whose class is coded for 0:28 its natural functionality and not for the framework which it will be used in. 0:31 That means that the class is coded with fields, constructors, getters and 0:36 setters, and methods that are useful to the object 0:40 as it fits in with the other classes of your application. 0:44 Not because of how it fits in with the framework you've chosen, such as Spring. 0:47 We're going to see this most clearly in the way we store our GIF data 0:53 into objects. 0:56 What we'll do is write the code that models or represents a GIF. 0:58 If this phrasing is unfamiliar to you, think of it this way. 1:02 Since we'll be storing and interacting with GIFs we should have a sense of 1:06 the kinds of we want to track about our GIFs. 1:09 We'll include the following data on our first go around. 1:12 We'll include the name which will be a String, 1:16 the date uploaded which will be a local date object, 1:18 the username of the user who uploaded the GIF which will also be a String. 1:22 And finally, whether or 1:26 not this GIF is marked as a favorite which will be a Boolean value. 1:27 The class we write to represent each GIF object is referred to as a model 1:31 because it serves as a model for the objects we'll create. 1:35 Kind of like a role model serves as an example of the traits and 1:39 behaviors we'd like to practice as human beings. 1:42 Well, see this term pop up again when we discuss the model view controller 1:46 architectural pattern later on. 1:50 Let's go to our first model together, the GIF class. 1:53 Just so that we can keep all potential models organized, 1:57 I'm going to create another package called com.teamtreehouse.giflib.model. 2:00 So I'll right-click on the giflib package. 2:05 New, Package, and I'll call it model. 2:07 That'll be a package side by side with controller here. 2:11 This is the package into which we would put all data models, whether we have 1 or 2:14 even 100. 2:19 To create the model, I'll create a Java class name Gif. 2:20 Right click, New, Java Class, Gif. 2:23 Now, even though Gif is an acronym, notice that I'm not capitalizing the I or the F. 2:26 We'll reserve the all caps for class constants. 2:32 In other words, static final fields. 2:35 So I'll click enter to create the class, 2:38 then we have our blank class ready to code. 2:39 We mentioned four fields that we'd be adding here. 2:42 Name, date uploaded, username, and favorite. 2:45 So let's create those now. 2:47 The name I will use a string for. 2:49 The date uploaded, we're going to use Java 8's LocalDate object. 2:53 dateUploaded. 2:59 The username will be another string. 3:01 And the favorite will be a boolean. 3:05 Wonderful. 3:10 We'll need getters and setters for each of these fields at this point, so 3:11 let's create them. 3:14 What I'll do is use the Cmd + End keyboard shortcut to 3:15 automatically create new methods. 3:18 So I'll arrow down to Getter and Setter, hit Enter. 3:19 Then, I'll highlight all the fields by holding Shift and 3:23 arrowing down until they're all selected. 3:26 You could also do this with the mouse. 3:28 Then I'll hit Enter, and just like that, boom, they're all generated. 3:30 Pretty sweet. 3:35 If you're using Eclipse, Spring Tool Suite, which is based on Eclipse, or 3:37 even NetBeans, they all have this option of generating getters and setters. 3:41 It's a huge time saver. 3:44 And hey, let's use another IDE shortcut to generate a constructor with all fields. 3:46 So I'll put that at the top here. 3:51 Again, I'll use Cmd+N. 3:53 Hit Enter with constructor highlighted, 3:55 then highlight the fields that I want to include in the constructor as parameters. 3:58 Again, I'll highlight all of them, and hit Enter. 4:02 And just like that, our constructor's created just as we want it. 4:05 Much faster than writing all that code manually. 4:10 And that's all there is to our model class for a Gif object. 4:14 Pretty straight forward, right? 4:17 Next, we'll look at how to use the Gif model in our controller 4:20 to pass data to our timely templates. 4:23
You need to sign up for Treehouse in order to download course files.Sign up