mybatis / spring-boot-starter

MyBatis integration with Spring Boot
Apache License 2.0
4.14k stars 1.79k forks source link

How to config mybatis datasource?not a simple jdbc #10

Closed CharlesMaster closed 8 years ago

CharlesMaster commented 8 years ago

in a spring project,I usually config like this:

    <bean id="rowanTemplateSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="XXXDataSource" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="rowanTemplateSqlSessionFactory" />
    </bean>
``

but when I use mybatis-spring-boot,how to config XXXDataSource?I tried lots of times,but still failed.
liukaitj commented 8 years ago

Here's an example I used to config mybatis in spring boot, combined with a plugin (PageHelper) that functions as pagination.

    @Bean
    public DataSource dataSource() {
        PooledDataSource dataSource = new PooledDataSource(dbDriver, dbUrl, dbUser, dbPass);
        dataSource.setPoolMaximumActiveConnections(maxActiveConnections);
        dataSource.setPoolMaximumIdleConnections(maxIdleConnections);
        dataSource.setPoolMaximumCheckoutTime(maxCheckoutTime);
        dataSource.setPoolTimeToWait(timeToWait);
        dataSource.setPoolPingQuery(pingQuery);
        dataSource.setPoolPingEnabled(pingEnabled);
        dataSource.setPoolPingConnectionsNotUsedFor(connectionsNotUserdFor);
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

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

        if (usePageHelper) {
            Properties prop = new Properties();
            prop.setProperty("dialect", dialect);
            prop.setProperty("offsetAsPageNum", offsetAsPageNum);
            prop.setProperty("rowBoundsWithCount", rowBoundsWithCount);
            prop.setProperty("pageSizeZero", pageSizeZero);
            prop.setProperty("reasonable", reasonable);

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

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

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

Of course, you need to place annotation-based configurations on your @Configuration class:

@Configuration
@EnableTransactionManagement
@MapperScan(value = "your.mapper.package")
public class MybatisConfiguration {
// ...
}
CharlesMaster commented 8 years ago

thanks,I'll try.

mallim commented 8 years ago

@CharlesMaster you still have any problem? Or we can proceed to close this issue?

eddumelendez commented 8 years ago

@CharlesMaster any news about this?

gariem commented 8 years ago

Add this to your dependencies:

compile("org.mybatis:mybatis:3.3.0")
compile("org.mybatis:mybatis-spring:1.2.3")
compile("org.springframework:spring-jdbc:4.1.7.RELEASE")

Then, put this in your application.yml:

spring:
  datasource:
    url: jdbc:oracle:thin:@db-host:port/catalog
    username: user
    password: pass
    driver-class-name: oracle.jdbc.OracleDriver

Then you can autowire the datasource inside any spring-managed bean, for instance in a configuration:

@Configuration
@MapperScan(basePackages = "com.mycompany.application.persistence.mapper", sqlSessionFactoryRef = "mySqlSessionFactory")
public class MyScannerConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyScannerConfig.class);

    @Autowired
    DataSource myDataSource;

    @Bean(name = "mySqlSessionFactory")
    public SqlSessionFactory mySqlSessionFactory() {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(myDataSource);
        sessionFactory.setTypeAliasesPackage("com.mycompany.application.domain");
        return sessionFactory.getObject();
    }
}

Then you just need to keep writing your Mapper XMLs and Interfaces in the same way you've been doing it.