我这里使用的MyBatis-Plus是当前最新的3.2.0版本,
1. 引入需要的jar,基础jar包括:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.1.6.RELEASE</version> <scope>test</scope> </dependency> <!--阿里数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.14</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- MySQL数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> </dependencies>
2. 配置application.yml文件
spring: # 数据库连接池 datasource: druid: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/****?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: **** password: **** initialSize: 5 minIdle: 10 maxActive: 20 maxWait: 60000 # 配置MyBatis-Plus打印SQL日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3. 根据数据库表生成mapper.xml,dao,service,controller,大家可以参照代码生成器文章:MyBatis-Plus使用(1)-概述+代码生成器
4. 编写启动类
@MapperScan("com.zh.dao") @ComponentScan(value = "com.zh") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); System.out.println(" ====== Application Start ======"); } /** * MyBatisPlus的分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
以上三个注解简单讲下作用:
@MapperScan是mybatis.spring的注解,作用是指定扫描的dao路径,如果你的每个dao类上都有@Mapper注解,则@MapperScan就可以忽略不写了。
@ComponentScan 是spring注解,用于扫描需要Spring自动装配的文件。
@SpringBootApplication是启动Spring自动装配。
5. 测试
@RunWith(SpringRunner.class) @SpringBootTest(classes = {Application.class}) public class ServiceTest { @Autowired private UserInfoService userInfoService; @Test public void pageTest() { Page<UserInfo> page = new Page<>(); page.setCurrent(1); page.setSize(10); page.setOrders(Lists.newArrayList(OrderItem.asc("username"))); QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("gender", 0); IPage<UserInfo> result = userInfoService.page(page, wrapper); System.out.println(JSON.toJSONString(result)); } }
上面的作用是分页查询,根据username排序,查询gender=0的用户信息,生成的SQL如下:
==> Preparing: SELECT COUNT(1) FROM user_info WHERE (gender = ?) ==> Parameters: 0(Integer) <== Columns: COUNT(1) <== Row: 2 ==> Preparing: SELECT id, address, gender, username FROM user_info WHERE (gender = ?) ORDER BY username LIMIT ?,? ==> Parameters: 0(Integer), 0(Long), 10(Long) <== Columns: id, address, gender, username <== Row: 1, 花果山, 0, 孙悟空 <== Row: 2, 高老庄, 0, 猪八戒 <== Total: 2
注意:我用的MyBatis-Plus是3.2.0版本,已经对分页做了优化,大家如果用以前的版本最好升级到3.2.0版本。
因为之前的3.1.2版本生成的SQL语句效率是有问题的,主要问题在count语句,他是把用子查询把所有符合条件的数据都查询出来,再count,真要优化还需要重写page方法,SQL如下:
SELECT COUNT(1) FROM (SELECT id, address, gender, username FROM user_info WHERE (gender = 0)) AS D
@SpringBootApplication