SpringBoot2.0+MyBatis+druid+PageHelper

上篇文章我們介紹了SpringBoot和MyBatis的整合,可以說非常簡單快捷的就搭建了一個web項目,但是在一個真正的企業級項目中,可能我們還需要更多的更加完善的框架才能開始真正的開發,比如連接池、分頁插件等。下面我們就來看看在SpringBoot中怎麼快速的集成這些東西。

一、新建一個項目,引入相關依賴,加粗的是本項目中新引入的依賴



org.springframework.boot
spring-boot-starter-test




org.springframework.boot
spring-boot-starter-web



org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1




com.alibaba
druid
1.1.8





mysql
mysql-connector-java





com.github.pagehelper
pagehelper
5.1.2


二、為了項目配置的整潔性,在SpringBoot-mybatis的項目基礎上,我將數據庫和MyBatis的相關操作進行了統一配置,使得配置根據清晰簡單,項目結構如下

SpringBoot2.0+MyBatis+druid+PageHelper

後期項目會將所有的配置放在configuration包下,具體數據庫配置如下

@Configuration
@MapperScan(value = "com.somta.springboot.dao")
public class MyBatisConfiguration {
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Bean
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
dataSource.setInitialSize(5);
dataSource.setMaxActive(30);
dataSource.setMinIdle(5);
dataSource.setMaxWait(60000);
return dataSource;
}
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/**/Mysql_*Mapper.xml"));
return sqlSessionFactoryBean;
}
}

1、將Dao層掃描和MyBatis文件的掃描統一放在配置文件中

2、使用了阿里開源的Druid連接池,SpringBoot默認使用的連接池是Hikari,兩者之間的優缺點後續將會單獨介紹,配置成功後啟動項目,我們可以看到項目當前使用的是那種連接池,如下圖:

SpringBoot2.0+MyBatis+druid+PageHelper

3、在src/main/resources下面新增了一個mybatis-config文件,該文件配置了MyBatis與數據庫的相關信息,和PageHelper的相關配置,注意:(在不同的PageHelper版本中PageHelper的攔截器發生了變化,PageHelper-4.1.1中使用的是com.github.pagehelper.PageHelper,在PageHelper-5.1.2中使用的攔截器是com.github.pagehelper.PageInterceptor,具體小版本以官網公告為準)


br> PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">















































三、PageHelper的使用方法

PageHelper.startPage(pageNum, pageSize)只作用離它最近的一個查詢,更多與分頁相關的信息我們都可以在page對象中拿到,完全可以滿足各種情況下的分頁查詢。

@Test
public void testQueryUserList() throws Exception {
int pageNum=1;
int pageSize=10;
Page page = PageHelper.startPage(pageNum, pageSize);
userDao.queryUserList();
System.out.println("總共條數:"+page.getTotal());
for (User user : page.getResult()) {
System.out.println(user.getName());
}
}

看到如圖所示的輸出表示分頁插件配置成功了

SpringBoot2.0+MyBatis+druid+PageHelper

Git代碼地址:https://gitee.com/Somta/SpringBoot/tree/master/SpringBoot-mybatis-expand

現在私信我“資料”即可獲取Java工程化、高性能及分佈式、高性能、高架構。性能調優、Spring,MyBatis,Netty源碼分析和大數據等多個知識點高級進階乾貨的直播免費學習權限及領取相關資料


分享到:


相關文章: