Skip to content

Commit 6a34c2d

Browse files
committed
Part 4 - MyBatis and SQLite
1 parent dd14031 commit 6a34c2d

File tree

14 files changed

+422
-35
lines changed

14 files changed

+422
-35
lines changed

.classpath

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" output="target/classes" path="src/main/java">
3+
<classpathentry kind="src" output="target/hellogwt/WEB-INF/classes" path="src/main/java">
44
<attributes>
55
<attribute name="optional" value="true"/>
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
9+
<classpathentry excluding="**" kind="src" output="target/hellogwt/WEB-INF/classes" path="src/main/resources">
1010
<attributes>
1111
<attribute name="maven.pomderived" value="true"/>
1212
</attributes>
@@ -17,7 +17,7 @@
1717
<attribute name="maven.pomderived" value="true"/>
1818
</attributes>
1919
</classpathentry>
20-
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
20+
<classpathentry excluding="**" kind="src" output="target/hellogwt/WEB-INF/test-classes" path="src/test/resources">
2121
<attributes>
2222
<attribute name="maven.pomderived" value="true"/>
2323
</attributes>
@@ -38,5 +38,5 @@
3838
<attribute name="owner.project.facets" value="java"/>
3939
</attributes>
4040
</classpathentry>
41-
<classpathentry kind="output" path="target/classes"/>
41+
<classpathentry kind="output" path="target/hellogwt/WEB-INF/classes"/>
4242
</classpath>

