forked from dropwizard/dropwizard
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dropwizard#49 from wolfeidau/master
Add JDBI usage to dropwizard-example
- Loading branch information
Showing
12 changed files
with
281 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
target | ||
atlassian-ide-plugin.xml | ||
logs | ||
*.iml | ||
*.ipr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Introduction | ||
|
||
The drop wizard example application was developed to, as its name implies, provide examples of some of the features | ||
present in drop wizard. | ||
|
||
# Overview | ||
|
||
Included with this application is an example of the optional db API module. The examples provided illustrate a few of | ||
the features available in (JDBI)[http://jdbi.org], along with demonstrating how these are used from within dropwizard. | ||
|
||
This database example is comprised of the following classes. | ||
|
||
* The `PersonDAO` illustrates using the [SQL Object Queries](http://jdbi.org/sql_object_api_queries/) and string template | ||
features in JDBI. | ||
|
||
* The `PeopleDAO.sql.stg` stores all the SQL statements for use in the `PersonDAO`, note this is located in the | ||
src/resources under the same path as the `PersonDAO` class file. | ||
|
||
* The `SetupDatabaseCommand` illustrates building a "setup" command which can create your database prior to running | ||
dropwizard your application for the first time. | ||
|
||
* The `PersonResource` and `PeopleResource` are the REST resource which use the PersonDAO to retrieve data from the database, note the injection | ||
of the PersonDAO in their constructors. | ||
|
||
As with all the modules the db example is wired up in the `initialize` function of the `HelloWorldService`. | ||
|
||
# Running The Application | ||
|
||
To test the example application run the following commands. | ||
|
||
* To package the example run. | ||
|
||
mvn package | ||
|
||
* To setup the h2 database run. | ||
|
||
java -jar target/dropwizard-example-0.3.0-SNAPSHOT.jar setup example.yml | ||
|
||
* To run the server run. | ||
|
||
java -jar target/dropwizard-example-0.3.0-SNAPSHOT.jar server example.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
dropwizard-example/src/main/java/com/example/helloworld/cli/SetupDatabaseCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.example.helloworld.cli; | ||
|
||
import com.example.helloworld.HelloWorldConfiguration; | ||
import com.example.helloworld.db.PeopleDAO; | ||
import com.yammer.dropwizard.AbstractService; | ||
import com.yammer.dropwizard.cli.ConfiguredCommand; | ||
import com.yammer.dropwizard.config.Environment; | ||
import com.yammer.dropwizard.db.Database; | ||
import com.yammer.dropwizard.db.DatabaseFactory; | ||
import com.yammer.dropwizard.logging.Log; | ||
import org.apache.commons.cli.CommandLine; | ||
|
||
public class SetupDatabaseCommand extends ConfiguredCommand<HelloWorldConfiguration> { | ||
|
||
public SetupDatabaseCommand() { | ||
super("setup", "Setup the database."); | ||
} | ||
|
||
@Override | ||
protected void run(AbstractService<HelloWorldConfiguration> service, HelloWorldConfiguration configuration, CommandLine params) throws Exception { | ||
|
||
final Log log = Log.forClass(SetupDatabaseCommand.class); | ||
final Environment environment = new Environment(configuration, service); | ||
//service.initializeWithBundles(configuration, environment); | ||
final DatabaseFactory factory = new DatabaseFactory(environment); | ||
final Database db = factory.build(configuration.getDatabaseConfiguration(), "h2"); | ||
final PeopleDAO peopleDAO = db.onDemand(PeopleDAO.class); | ||
|
||
log.info("creating tables."); | ||
peopleDAO.createPeopleTable(); | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
dropwizard-example/src/main/java/com/example/helloworld/core/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.helloworld.core; | ||
|
||
public class Person { | ||
|
||
private long id; | ||
private String fullName; | ||
private String jobTitle; | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getFullName() { | ||
return fullName; | ||
} | ||
|
||
public void setFullName(String fullName) { | ||
this.fullName = fullName; | ||
} | ||
|
||
public String getJobTitle() { | ||
return jobTitle; | ||
} | ||
|
||
public void setJobTitle(String jobTitle) { | ||
this.jobTitle = jobTitle; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
dropwizard-example/src/main/java/com/example/helloworld/db/PeopleDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.helloworld.db; | ||
|
||
import com.example.helloworld.core.Person; | ||
import com.google.common.collect.ImmutableList; | ||
import org.skife.jdbi.v2.sqlobject.Bind; | ||
import org.skife.jdbi.v2.sqlobject.BindBean; | ||
import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys; | ||
import org.skife.jdbi.v2.sqlobject.SqlQuery; | ||
import org.skife.jdbi.v2.sqlobject.SqlUpdate; | ||
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapperFactory; | ||
import org.skife.jdbi.v2.sqlobject.stringtemplate.ExternalizedSqlViaStringTemplate3; | ||
import org.skife.jdbi.v2.tweak.BeanMapperFactory; | ||
|
||
@ExternalizedSqlViaStringTemplate3 | ||
@RegisterMapperFactory(BeanMapperFactory.class) | ||
public interface PeopleDAO { | ||
|
||
@SqlUpdate | ||
void createPeopleTable(); | ||
|
||
@SqlQuery | ||
Person findById(@Bind("id") Long id); | ||
|
||
@SqlUpdate | ||
@GetGeneratedKeys | ||
long create(@BindBean Person person); | ||
|
||
@SqlQuery | ||
ImmutableList<Person> findAll(); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
dropwizard-example/src/main/java/com/example/helloworld/resources/PeopleResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.example.helloworld.resources; | ||
|
||
import com.example.helloworld.core.Person; | ||
import com.example.helloworld.db.PeopleDAO; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import java.util.List; | ||
|
||
@Path("/people") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class PeopleResource { | ||
|
||
private final PeopleDAO peopleDAO; | ||
|
||
public PeopleResource(PeopleDAO peopleDAO) { | ||
this.peopleDAO = peopleDAO; | ||
} | ||
|
||
@POST | ||
public Person createPerson(Person person) { | ||
final long personId = peopleDAO.create(person); | ||
return peopleDAO.findById(personId); | ||
} | ||
|
||
@GET | ||
public List<Person> listPeople() { | ||
return peopleDAO.findAll(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
dropwizard-example/src/main/java/com/example/helloworld/resources/PersonResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.helloworld.resources; | ||
|
||
import com.example.helloworld.core.Person; | ||
import com.example.helloworld.db.PeopleDAO; | ||
import com.yammer.dropwizard.jersey.params.LongParam; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@Path("/person/{personId}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class PersonResource { | ||
|
||
private final PeopleDAO peopleDAO; | ||
|
||
public PersonResource(PeopleDAO peopleDAO) { | ||
this.peopleDAO = peopleDAO; | ||
} | ||
|
||
@GET | ||
public Person getPerson(@PathParam("personId") LongParam personId) { | ||
return peopleDAO.findById(personId.get()); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
dropwizard-example/src/main/resources/com/example/helloworld/db/PeopleDAO.sql.stg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
group PeopleDAO; | ||
|
||
createPeopleTable() ::= << | ||
create table people (id Serial primary key, fullName varchar(255), jobTitle varchar(100)) | ||
>> | ||
|
||
findById(id) ::= << | ||
select id, fullName, jobTitle from people where id = :id | ||
>> | ||
|
||
create() ::= << | ||
insert into people (fullName, jobTitle) values (:fullName, :jobTitle) | ||
>> | ||
|
||
findAll() ::= << | ||
select id, fullName, jobTitle from people | ||
>> |