pom.xmlhtml
<!-- 引入分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency>
yml
spring:
application:java
name: spring-cloud-dynamic
datasource:mysql
#類型 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/f2f?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: initial-size: 2 max-idle: 10 min-idle: 1 max-wait: 60000 max-active: 20 #最大空閒鏈接數 #多久進行一次檢測,檢測須要關閉的空閒鏈接 time-between-eviction-tuns-millis: 60000
MybatisConfig
/**git
@Configuration
@EnableTransactionManagement
@PropertySource(value = "classpath:application.yml", ignoreResourceNotFound = true)
public class MybatisConfig implements TransactionManagementConfigurer {github
@Value("${mybatis.mapper-locations}") private String mapper; @Value("${mybatis.type-aliases-package}") private String aliases; @Autowired private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); // 設置數據源 bean.setDataSource(dataSource); // 設置xml bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper)); // 設置別名 bean.setTypeAliasesPackage(aliases); // 添加分頁插件 bean.setPlugins(new Interceptor[]{pageInterceptor()}); bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return bean.getObject(); } /** * 分頁攔截器 * @return */ private PageInterceptor pageInterceptor() { PageInterceptor pageInterceptor = new PageInterceptor(); // 詳見 com.github.pagehelper.page.PageParams Properties p = new Properties(); // RowBounds是否進行count查詢[利率期貨](https://www.gendan5.com/ff/if.html) - 默認不查詢 p.setProperty("rowBoundsWithCount", "true"); // 當設置爲true的時候,若是page size設置爲0(或RowBounds的limit=0),就不執行分頁,返回所有結果 p.setProperty("pageSizeZero", "true"); // 分頁合理化 p.setProperty("reasonable", "false"); // 是否支持接口參數來傳遞分頁參數,默認false p.setProperty("supportMethodsArguments", "true"); // 設置數據庫方言 , 也能夠不設置,會動態獲取 p.setProperty("helperDialect", "mysql"); pageInterceptor.setProperties(p); return pageInterceptor; } @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); }
}spring