更新時間:2023年05月11日18時11分 來源:傳智教育 瀏覽次數(shù):
增刪改查四個操作中,查詢是非常重要的也是非常復(fù)雜的操作,MyBatisPlus將書寫復(fù)雜的SQL查詢條件進(jìn)行了封裝,使用編程的形式完成查詢條件的組合。 在查詢所有和分頁查詢的時候,都能看到過一個Wrapper類,這個類就是用來構(gòu)建查詢條件的,如下圖所示:
環(huán)境構(gòu)建
在構(gòu)建條件查詢之前,我們先來準(zhǔn)備下環(huán)境: 創(chuàng)建一個SpringBoot項目和pom.xml中添加對應(yīng)的依賴。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> </parent> <groupId>com.itheima</groupId> <artifactId>mybatisplus_02_dql</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactID> </plugin> </plugins> </build> </project>
編寫UserDao接口
@Mapper public interface UserDao extends BaseMapper<User> { }
編寫模型類
@Data public class User { private Long id; private String name; private String password; private Integer age; private String tel; }
編寫引導(dǎo)類
@SpringBootApplication public class Mybatisplus02DqlApplication { public static void main(String[] args) { SpringApplication.run(Mybatisplus02DqlApplication.class, args); } }
編寫配置文件
# dataSource spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC username: root password: root # mp日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
編寫測試類
@SpringBootTest class Mybatisplus02DqlApplicationTests { @Autowired private UserDao userDao; @Test void testGetAll(){ List<User> userList = userDao.selectList(null); System.out.println(userList); } }
最終創(chuàng)建的項目結(jié)構(gòu)為:
測試的時候,控制臺打印的日志比較多,速度有點(diǎn)慢而且不利于查看運(yùn)行結(jié)果,所以接下來我們把 這個日志處理下:
取消初始化spring日志打印,resources目錄下添加logback.xml,名稱固定,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> </configuration>
application.yml添加如下內(nèi)容
# mybatis-plus日志控制臺輸出 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: banner: off # 關(guān)閉mybatisplus啟動圖標(biāo)
取消SpringBoot的log打印
application.yml添加如下內(nèi)容:
spring: main: banner-mode: off # 關(guān)閉SpringBoot啟動圖標(biāo)(banner)