[TOC] # 一、加入相关依赖 ``` <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.5.0</version><!-- 这个版本号一定要和mybatisplus的主版本号一致 --> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.0</version> </dependency> <!--必须使用0.9以上的版本--> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>4.2</version> </dependency> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.8.1</version> </dependency> <!-- MySQL 连接驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.1</version> </dependency> <!--Commons--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> ``` # 二、修改application.yml ``` spring: application: name: qingfeng datasource: dynamic: p6spy: true hikari: connection-timeout: 30000 max-lifetime: 1800000 max-pool-size: 15 min-idle: 5 connection-test-query: select 1 pool-name: QingfengHikariCP primary: base datasource: base: username: root password: Root@123 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/qingfeng_admin?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true mybatis-plus: type-aliases-package: com.qingfeng.*.entity.* mapper-locations: classpath:mapper/*/*.xml configuration: jdbc-type-for-null: null map-underscore-to-camel-case: false # 数据库下划线自动转驼峰标示关闭 global-config: banner: false ``` # 三、修改Application启动类 增加:@MapperScan(**"com.qingfeng.\*.mapper"**) ![](https://img.kancloud.cn/9f/6b/9f6bb64f4f05787b633c756accc5d341_1151x505.png) # 四、测试案例 ## demo案例模块搭建 新建demo测试案例模块,分别创建:DemoMapper.xml 、DemoMapper.java、IDemoService.java、DemoServiceImpl.java ![](https://img.kancloud.cn/61/29/6129327afd6da02cba4a25c43eec481c_1073x748.png) ## demo测试类 ``` package com.qingfeng; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.qingfeng.common.entity.Demo; import com.qingfeng.common.service.IDemoService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; /** * @author Administrator * @version 1.0.0 * @ProjectName qingfeng * @Description TODO * @createTime 2022年01月11日 15:39:00 */ @SpringBootTest public class DemoTest { @Autowired private IDemoService demoService; @Test void save() { Demo demo = new Demo(); demo.setId("1"); demo.setName("张三"); demo.setRemark("备注"); demo.setCreate_time("20220111"); demoService.save(demo); } @Test void update() { Demo demo = new Demo(); demo.setId("1"); demo.setName("李四"); demo.setRemark("备注"); demo.setCreate_time("20220111"); demoService.updateById(demo); } @Test void info() { Demo demo = demoService.getById("1"); System.out.println(demo); } @Test void list() { QueryWrapper wrapper = new QueryWrapper(); wrapper.like("name","李"); List<Demo> list = demoService.list(wrapper); for (Demo demo:list) { System.out.println(demo); } } } ```