Git Product home page Git Product logo

guice-persist-jooq's Introduction

Guice Persist / jOOQ Integration

A simple integration between Guice's persistence extensions and jOOQ. Follows closely in the pattern of of the JPA persistence extension written by Dhanji R. Prasanna ([email protected]).

Maven Coordinates

The project is deployed to Maven Central:

<dependency>
   <groupId>com.adamlewis</groupId>
   <artifactId>guice-persist-jooq</artifactId>
   <version>0.1.1</version>
</dependency>

Basic Usage

See Guice Persist and Transactions and Units of Work for a reference on the basic semantics of the Guice Persist extension.

In your module, install a new com.adamlewis.guice.persist.jooq.JooqPersistModule and then provide bindings for javax.sql.DataSource and org.jooq.SQLDialect. Then write @Injectable DAOs which depend on org.jooq.DSLContext.

Example

Here is an example Guice module written to connect guice-persist-jooq up to the Dropwizard connection factory:

import javax.sql.DataSource;

import org.jooq.SQLDialect;

import com.adamlewis.guice.persist.jooq.JooqPersistModule;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.yammer.dropwizard.db.DatabaseConfiguration;
import com.yammer.dropwizard.db.ManagedDataSource;
import com.yammer.dropwizard.db.ManagedDataSourceFactory;

public class MyPersistenceModule extends AbstractModule {

	private final DatabaseConfiguration configuration;
	
	public MyPersistenceModule(final DatabaseConfiguration configuration) {
		this.configuration = configuration;
	}
	
	@Override
	protected void configure() {
		install(new JooqPersistModule());
		bind(DataSource.class).to(ManagedDataSource.class);
	}
	

	@Provides ManagedDataSource dataSource(final ManagedDataSourceFactory factory) throws ClassNotFoundException {
		return factory.build(configuration);
	}
	
	@Provides
	public SQLDialect dialect() {
		//TODO read from DB configuration
		return SQLDialect.POSTGRES;
	}
}

And here is an example of what a DAO might look like:

public class UserDao {

	private final DSLContext create;
	
	@Inject
	public UserDao(final DSLContext dsl) {
		this.create = dsl;
	}
	
	
	public List<String> getUsernames() {
		return create.selectDistinct(User.USER.NAME).from(User.USER).fetch(User.USER.NAME);
	}	
}

guice-persist-jooq's People

Contributors

supercargo avatar johnstok avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.