Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering Plugin #7

Closed
patrek opened this issue Dec 1, 2015 · 7 comments
Closed

Registering Plugin #7

patrek opened this issue Dec 1, 2015 · 7 comments
Milestone

Comments

@patrek
Copy link

patrek commented Dec 1, 2015

Is there an easy way to register a Plugin (org.apache.ibatis.plugin.Interceptor) to the sqlSessionFactory with only java configuration?

@liukaitj
Copy link

liukaitj commented Dec 1, 2015

This is an example which I use to register a pagination plugin (PageHelper) in pure Java config style. Though looks a bit bored, it's not so unacceptable in terms of the fact that you need to write it only once.

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean sfb = new SqlSessionFactoryBean();
        sfb.setDataSource(dataSource);

        Properties prop = new Properties();
        prop.setProperty("dialect", "mysql");

        PageHelper pagePlugin = new PageHelper();
        pagePlugin.setProperties(prop);

        Interceptor[] plugins = {pagePlugin};
        sfb.setPlugins(plugins);

        SqlSessionFactory factory = sfb.getObject();
        factory.getConfiguration().setMapUnderscoreToCamelCase(true);
        return factory;
    }

@patrek
Copy link
Author

patrek commented Dec 1, 2015

Thanks.

Would have been nice if it was possible to do it by setting something in application.properties instead of overriding the bean creation.

@eddumelendez
Copy link
Member

I am thinking to register all plugins automatically. Let try this approach. PR is welcome :)

@patrek
Copy link
Author

patrek commented Dec 1, 2015

I'll see what I can do this weekend :-)

@patrek
Copy link
Author

patrek commented Dec 10, 2015

Didn't get around to work in it yet.

While I'm at it, any idea of the best way to implement the configuration of settings the same way?

I've seen liukaitj's example about doing it. Is this the best approach?

SqlSessionFactory factory = sfb.getObject();
        factory.getConfiguration().setMapUnderscoreToCamelCase(true);

instead of using mybatis-config.xml

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

@mallim
Copy link
Contributor

mallim commented Dec 11, 2015

Actually if this works, why need to bring it inside?

 <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

The issue with #8 is it does not work by putting it in mybatis-config.xml

To quote the PageHelper example in here, I am doing this in mybatis-config.xml and it is working well:

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="pagehelper.SqlServer2012Dialect"/>
        </plugin>
    </plugins>

@patrek
Copy link
Author

patrek commented Dec 21, 2015

As soon as you define mybatis-config.xml in the application.properties file, all other mybatis settings from that file are ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants