Closed CharlesMaster closed 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 {
// ...
}
thanks,I'll try.
@CharlesMaster you still have any problem? Or we can proceed to close this issue?
@CharlesMaster any news about this?
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.
in a spring project,I usually config like this: