快速配置
依赖注入
注意:springboot3和springboot2支持的版本不同,下面是springboot3版本
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
mapper接口文件扫描
package com.itheima.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//扫描mybatis mapper接口
@MapperScan("com.itheima.mybatisplus.mapper")
public class MyBatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisPlusApplication.class, args);
}
}
数据库配置
在配置文件中配置数据库文件
# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=***********
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
数据测试
查询测试
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.isTrue(5 == userList.size(), "");
userList.forEach(System.out::println);
}
测试结果
基本增删改查操作
插入操作
@Test
public void testInsert() {
User user = new User();
user.setName("赵前");
user.setId(8L);
user.setAge(21);
user.setEmail("2622092872@qq.com");
userService.save(user);
System.out.println("success");
}
批量插入操作
@Test
public void listSave(){
// 假设有一组 User 实体对象
List<User> users = Arrays.asList(
new User(9L, "Tom", 20, "123456@qq.com"),
new User(10L, "Jerry", 21, "123456@qq.com"),
new User(11L, "Lily", 22, "123456@qq.com")
);
// 使用默认批次大小进行批量插入
boolean result = userService.saveBatch(users); // 调用 saveBatch 方法,默认批次大小
if (result) {
System.out.println("Users saved successfully.");
} else {
System.out.println("Failed to save users.");
}
}
注意:saveBatch还有一个参数为批次量大小,就是一次性插入几条数据
更新或者插入操作
//更新操作
@Test
public void testUpdate() {
User user = new User();
user.setId(6L);
user.setName("张三");
user.setAge(22);
user.setEmail("2622792872@qq.com");
Boolean result = userService.saveOrUpdate(user);
if (result){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
删除操作
根据id删除
@Test
public void testDelete() {
Boolean result = userService.removeById(6L);
if (result){
System.out.println("删除成功");
}
}
根据条件构造器删除
//queryWrapper删除操作
@Test
public void testDeleteByCondition() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", 7L);
Boolean result = userService.remove(queryWrapper);
if (result){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
更新操作
单个对象更新
@Test
public void testUpdateById() {
User user = new User();
user.setId(7L);
user.setName("张三");
boolean result = userService.updateById(user);
if (result){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
多个对象批量更新
//批量更新操作
@Test
public void testUpdateBatchById() {
List<User> users = Arrays.asList(
new User(8L, "赵新雨", 20, "123456@qq.com"),
new User(9L, "杨攀", 21, "123456@qq.com"),
new User(10L, "周宇涵", 22, "123456@qq.com")
);
boolean result = userService.updateBatchById(users);
if (result){
System.out.println("批量更新成功");
}else {
System.out.println("批量更新失败");
}
}
查询操作
查询一条记录
//查询操作
@Test
public void testSelectById() {
User user = userService.getById(7L);
System.out.println(user);
}
查询所有记录
@Test
public void testSelectByCondition() {
List<User> userList = userService.list();
userList.forEach(System.out::println);
}