教育行业A股IPO第一股(股票代码 003032)

全国咨询/投诉热线:400-618-4000

MyBatisPlus中条件查询怎么用Wrapper来构建?

更新时间:2023年05月11日18时11分 来源:大阳城app官网入口 浏览次数:

增删改查四个操作中,查询是非常重要的也是非常复杂的操作,MyBatisPlus将书写复杂的SQL查询条件进行了封装,使用编程的形式完成查询条件的组合。 在查询所有和分页查询的时候,都能看到过一个Wrapper类,这个类就是用来构建查询条件的,如下图所示:

条件查询类

环境构建

在构建条件查询之前,我们先来准备下环境: 创建一个SpringBoot项目和pom.xml中添加对应的依赖。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.0
    
    com.itheima
    mybatisplus_02_dql
    0.0.1-SNAPSHOT
    
        1.8
    
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            com.alibaba
            druid
            1.1.16
        

        
            mysql
            mysql-connector-java
            runtime
        

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

        
            org.projectlombok
            lombok
        

        

        
            
                
                     org.springframework.boot
                     spring-boot-maven-plugin
                 
              
          
      

编写UserDao接口

@Mapper   
public  interface UserDao  extends  BaseMapper  {   
}

编写模型类

@Data
public class User {
    private Long id;
    private String name;
    private String password;
    private Integer age;
    private String tel;
 }

编写引导类

@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 userList = userDao.selectList(null);
         System.out.println(userList);
     }
 }

最终创建的项目结构为:

1683799328371_最终构建的项目.png

测试的时候,控制台打印的日志比较多,速度有点慢而且不利于查看运行结果,所以接下来我们把 这个日志处理下:

取消初始化spring日志打印,resources目录下添加logback.xml,名称固定,内容如下:

 
 

application.yml添加如下内容

# mybatis-plus日志控制台输出
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    banner: off # 关闭mybatisplus启动图标

取消SpringBoot的log打印

1683799589609_27.png

application.yml添加如下内容:

spring:
  main:
    banner-mode: off # 关闭SpringBoot启动图标(banner)

0 分享到:
和我们在线交谈!
Baidu
sogou