I haven't dropped it in a public repository yet.  I'll see if I can make
time to push it out to our open source repository on github.

On Thu, Jan 26, 2017 at 1:07 AM Joe Ernst <[email protected]> wrote:

> Brent,
>
> Did you ever end up open-sourcing your MyBatis-Dropwizard integration?  I
> can copy your code from here, but I'd rather get it from GitHub if you have
> put it there.
>
>
> On Thursday, February 19, 2015 at 8:09:17 AM UTC-8, Brent Ryan wrote:
>
> It's a pretty simple implementation and we plan on putting this up as OSS
> soon if anyone wants it:
>
> *Initialize your mybatis tier like this:*
>         final ManagedDataSource ds =
> configuration.getDataSourceFactory().build(environment.metrics(), "sql");
>
>         MyBatisFactory factory = new MyBatisFactory();
>         SqlSessionFactory sessionFactory = factory.build(environment,
> configuration.getDataSourceFactory(), ds,
>                 "sqlserver");
>         sessionFactory.getConfiguration().addMapper(MyExampleDAO.class);
>
> sessionFactory.getConfiguration().getTypeHandlerRegistry().register(Date.class,
> ESTDateTypeHandler.class);
>
> *Use it like this:*
>         try (SqlSession session = sessionFactory.openSession(true)) {
>             MyExampleDAO dao = session.getMapper(MyExampleDAO.class);
>
>             dao.doSomething()
>         }
>
> *The factory class looks like this:*
>
> package com.cvent.dropwizard.mybatis;
>
> import io.dropwizard.setup.Environment;
> import io.dropwizard.db.ManagedDataSource;
> import io.dropwizard.db.DataSourceFactory;
> import org.apache.ibatis.io.Resources;
> import org.apache.ibatis.session.Configuration;
> import org.apache.ibatis.session.SqlSessionFactory;
> import org.apache.ibatis.session.SqlSessionFactoryBuilder;
> import org.apache.ibatis.transaction.TransactionFactory;
> import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
>
> import java.io.IOException;
> import java.io.InputStream;
>
> /**
>  * A factor for creating instances of MyBatis
>  *
>  * @author bryan
>  */
> public class MyBatisFactory {
>     /**
>      * A string to identify this instance within ibatis framework. Only
> here because API requires it.
>      */
>     private static final String ENV_NAME = "mybatis";
>
>     /**
>      * Create an instance of MyBatis.
>      *
>      * @param environment The dropwizard environment
>      * @param config A Mybatis config object
>      * @param name The name of this mybatis factory used for metrics
>      * @return An instance of MyBatis.
>      * @throws ClassNotFoundException
>      */
>     public final SqlSessionFactory build(Environment environment,
>             MyBatisConfiguration config,
>             String name) throws ClassNotFoundException {
>         final ManagedDataSource dataSource =
> config.getConfig().build(environment.metrics(), name);
>         return build(environment, config, dataSource, name);
>     }
>
>     /**
>      * Create an instance of MyBatis.
>      *
>      * @param environment The dropwizard environment
>      * @param config A Mybatis config object
>      * @param dataSource
>      * @param name The name of this mybatis factory used for metrics
>      * @return An instance of MyBatis.
>      */
>     public final SqlSessionFactory build(Environment environment,
>             MyBatisConfiguration config,
>             ManagedDataSource dataSource,
>             String name) {
>
>         SqlSessionFactory sessionFactory = null;
>
>         // Try to use the mybatis configuration file if it is specified
> and exists.
>         try (InputStream inputStream =
> Resources.getResourceAsStream(config.getConfigFile())) {
>             sessionFactory = new
> SqlSessionFactoryBuilder().build(inputStream);
>         } catch (IOException ioe) {
>             // Build session factory from configuration values given in
> the dropwizard config.
>             TransactionFactory transactionFactory = new
> JdbcTransactionFactory();
>             org.apache.ibatis.mapping.Environment myBatisEnvironment =
>                     new org.apache.ibatis.mapping.Environment(ENV_NAME,
> transactionFactory, dataSource);
>             Configuration mybatisConfiguration = new
> Configuration(myBatisEnvironment);
>             sessionFactory = new
> SqlSessionFactoryBuilder().build(mybatisConfiguration);
>         }
>
>         environment.lifecycle().manage(dataSource);
>         environment.healthChecks().register(name,
>                 new MyBatisHealthCheck(sessionFactory, name,
> getValidationQuery(config.getConfig())));
>
>         return sessionFactory;
>     }
>
>     /**
>      * Create an instance of MyBatis.
>      *
>      * @param environment The dropwizard environment
>      * @param configuration The data source factory/configuration
>      * @param dataSource The datasource you want to use.
>      * @param name The name of this mybatis factory used for metrics
>      * @return An instance of MyBatis.
>      */
>     public final SqlSessionFactory build(Environment environment,
>             DataSourceFactory configuration,
>             ManagedDataSource dataSource,
>             String name) {
>         // Initialize validation query
>         final String suppliedValidationQuery =
> configuration.getValidationQuery();
>         final String validationQuery;
>
>         //if the user uses the default health check in dropwizard, health
> check will fail
>         //because there will be no mapper for the query.
>         if (suppliedValidationQuery.equalsIgnoreCase("/* Health Check */
> SELECT 1")) {
>             validationQuery =
> "com.cvent.dropwizard.mybatis.mappers.HealthCheckMapper.healthCheck";
>         } else {
>             validationQuery = suppliedValidationQuery;
>         }
>
>         // Build mybatis session factory
>         TransactionFactory transactionFactory = new
> JdbcTransactionFactory();
>         org.apache.ibatis.mapping.Environment myBatisEnvironment =
>                 new org.apache.ibatis.mapping.Environment(ENV_NAME,
> transactionFactory, dataSource);
>         Configuration mybatisConfiguration = new
> Configuration(myBatisEnvironment);
>         SqlSessionFactory sessionFactory = new
> SqlSessionFactoryBuilder().build(mybatisConfiguration);
>
>         // Setup managed data source and health checks
>         environment.lifecycle().manage(dataSource);
>         environment.healthChecks().register(name, new
> MyBatisHealthCheck(sessionFactory, name, validationQuery));
>
>         return sessionFactory;
>     }
>
>     /**
>      * Get validation query value
>      *
>      * @param mybatisConfig Configuration values for mybatis
>      */
>     private String getValidationQuery(DataSourceFactory mybatisConfig) {
>
>         // Initialize validation query
>         final String suppliedValidationQuery =
> mybatisConfig.getValidationQuery();
>         final String validationQuery;
>
>         //if the user uses the default health check in dropwizard, health
> check will fail
>         //because there will be no mapper for the query.
>         if (suppliedValidationQuery.equalsIgnoreCase("/* Health Check */
> SELECT 1")) {
>             validationQuery =
> "com.cvent.dropwizard.mybatis.mappers.HealthCheckMapper.healthCheck";
>         } else {
>             validationQuery = suppliedValidationQuery;
>         }
>
>         return validationQuery;
>     }
> }
>
>
>
> On Sun Feb 08 2015 at 11:42:40 AM Alexandre Jacques <[email protected]>
> wrote:
>
> Hi Brent,
>
> I would be interested on your approach to integrate MyBatis. Could you
> post some thoughts on how you did it?
>
> Regards,
>
> Alexandre Jacques
>
>
> On Wednesday, January 21, 2015 at 10:19:06 PM UTC-2, Brent Ryan wrote:
>
> It was fairly easy to integrate mybatis with our dropwizard stack. Let me
> know if you need help in doing so.
>
> Brent
>
> On Wed, Jan 21, 2015 at 5:25 PM Miguel larraz <[email protected]> wrote:
>
> I agree, I am interested in use dropwizzard, but jdbi looks tedius,
> compared with mybatis. Is not posible configure mybatis support? i found
> hibernate alternative in the documentation, but no mybatis
>
> El sábado, 8 de febrero de 2014, 19:50:02 (UTC+1), Brent Ryan escribió:
>
> Out of curiosity, any reason why dropwizard went with jdbi instead of
> mybatis as the default?  It appears like mybatis is a little more advanced
> (but same concept) and has better documentation.
>
> Brent
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "dropwizard-user" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/dropwizard-user/K-3kek-xIrc/unsubscribe.
>
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
>
>
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "dropwizard-user" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/dropwizard-user/K-3kek-xIrc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "dropwizard-user" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/dropwizard-user/K-3kek-xIrc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to