系列文章:Spring Boot 3.x 系列教程
文章目录
- 系列文章目录
- 简介
- 一、快速开始
- 1.数据库表和数据准备
- 2.新增项目&导入依赖
- 3.配置
- 4.开发编码
- 5.运行测试
- 二、注解
- 三、测试
- 四、CRUD
- Service CRUD
- Mapper CRUD
- 五、完整CRUD例子
简介
官网介绍:
MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
一、快速开始
数据使用mysql
1.数据库表和数据准备表
DROP TABLE IF EXISTS `tb_student`; CREATE TABLE `tb_student` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '姓名', `sex` int(1) DEFAULT NULL COMMENT '性别', `age` int(11) DEFAULT NULL COMMENT '年龄', `grade` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '年级', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
数据
INSERT INTO `tb_student` VALUES (1, '张三', 1, 14, '初中'); INSERT INTO `tb_student` VALUES (2, '李四', 1, 16, '高中'); INSERT INTO `tb_student` VALUES (3, '王五', 1, 20, '大学'); INSERT INTO `tb_student` VALUES (5, '小红', 0, 20, '大学'); INSERT INTO `tb_student` VALUES (8, '大牛', 1, 30, '博士');2.新增项目&导入依赖
使用 SpringInitializr新增项目
导入依赖
添加mybatisplus依赖
mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 3.5.1
在这里插入代码片3.配置
在application.yaml中添加数据库相关配置
spring:
datasource:
#数据库驱动完整类名
driver-class-name: com.mysql.jdbc.Driver
#数据库连接url
url: jdbc:mysql://127.0.0.1:3306/spring-boot-data-learn
#数据库用户名
username: root
#数据库密码
password: 123456
启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan(basePackages = "com.example.springbootmybatisplusdemo.mapper")
public class SpringBootMybatisplusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisplusDemoApplication.class, args);
}
}
4.开发编码
1.新建表对应的实体类Student
@Data
//表名与实体类名称不一致需要手动指定表名
@TableName("tb_student")
public class Student implements Serializable {
private Integer id;
private String name;
private Integer sex;
private Integer age;
private String grade;
}
2.mapper文件夹下新建StudentMapper接口
public interface StudentMapper extends BaseMapper5.运行测试{ }
@SpringBootTest
class SpringBootMybatisplusDemoApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
public void testSelectList() {
System.out.println(("----- selectAll method test ------"));
List userList = studentMapper.selectList(null);
userList.forEach(System.out::println);
}
}
控制台输出:
Student(id=1, name=张三, sex=1, age=14, grade=初中) Student(id=2, name=李四, sex=1, age=16, grade=高中) Student(id=3, name=王五, sex=1, age=20, grade=大学) Student(id=5, name=小红, sex=0, age=20, grade=大学) Student(id=8, name=大牛, sex=1, age=30, grade=博士)二、注解
注解详细内容参见官网:MybatisPlus注解
| 注解 | 功能 |
|---|---|
| @TableName | 标示实体对应的表 |
| @TableId | 主键注解 |
| @TableField | 字段 注解非主键 |
| @Version | 乐观锁注解 |
| @EnumValue | 普通枚举类注解(注解在枚举字段上) |
| @TableLogic | 表字段逻辑处理注解(逻辑删除) |
| @OrderBy | 内置 SQL 默认指定排序,优先级低于 wrapper 条件查询 |
自动导入 MyBatis-Plus 测试所需相关配置,通过 @MybatisPlusTest 注解快速配置测试类。
@MybatisPlusTest
//因为使用的是外部数据库,不是嵌入式数据库,因此需要加入此注解
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MybatisPlusTestDemo {
@Autowired
private StudentMapper studentMapper;
@Test
void testSelectAll() {
Student student = new Student();
student.setName("赵六");
student.setSex(1);
student.setAge(40);
student.setGrade("博士");
studentMapper.insert(student);
assertThat(student.getId()).isNotNull();
}
}
四、CRUD
官方文档地址:CRUD
Service CRUD官网定义:
通用 Service CRUD 封装IService 接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
增
// 插入一条记录(选择字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(CollectionentityList); // 插入(批量) boolean saveBatch(Collection entityList, int batchSize);
删
// 根据 entity 条件,删除记录 boolean remove(WrapperqueryWrapper); // 根据 ID 删除 boolean removeById(Serializable id); // 根据 columnMap 条件,删除记录 boolean removeByMap(Map columnMap); // 删除(根据ID 批量删除) boolean removeByIds(Collection extends Serializable> idList);
改
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset boolean update(WrapperupdateWrapper); // 根据 whereWrapper 条件,更新记录 boolean update(T updateEntity, Wrapper whereWrapper); // 根据 ID 选择修改 boolean updateById(T entity); // 根据ID 批量更新 boolean updateBatchById(Collection entityList); // 根据ID 批量更新 boolean updateBatchById(Collection entityList, int batchSize);
查
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map getMap(Wrapper queryWrapper);
// 根据 Wrapper,查询一条记录
V getObj(Wrapper queryWrapper, Function super Object, V> mapper);
//查询所有
List list();
// 查询列表
List list(Wrapper queryWrapper);
// 查询(根据ID 批量查询)
Collection listByIds(Collection extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection listByMap(Map columnMap);
// 查询所有列表
List
Mapper CRUD
官网定义:
通用 CRUD 封装BaseMapper (opens new window)接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
增
// 插入一条记录 int insert(T entity);
删
// 根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapperwrapper); // 删除(根据ID 批量删除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); // 根据 ID 删除 int deleteById(Serializable id); // 根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
改
/ 根据 whereWrapper 条件,更新记录 int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) WrapperwhereWrapper); // 根据 ID 修改 int updateById(@Param(Constants.ENTITY) T entity);
查
// 根据 ID 查询 T selectById(Serializable id); // 根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) Wrapper五、完整CRUD例子queryWrapper); // 查询(根据ID 批量查询) List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); // 根据 entity 条件,查询全部记录 List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 查询(根据 columnMap 条件) List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); // 根据 Wrapper 条件,查询全部记录 List > selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 entity 条件,查询全部记录(并翻页) IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录(并翻页) IPage > selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询总记录数 Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
entity:
@Data
@TableName("tb_student")
public class Student implements Serializable {
private Integer id;
private String name;
private Integer sex;
private Integer age;
private String grade;
}
mapper:
public interface StudentMapper extends BaseMapper{ }
service接口:
public interface IStudentService extends IService{ }
service实现:
@Service("studentService")
public class StudentServiceImpl extends ServiceImpl implements IStudentService {
}
测试:
@Autowired
private IStudentService studentService;
@Test
public void testCRUD(){
Student student= studentService.getById(5);
System.out.println(student);
studentService.removeById(5);
Student student1 = new Student();
student.setName("赵六");
student.setSex(1);
student.setAge(40);
student.setGrade("博士");
studentService.save(student1);
}
项目结构
源代码



