文章目录
- 🎄环境准备
- ⭐导入依赖
- ⭐写入User类
- ⭐配置启动类
- ⭐创建UserDao 的 MyBatis Mapper 接口,用于定义数据库访问操作
- ⭐创建配置文件
- 🛸创建测试类MpATest.java
- 🍔范围查询
- ⭐eq
- ⭐between
- ⭐gt
- 🍔模糊匹配
- ⭐like
- 🍔分组查询
- 🛸更多查询条件
🎄环境准备
搭建好配置环境后,创建这几个包和类
准备数据库和表
⭐导入依赖
pom.xml
<?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 http://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><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>MybatisPlusDemo</artifactId><version>1.0-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><version>3.1.2</version></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><version>8.0.25</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>3.1.4</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin></plugins></build>
</project>
⭐写入User类
User.java
注意要和数据库表中的数据对应
package com.example.domain;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("tb_abc1") //数据库表名public class User {private Integer id;private String name;private Integer money;
}
⭐配置启动类
MybatisplusApplication.java
注意是启动springboot的类,我们测试的时候使用test进行测试,在运行该应用程序时,Spring Boot 会扫描并加载该类,并自动配置 MyBatisPlus 相关的组件,根据注解中的配置信息进行自动装配。
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MybatisplusApplication {public static void main(String[] args) {SpringApplication.run(MybatisplusApplication.class,args);}
}
⭐创建UserDao 的 MyBatis Mapper 接口,用于定义数据库访问操作
该接口继承 BaseMapper 接口,表示它是一个基于 MyBatis Plus 框架的 Mapper 接口。在这个示例中,由于 User 类已经使用注解标注了 @TableName(“user”),因此这里可以直接使用 User 类作为泛型参数。
通过定义这样的 Mapper 接口,我们可以更加方便地使用 MyBatis 进行数据库的增删改查等操作。同时,MyBatis Plus 提供的基础 Mapper 接口还内置了许多常见的数据库操作方法,如插入、更新、查询等方法,可以帮助我们更加高效地进行数据库相关操作。
package com.example.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserDao extends BaseMapper<User> {
}
⭐创建配置文件
在resources里面创建application.yml
# dataSource
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/abc?serviceTimezone=Asia/Shanghaiusername: rootpassword: 1234main:banner-mode: off
# mp日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:banner: false
在resources里面创建logback.xml
减少控制台对于的代码,使控制台打印的东西看起来更加清爽
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
</configuration>
🛸创建测试类MpATest.java
下面我们写入代码来展示测试类的测试效果
🍔范围查询
⭐eq
等于
package com.example;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MpATest {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){//相当于一个登录操作LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<User>();//等同于=lqw.eq(User::getName,"张三").eq(User::getId,'1');//这里查询一个对象,使用selectone就行了User loginUser=userDao.selectOne(lqw);System.out.println(loginUser);}
}
⭐between
在什么什么之间
package com.example;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MpATest {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<User>();//betweenlqw.between(User::getId,0,1);List<User> userList=userDao.selectList(lqw);System.out.println(userList);}
}
⭐gt
大于
package com.example;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MpATest {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<User>();//gtlqw.gt(User::getId,1);List<User> userList=userDao.selectList(lqw);System.out.println(userList);}
}
gt 和 lt ,它们分别代表了“大于”和“小于”的含义
le 和 ge ,它们分别代表了“小于等于”和“大于等于”的含义
🍔模糊匹配
⭐like
类似于mysql里面的like操作
package com.example;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MpATest {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<User>();//likelqw.like(User::getMoney,"2");List<User> userList=userDao.selectList(lqw);System.out.println(userList);}
}
还有likeRight,likeLeft
likeRight
,类似于MySQL的like%
likeLeft
,类似于MySQL的%like
🍔分组查询
分组查询聚合函数
package com.example;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MpATest {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){QueryWrapper<User> qw=new QueryWrapper<User>();qw.select("id","count(*) as nums");qw.groupBy("id");List<Map<String,Object>> maps=userDao.selectMaps(qw);System.out.println(maps);}
}
🛸更多查询条件
更多查询条件请看
https://mybatis.plus/guide/wrapper.html#abstractwrapper