Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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;
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73 and 74: Chapter 6 ■ Using Resource Files
- Page 75 and 76: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125 and 126: Chapter 9 ■ Adding Persistence to
- Page 127 and 128: Chapter 9 ■ Adding Persistence to
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
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