14-SpringBoot3入门-MyBatis-Plus之CRUD

发布于:2025-04-03 ⋅ 阅读:(18) ⋅ 点赞:(0)

1、整合

13-SpringBoot3入门-整合MyBatis-Plus-CSDN博客

 2、表

3、crud

package com.sgu;

import com.sgu.mapper.UserMapper;
import com.sgu.pojo.User;
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 中瑞
 * @Date 2025/4/2 12:23
 */

@SpringBootTest
public class SampleTest {
	/**
	 * mybatis-plus CRUD
	 */
	@Autowired
	private UserMapper userMapper;

	// 增
	@Test
	public void insert(){
		User user = new User();
		user.setId(5L);
		user.setName("Tom");
		user.setAge(17);
		user.setEmail("tom@qq.com");
		userMapper.insert(user);
	}

	// 根据id删
	@Test
	public void delete(){
		userMapper.deleteById(5);
	}

	// 根据id改
	@Test
	public void update(){
		User user = new User();
		user.setId(5L);
		user.setName("Marry");
		user.setEmail("marry@qq.com");
		userMapper.updateById(user);
	}

	// 根据id查询
	@Test
	public void queryById(){
		User user = userMapper.selectById(5);
		System.out.println(user);
	}

	// 查询所有
	@Test
	public void queryAll(){
		List<User> users = userMapper.selectList(null);
		for (User u : users){
			System.out.println(u);
		}
	}
}

4、参考

153-mybatis-plus-基于baseMapper的curd增强_哔哩哔哩_bilibili


网站公告

今日签到

点亮在社区的每一天
去签到