Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 9 ■ Adding Persistence to Your Spring Application public void setDesc(String desc) { this.desc = desc; } public String getExtension() { return extension; } public void setExtension(String extension) { this.extension = extension; } } public String toString(){ StringBuilder builder = new StringBuilder("Type("); builder.append("id: "); builder.append(typeId); builder.append("name: "); builder.append(name); builder.append(", description: "); builder.append(desc); builder.append(", extension: "); builder.append(extension); builder.append(")"); return builder.toString(); } Listing 9-2 shows the Type class. This class has some properties like the typeId that will be the reference for the relationship in the Document class. Also, you added the name, description, and extension properties. Let’s review Listing 9-3, which shows the User class in action. Listing 9-3. User.java package com.apress.isf.java.model; import java.util.ArrayList; import java.util.List; public class User { private String userId; private String email; private String password; private String name; List documents = new ArrayList(); public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } 115

Chapter 9 ■ Adding Persistence to Your Spring Application } 116 public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getDocuments() { return documents; } public void setDocuments(List documents) { this.documents = documents; } Listing 9-3 shows the User class, which has several properties, such as the userId that will be part of the relationship of the document. You also added a list of documents. For purposes of the application and just for this chapter, these relationships will be enough to show how to use persistence in My Documents. In later chapters, you are going to use a different mechanism of persistence. Remember the DocumentDAO interface and its implementation? It contained some examples of only the implementation of the interface methods, and in another example it had a storage method that provided all the data. Now you are going to modify it in order to use JDBC; see Listing 9-4. Listing 9-4. DocumentRepository.java package com.apress.isf.spring.data; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List;

Chapter 9 ■ Adding Persistence to Your <strong>Spring</strong> Application<br />

public void setDesc(String desc) {<br />

this.desc = desc;<br />

}<br />

public String getExtension() {<br />

return extension;<br />

}<br />

public void setExtension(String extension) {<br />

this.extension = extension;<br />

}<br />

}<br />

public String toString(){<br />

StringBuilder builder = new StringBuilder("Type(");<br />

builder.append("id: ");<br />

builder.append(typeId);<br />

builder.append("name: ");<br />

builder.append(name);<br />

builder.append(", description: ");<br />

builder.append(desc);<br />

builder.append(", extension: ");<br />

builder.append(extension);<br />

builder.append(")");<br />

return builder.toString();<br />

}<br />

Listing 9-2 shows the Type class. This class has some properties like the typeId that will be the reference for the<br />

relationship in the Document class. Also, you added the name, description, and extension properties. Let’s review<br />

Listing 9-3, which shows the User class in action.<br />

Listing 9-3. User.java<br />

package com.apress.isf.java.model;<br />

import java.util.ArrayList;<br />

import java.util.List;<br />

public class User {<br />

private String userId;<br />

private String email;<br />

private String password;<br />

private String name;<br />

List documents = new ArrayList();<br />

public String getUserId() {<br />

return userId;<br />

}<br />

public void setUserId(String userId) {<br />

this.userId = userId;<br />

}<br />

115

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!