Skip to content

Instantly share code, notes, and snippets.

@dhoss
Last active May 16, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoss/539650094085c2ef152acab6d5884a0d to your computer and use it in GitHub Desktop.
Save dhoss/539650094085c2ef152acab6d5884a0d to your computer and use it in GitHub Desktop.
dao base class
public abstract class BaseService {
protected void create(GenericTablePojo tableObject) {
GenericRecord g = buildRecordObject(tableObject);
g.store();
}
protected void update(GenericTablePojo tableObject) {
GenericRecord g = buildGenericRecord(tableObject);
g.store();
}
protected void delete(GenericTablePojo tableObject) {
GenericRecord g = buildGenericRecord(tableObject);
g.delete();
}
protected GenericTablePojo find(String slug) {
return dao().fetchOneBySlug(slug);
}
public List<GenericRecord> list(int pageNumber) {
pager = new Page(count());
TABLE table = table();
RecordMapper genericMapper = mapper();
return sql.select()
.from(table)
.orderBy(table.CREATED_ON.desc())
.limit(pageSize)
.offset(pager.offsetFromPage(pageNumber))
.fetch(genericMapper);
}
// these would need to be provided by the subclass
protected abstract GenericRecord buildRecordObject(GenericTablePojo tableObject);
protected abstract TABLE table();
protected abstract BaseDao dao();
protected abstract RecordMapper mapper();
}
public class GalleryService extends BaseService {
// all we have to do here is define the table to query
@Override
protected TABLE table() {
return GALLERIES;
}
@Override
protected BaseDao dao() {
return new GalleryDao(configuration);
}
@Override
protected GalleryRecord buildRecordsObject(Gallery gallery) {
// try to fetch the record
GalleryRecord g = sql.fetchOne(GALLERY, GALLERY.ID.equal(gallery.getId()));
if (g == null) {
g = sql.newRecord(GALLERY);
}
g.setName(gallery.getName());
g.setDescription(gallery.getDescription());
g.setCoverPhoto(gallery.getCoverPhoto());
g.setSlug(gallery.getSlug());
g.setCreatedOn(gallery.getCreatedOn());
g.setUpdatedOn(gallery.getUpdatedOn());
return g;
}
@Override
protected RecordMapper mapper() {
return new Gallery(...);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<!-- Specify the maven code generator plugin -->
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.7.0</version>
<!-- The plugin should hook into the generate goal -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!-- Manage the plugin's dependency. In this example, we'll use a PostgreSQL database -->
<dependencies>
<!-- Manage the plugin's dependency. In this example, we'll use a PostgreSQL database -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
</dependencies>
<!-- Specify the plugin configuration.
The configuration format is the same as for the standalone code generator -->
<configuration>
<!-- JDBC connection parameters -->
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>${db.url}</url>
<user>${db.username}</user>
<password>${db.password}</password>
</jdbc>
<!-- Generator parameters -->
<generator>
<generate>
<daos>true</daos>
</generate>
<name>org.jooq.util.DefaultGenerator</name>
<database>
<name>org.jooq.util.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes />
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>com.lumos.db</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment