2 回答
TA贡献1785条经验 获得超8个赞
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.example.schedule.dao")
@PropertySource("classpath:application.yml")
public class MyBatisConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("druid.url"));
dataSource.setUsername(env.getProperty("druid.username"));
dataSource.setPassword(env.getProperty("druid.password"));
dataSource.setInitialSize(Integer.parseInt(env.getProperty("druid.initial-size")));
dataSource.setMinIdle(Integer.parseInt(env.getProperty("druid.min-idle")));
dataSource.setMaxActive(Integer.parseInt(env.getProperty("druid.max-active")));
dataSource.setTestOnBorrow(Boolean.valueOf(env.getProperty("druid.test-on-borrow")));
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//分页插件
PageHelper pageHelper = new PageHelper();
Properties props = new Properties();
props.setProperty("reasonable", "true");
props.setProperty("supportMethodsArguments", "true");
props.setProperty("returnPageInfo", "check");
props.setProperty("params", "count=countSql");
pageHelper.setProperties(props);
//添加插件
bean.setPlugins(new Interceptor[]{pageHelper});
try {
//指定基包
bean.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package"));
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("mapper/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
TA贡献1786条经验 获得超12个赞
PageHelper pageHelper这个没有实现Interceptor接口怎么给new Interceptor[]?
新版本中有个PageInterceptor类实现了Interceptor接口,这个时候赋值给new Interceptor[]才可行。
// 设置MyBatis分页插件
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "oracle");
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
pageInterceptor.setProperties(properties);
bean.setPlugins(new Interceptor[]{pageInterceptor});
添加回答
举报