ReadMe.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
This project is a step-by-step Spring-GWT tutorial.
2-
It is inspired from the article written by Alex Tretyakov on 2011-11-25
3-
[Spring and GWT tutorial. Part 3 - GWT RPC Services](http://alextretyakov.blogspot.fr/2011/10/developing-simple-web-application-using.html).
2+
It is inspired from the article written by Alex Tretyakov on 2011-11-14
3+
[Spring and GWT tutorial. Part 4 - MyBatis and MySQL](http://alextretyakov.blogspot.fr/2011/11/developing-simple-web-application-using.html).
44

55
The project was created as a "Maven Project" in Eclipse.
6-
Then files from hellogwt were added.
6+
Then files from hellogwt were added, commented and modified to run with SQLite instead of MySQL.
77

88
The source code is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
99

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
2121
<log4j.version>1.2.16</log4j.version>
2222
<!-- Convenience property to set the GWT version -->
2323
<gwt.version>2.7.0</gwt.version>
24+
<mybatis.version>3.2.1</mybatis.version>
25+
<mybatis-spring.version>1.2.0</mybatis-spring.version>
26+
<!-- http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
27+
<sqlite-jdbc.version>3.8.7</sqlite-jdbc.version>
2428
<!-- Note: GWT needs at least java 1.6 -->
2529
<maven.compiler.source>1.7</maven.compiler.source>
2630
<maven.compiler.target>1.7</maven.compiler.target>
@@ -34,6 +38,11 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
3438
<artifactId>spring-web</artifactId>
3539
<version>${spring.version}</version>
3640
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-jdbc</artifactId>
44+
<version>${spring.version}</version>
45+
</dependency>
3746
<!-- GWT -->
3847
<dependency>
3948
<groupId>com.google.gwt</groupId>
@@ -51,6 +60,22 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
5160
<artifactId>log4j</artifactId>
5261
<version>${log4j.version}</version>
5362
</dependency>
63+
<!-- persistence -->
64+
<dependency>
65+
<groupId>org.mybatis</groupId>
66+
<artifactId>mybatis</artifactId>
67+
<version>${mybatis.version}</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.mybatis</groupId>
71+
<artifactId>mybatis-spring</artifactId>
72+
<version>${mybatis-spring.version}</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.xerial</groupId>
76+
<artifactId>sqlite-jdbc</artifactId>
77+
<version>${sqlite-jdbc.version}</version>
78+
</dependency>
5479
</dependencies>
5580

5681
<build>

src/main/java/com/hellogwt/HelloGWT.gwt.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ XML module file for the com.hellogwt package.
2222

2323
<!-- Specify the app entry point class. -->
2424
<entry-point class='com.hellogwt.client.HelloGWT'/>
25+
26+
<!-- Compile to JavaScript classes from com.hellogwt.client and com.hellogwt.shared packages. -->
27+
<source path='client'/>
28+
<source path='shared'/>
29+
30+
<!-- only compile for Firefox to speed up GWT compile time -->
31+
<set-property name="user.agent" value="gecko1_8"/>
2532
</module>

src/main/java/com/hellogwt/client/GreetingService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@
1919

2020
import com.google.gwt.user.client.rpc.RemoteService;
2121
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
22+
import com.hellogwt.shared.domain.Greeting;
23+
import java.util.List;
2224

2325
@RemoteServiceRelativePath("springGwtServices/greetingService")
2426
public interface GreetingService extends RemoteService {
27+
28+
Greeting getGreeting(String text);
2529

30+
void addGreeting(String author, String text);
31+
32+
void updateGreeting(String author, String text);
33+
34+
void deleteGreeting(String text);
35+
36+
List<Greeting> getGreetings();
37+
2638
String greet(String name);
2739
}

src/main/java/com/hellogwt/client/GreetingServiceAsync.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@
1616
package com.hellogwt.client;
1717

1818
import com.google.gwt.user.client.rpc.AsyncCallback;
19+
import com.hellogwt.shared.domain.Greeting;
20+
import java.util.List;
1921

2022
public interface GreetingServiceAsync {
2123

24+
void getGreeting(String text, AsyncCallback<Greeting> async);
25+
26+
void addGreeting(String author, String text, AsyncCallback<Void> async);
27+
28+
void updateGreeting(String author, String text, AsyncCallback<Void> async);
29+
30+
void deleteGreeting(String text, AsyncCallback<Void> async);
31+
32+
void getGreetings(AsyncCallback<List<Greeting>> async);
33+
2234
void greet(String name, AsyncCallback<String> callback);
2335
}

src/main/java/com/hellogwt/client/HelloGWT.java

Lines changed: 157 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,45 @@
1919
import com.google.gwt.core.client.GWT;
2020
import com.google.gwt.event.dom.client.KeyUpEvent;
2121
import com.google.gwt.event.dom.client.KeyUpHandler;
22+
import com.google.gwt.event.dom.client.ClickEvent;
23+
import com.google.gwt.event.dom.client.ClickHandler;
24+
import com.google.gwt.user.client.Window;
2225
import com.google.gwt.user.client.rpc.AsyncCallback;
26+
import com.google.gwt.user.client.ui.Button;
27+
import com.google.gwt.user.client.ui.FlexTable;
28+
import com.google.gwt.user.client.ui.HorizontalPanel;
2329
import com.google.gwt.user.client.ui.Label;
2430
import com.google.gwt.user.client.ui.RootPanel;
2531
import com.google.gwt.user.client.ui.TextBox;
32+
import com.hellogwt.shared.domain.Greeting;
33+
import java.util.List;
2634

2735
/**
2836
* @author Alex Tretyakov
2937
*/
3038
public class HelloGWT implements EntryPoint {
39+
3140
private GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
41+
42+
private HorizontalPanel greetingPanel = new HorizontalPanel();
3243
private TextBox nameTextBox = new TextBox();
3344
private Label greetingLabel = new Label("Hello, GWT!");
3445

46+
private HorizontalPanel authorPanel = new HorizontalPanel();
47+
private HorizontalPanel textPanel = new HorizontalPanel();
48+
private HorizontalPanel editPanel = new HorizontalPanel();
49+
50+
private Label authorLabel = new Label("Author:");
51+
private Label textLabel = new Label("Text:");
52+
private TextBox authorTextBox = new TextBox();
53+
private TextBox textTextBox = new TextBox();
54+
55+
private Button addButton = new Button("Add");
56+
private Button updateButton = new Button("Update");
57+
private Button deleteButton = new Button("Delete");
58+
59+
private FlexTable greetingsFlexTable = new FlexTable();
60+
3561
/**
3662
* Use our service GreetingService in application.
3763
*
@@ -40,23 +66,150 @@ public class HelloGWT implements EntryPoint {
4066
* Otherwise label text will tell us about error.
4167
*/
4268
public void onModuleLoad() {
43-
RootPanel.get().add(nameTextBox);
44-
RootPanel.get().add(greetingLabel);
69+
initWidgets();
70+
initHandlers();
71+
72+
refreshGreetingsTable();
73+
}
74+
75+
private void initWidgets() {
76+
// label
77+
greetingPanel.add(nameTextBox);
78+
greetingPanel.add(greetingLabel);
79+
RootPanel.get().add(greetingPanel);
80+
81+
// table
82+
authorLabel.setWidth("50");
83+
authorTextBox.setWidth("100");
84+
authorPanel.add(authorLabel);
85+
authorPanel.add(authorTextBox);
86+
87+
textLabel.setWidth("50");
88+
textTextBox.setWidth("100");
89+
textPanel.add(textLabel);
90+
textPanel.add(textTextBox);
91+
92+
addButton.setWidth("50");
93+
updateButton.setWidth("50");
94+
deleteButton.setWidth("50");
95+
editPanel.add(addButton);
96+
editPanel.add(updateButton);
97+
editPanel.add(deleteButton);
4598

46-
final AsyncCallback<String> callback = new AsyncCallback<String>() {
99+
RootPanel.get().add(authorPanel);
100+
RootPanel.get().add(textPanel);
101+
RootPanel.get().add(editPanel);
102+
RootPanel.get().add(greetingsFlexTable);
103+
}
104+
105+
private void initHandlers() {
106+
// label
107+
final AsyncCallback<String> textbox_callback = new AsyncCallback<String>() {
108+
@Override
47109
public void onFailure(Throwable caught) {
48110
greetingLabel.setText("ERROR!");
49111
}
50112

113+
@Override
51114
public void onSuccess(String result) {
52115
greetingLabel.setText(result);
53116
}
54117
};
55118

56119
nameTextBox.addKeyUpHandler(new KeyUpHandler() {
120+
@Override
57121
public void onKeyUp(KeyUpEvent keyUpEvent) {
58-
greetingService.greet(nameTextBox.getText(), callback);
122+
greetingService.greet(nameTextBox.getText(), textbox_callback);
59123
}
60124
});
125+
126+
// table
127+
final AsyncCallback<Void> btn_callback = new AsyncCallback<Void>() {
128+
@Override
129+
public void onFailure(Throwable caught) {
130+
Window.alert("ERROR: Cannot edit greetings!");
131+
}
132+
133+
@Override
134+
public void onSuccess(Void result) {
135+
refreshGreetingsTable();
136+
}
137+
};
138+
139+
addButton.addClickHandler(new ClickHandler() {
140+
@Override
141+
public void onClick(ClickEvent clickEvent) {
142+
if (!authorTextBox.getText().isEmpty()
143+
&& !textTextBox.getText().isEmpty()) {
144+
greetingService.getGreeting(textTextBox.getText(),
145+
new AsyncCallback<Greeting>() {
146+
@Override
147+
public void onFailure(Throwable caught) {
148+
Window.alert("ERROR: Cannot find greeting!");
149+
}
150+
151+
@Override
152+
public void onSuccess(Greeting result) {
153+
if (result == null) {
154+
greetingService.addGreeting(authorTextBox.getText(),
155+
textTextBox.getText(), btn_callback);
156+
} else {
157+
Window.alert("Greeting already exists!");
158+
}
159+
}
160+
});
161+
} else {
162+
Window.alert("\"Author\" and \"Text\" fields cannot be empty!");
163+
}
164+
}
165+
});
166+
167+
updateButton.addClickHandler(new ClickHandler() {
168+
@Override
169+
public void onClick(ClickEvent event) {
170+
if (!authorTextBox.getText().isEmpty()
171+
&& !textTextBox.getText().isEmpty()) {
172+
greetingService.updateGreeting(authorTextBox.getText(),
173+
textTextBox.getText(), btn_callback);
174+
} else {
175+
Window.alert("\"Author\" and \"Text\" fields cannot be empty!");
176+
}
177+
}
178+
});
179+
180+
deleteButton.addClickHandler(new ClickHandler() {
181+
@Override
182+
public void onClick(ClickEvent event) {
183+
greetingService.deleteGreeting(textTextBox.getText(), btn_callback);
184+
}
185+
});
186+
}
187+
188+
private void refreshGreetingsTable() {
189+
greetingService.getGreetings(new AsyncCallback<List<Greeting>>() {
190+
@Override
191+
public void onFailure(Throwable throwable) {
192+
Window.alert("ERROR: Cannot load greetings!");
193+
}
194+
195+
@Override
196+
public void onSuccess(List<Greeting> greetings) {
197+
fillGreetingsTable(greetings);
198+
}
199+
});
200+
}
201+
202+
private void fillGreetingsTable(List<Greeting> greetings) {
203+
greetingsFlexTable.removeAllRows();
204+
205+
greetingsFlexTable.setText(0, 0, "Author");
206+
greetingsFlexTable.setText(0, 1, "Text");
207+
208+
for (Greeting greeting : greetings) {
209+
int row = greetingsFlexTable.getRowCount();
210+
211+
greetingsFlexTable.setText(row, 0, greeting.getAuthor());
212+
greetingsFlexTable.setText(row, 1, greeting.getText());
213+
}
61214
}
62215
}

0 commit comments

Comments
 (0)