做国内网站阿里云怎么样,semcms外贸网站管理系统,福建论坛,南京网站优化哪家好一#xff0c;初始 MyBatis-Plus 文章目录 一#xff0c;初始 MyBatis-Plus1. MyBatis-Plus 的概述2. 入门配置第一个 MyBatis-Plus 案例3. 补充说明#xff1a;3.1 通用 Mapper 接口介绍3.1.1 Mapper 接口的 “增删改查”3.1.1.1 查询所有记录3.1.1.2 插入一条数据3.1.1.3 …一初始 MyBatis-Plus 文章目录 一初始 MyBatis-Plus1. MyBatis-Plus 的概述2. 入门配置第一个 MyBatis-Plus 案例3. 补充说明3.1 通用 Mapper 接口介绍3.1.1 Mapper 接口的 “增删改查”3.1.1.1 查询所有记录3.1.1.2 插入一条数据3.1.1.3 删除一条数据3.1.1.4 更新一条数据3.1.1.5 查询一条数据 3.2 通用 service接口介绍3.2.1 Service 接口的“增删改查”3.2.1.1 添加一条记录3.2.1.2 删除一条记录3.2.1.3 修改一条记录3.2.1.4 查询一条记录 3.3 自定义方法3.3.1 自定义Mapper接口方法 4. 总结6. 最后 1. MyBatis-Plus 的概述
学习 MyBatis -Plus 之前我们可以先来简单的稍微的回顾一下MyBatis 框架的一些问题
这里只是简单的回顾关于MyBatis 的更多详细内容大家可以移步至✏️✏️✏️ MyBatis_ChinaRainbowSea的博客-CSDN博客
我们来简单思考一下MyBatis 框架的开发效率怎么样 开发效率也就是我们使用这款框架开发的速度快不快是否简单好用易上手。从这个角度思考每当我们需要编写一个SQL需求的时候我们需要做几步 Mapper 接口提供一个抽象方法。Mapper 接口对应的映射配置文件提供对应的标签和SQL语句在 Service 中依赖 Mapper 实例对象调用 Mapper 实例中的方法在Controller 中依赖 Service 实例对象调用 Service 实例中的方法 通过上面的发现对于一个SQL需求无论是单表还是多表我们是需要完成如上几步才能实现SQL需求的开发。 但是在开发中有一些操作是通过逻辑这些通用逻辑是可以被简化的例如 对于 dao 是否可以由框架帮我们提供好单表的 Mapper 抽象方法和对应的SQL实现不需要程序员去实现这些。对于 service使用可以有框架直接帮我们提供好一些 serivce 的抽象方法和对应的实现不需要程序员去实现这些。一些其它的企业开发中所需要的操作 分析到这里我们发现其实核心框架并没有发生变化依赖还是 Mybatis只不过我们希望对于 MyBatis 进行一些封装和优化让他更加的好用更加的易用。 所以MyBatis Plus 它就来了它是 MyBatis 的一款增强工具。 MybatisPlus从名称上来看我们就发现他和Mybatis长得很像其实MybatisPlus就是Mybatis的孪生兄弟。学习MyBatis-Plus 首先我们来看看它的官网地址是https://baomidou.com/ MyBatis-Plus (opens new window)简称 MP是一个 MyBatis (opens new window)的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。 愿景
我们的愿景是成为 MyBatis 最好的搭档就像 魂斗罗 中的 1P、2P基友搭配效率翻倍。 MyBatis-Plus的特性说明
无侵入只做增强不做改变引入它不会对现有工程产生影响如丝般顺滑损耗小启动即会自动注入基本 CURD性能基本无损耗直接面向对象操作强大的 CRUD 操作内置通用 Mapper、通用 Service仅仅通过少量配置即可实现单表大部分 CRUD 操作更有强大的条件构造器满足各类使用需求支持 Lambda 形式调用通过 Lambda 表达式方便的编写各类查询条件无需再担心字段写错支持主键自动生成支持多达 4 种主键策略内含分布式唯一 ID 生成器 - Sequence可自由配置完美解决主键问题支持 ActiveRecord 模式支持 ActiveRecord 形式调用实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作支持全局通用方法注入 Write once, use anywhere 内置代码生成器采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码支持模板引擎更有超多自定义配置等您来使用内置分页插件基于 MyBatis 物理分页开发者无需关心具体操作配置好插件之后写分页等同于普通 List 查询分页插件支持多种数据库支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库内置性能分析插件可输出 SQL 语句以及其执行时间建议开发测试时启用该功能能快速揪出慢查询内置全局拦截插件提供全表 delete 、 update 操作智能分析阻断也可自定义拦截规则预防误操作
MyBatis-Plus 的框架结构 什么是Mybatis框架呢他是一个持久层框架目的是简化持久层的开发。在这里我们就使用springboot整合Mybatis实现Mybatis框架的搭建。 特别说明这里这个是 IDEA 2021 创建项目的操作步骤 我们首先创建一个空的工程 工程名称是mp 创建springboot模块 这里我们选择springboot2.7.8的版本并不勾选依赖随后通过pom.xml手动添加 特别说明下面这个是IDEA 2024 的操作配置 2. 入门配置第一个 MyBatis-Plus 案例
第一步 创建数据库 mybatisplus 这些测试表是来自于 MyBatis-Plus 官网教学的内容大家也可以移步去看看✏️✏️✏️ https://baomidou.com/getting-started/ 现有一张 User 表其表结构如下
idnameageemail1Jone18test1baomidou.com2Jack20test2baomidou.com3Tom28test3baomidou.com4Sandy21test4baomidou.com5Billie24test5baomidou.com
其对应的数据库 Schema 脚本如下
DROP TABLE IF EXISTS user;CREATE TABLE user
(id BIGINT NOT NULL COMMENT 主键ID,name VARCHAR(30) NULL DEFAULT NULL COMMENT 姓名,age INT NULL DEFAULT NULL COMMENT 年龄,email VARCHAR(50) NULL DEFAULT NULL COMMENT 邮箱,PRIMARY KEY (id)
);其对应的数据库 Data 脚本如下
DELETE FROM user;INSERT INTO user (id, name, age, email) VALUES
(1, Jone, 18, test1baomidou.com),
(2, Jack, 20, test2baomidou.com),
(3, Tom, 28, test3baomidou.com),
(4, Sandy, 21, test4baomidou.com),
(5, Billie, 24, test5baomidou.com);第二步 引入相关依赖在 pom.xml 文件当中配置我们所需要的 jar 依赖。 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.3/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.rainbowsea/groupIdartifactIdmp02/artifactIdversion0.0.1-SNAPSHOT/versionnamemp02/namedescriptionmp02/descriptionurl/licenseslicense//licensesdevelopersdeveloper//developersscmconnection/developerConnection/tag/url//scmpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!-- spring boot web 依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- mysql 驱动依赖--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency!-- lombok 的依赖--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency!-- druid--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.8/version/dependency!-- mybatis-plus 的依赖--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.3/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/buildrepositoriesrepositoryidspring-snapshots/idnameSpring Snapshots/nameurlhttps://repo.spring.io/snapshot/urlreleasesenabledfalse/enabled/releases/repository/repositoriespluginRepositoriespluginRepositoryidspring-snapshots/idnameSpring Snapshots/nameurlhttps://repo.spring.io/snapshot/urlreleasesenabledfalse/enabled/releases/pluginRepository/pluginRepositories/project 特别说明这里我们将 Spring boot 改为2.5.3 版本的。 在MyBatis-Plus 当中是包含了MyBatis 的依赖的。所以引入了 MyBatis-Plus 也就引入了MyBatis了。 第三步 编写该项目/模块的场景启动器 第四步 根据数据表创建对应的实体类 package com.rainbowsea.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;Data
NoArgsConstructor
AllArgsConstructor
public class User {private Long id;private String name;private Integer age;private String email;
}
第五步 切换数据库连接池为 Druid 。这里我们编写配置类的方式切换数据库连接池。
在 resoucre 类路径下创建名为 applicaiton.yaml 的配置文件其中编写连接数据库的配置信息。 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisplus?serverTimezoneUTCcharacterEncodingutf8useUnicodetrueuseSSLfalseusername: rootpassword: MySQL123
编写配置类切换数据库为 Druid package com.rainbowsea.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/*** 配置切换Druid 数据库连接池*/
Configuration // 标志配置类
public class DruidDataSourceConfig {Bean // 被 Spring ioc 容器管理起来ConfigurationProperties(value spring.datasource)public DataSource getDataSource() {DruidDataSource druidDataSource new DruidDataSource();return druidDataSource;}
} 特别说明关于spring boot 中切换数据库连接池的详细内容大家可以移步至✏️✏️✏️ 十六Spring Boot 整合 Druid 以及使用 Druid 监控功能-CSDN博客 测试看看我们是否成功切换为了 Druid 数据库连接池。 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate JdbcTemplate jdbcTemplate;Testvoid contextLoads() {System.out.println(jdbcTemplate.getDataSource().getClass());}
}第六步 后面的都是对应 MyBatis-Plus 的内容上的重点内容。
编写 Mapper接口 package com.rainbowsea.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rainbowsea.bean.User;
import org.apache.ibatis.annotations.Mapper;Mapper // 被Spring Boot 扫描到
public interface UserMapper extends BaseMapperUser {} 我们的自己编写的 Mapper 接口 extends(继承) BaseMapper 接口就会拥有了其该接口下的方法。 第七步 编写 Service 接口 package com.rainbowsea.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.rainbowsea.bean.User;import java.util.List;public interface UserService extends IServiceUser {// 当 IService 当中提供的方法不能满足我们的业务需要的时候// 我们可以自定义方法。// 自定义的方法ListUser selectAll();
} 第八步 编写ServiceImpl package com.rainbowsea.service.Impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;Service
public class UserServiceImpl extends ServiceImplUserMapper,Userimplements UserService {//ResourceAutowired // 自动装配private UserMapper userMapper;// 对自定义方法的重写实现Overridepublic ListUser selectAll() {return userMapper.selectList(null);}
} 它们之间的关系结构如下 **第九步**编写Controller 控制器 package com.rainbowsea.controller;import com.rainbowsea.bean.User;
import com.rainbowsea.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;RestController
public class UserController {Resourceprivate UserService userService;GetMapping(value {/selectList})public ListUser selectList() {return userService.selectAll();}
}
第十一步 打开浏览器运行测试。 3. 补充说明
3.1 通用 Mapper 接口介绍
有关于 Mapper 接口之前我们已经看到了我们自己编写的Mapper接口继承 自己 BaseMapper 接口由 BaseMapper 接口提供了很多单表的增删改查 相关的操作方法在入门案例种我们测试了查询所有的操作。我们介绍一些简单的Mapper 接口中的方法主要是感觉一下。Mapper接口中对于单表的增删改查的操作都有涉及更加高级的一些操作。 3.1.1 Mapper 接口的 “增删改查”
3.1.1.1 查询所有记录 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate UserMapper userMapper;// 查询所有Testvoid selectList() {ListUser users userMapper.selectList(null);for (User user : users) {System.out.println(user);}}
}3.1.1.2 插入一条数据 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate UserMapper userMapper;// 简单添加Testvoid insert() {User user new User();user.setId(6L);user.setAge(18);user.setName(Lihua);user.setEmail(test6baomidou.com);userMapper.insert(user);}
}3.1.1.3 删除一条数据 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate UserMapper userMapper;// 简单删除Testvoid deleteOne() {userMapper.deleteById(4L);}}3.1.1.4 更新一条数据 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate UserMapper userMapper;// 简单更新Testvoid updateById() {User user new User();user.setId(2L);user.setAge(18);user.setName(李华);user.setEmail(lihuabaomidou.com);userMapper.updateById(user);}}3.1.1.5 查询一条数据 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate UserMapper userMapper;// 简单查询Testvoid selectById() {User user userMapper.selectById(6L);System.out.println(user);}}3.2 通用 service接口介绍
除了Mapper接口MybatisPlus还提供了IService接口和对应的实现类ServiceImpl该实现类已经提供好了一些对应的Service相关的方法在某些场景下我们可以直接使用ServiceImpl提供的方法实现对应的功能。
IService接口
IService接口中包含了service相关的一些增删改查方法
ServiceImpl实现类
ServiceImpl实现类提供了service相关的增删改查方法的实现 UserService接口继承自IService接口 UserServiceImpl类继承ServiceImplUserMapper,User 注入UserService对象测试相关方法 3.2.1 Service 接口的“增删改查”
3.2.1.1 添加一条记录 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Autowiredprivate UserService userService;// UserService 添加Testvoid insertService() {User user new User();user.setId(7L);user.setAge(18);user.setName(Tom);user.setEmail(Tombaomidou.com);userService.save(user);}}3.2.1.2 删除一条记录 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Autowiredprivate UserService userService;// UserService 删除Testvoid deleteServie() {userService.removeById(2L);}
}3.2.1.3 修改一条记录 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Autowiredprivate UserService userService;// userService 修改Testvoid updateService() {User user new User();user.setId(3L);user.setAge(22);userService.updateById(user);}}3.2.1.4 查询一条记录 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Autowiredprivate UserService userService;// UserService 查询Testvoid selectService() {ListUser users userService.selectAll();for (User user : users) {System.out.println(user);}}
}3.3 自定义方法
MybatisPlus除了给我们提供了这些丰富的接口方法以外对于我们自己的需求也可以编写自定义的接口方法我们通过自己编写SQL语句的形式实现想要的SQL需求
3.3.1 自定义Mapper接口方法
Mapper接口中提供抽象方法 package com.rainbowsea.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rainbowsea.bean.User;
import org.apache.ibatis.annotations.Mapper;Mapper // 被Spring Boot 扫描到
public interface UserMapper extends BaseMapperUser {// 自定义方法User selectByName(String name);}
提供映射配置文件提供对应的SQL语句 注意对应的映射的 SQL 配置文件的路径/包要一致以及名称也要一致才行。 ?xml version1.0 encodingUTF-8? !-- 这句不要动,表示xml的版本,以及读取的编码 --!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.rainbowsea.mapper.UserMapper select idselectByName resultTypecom.rainbowsea.bean.User parameterTypestringselect id,age,name,emailfrom userwhere name #{value}/select
/mapper测试自定义的Mapper接口方法 import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;SpringBootTest(classes Mp02Application.class) // Spring Boot 当中的测试必须要有 项目的启动场景不然无法测试报错
class Mp02ApplicationTests {Resourceprivate UserMapper userMapper;// 测试自定义方法Testvoid myMethod() {User tom userMapper.selectByName(Lihua);System.out.println(tom);}
}4. 总结
MyBatis-Plus官网地址https://baomidou.com/MyBatis-Plus (opens new window)简称 MP是一个 MyBatis (opens new window)的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。记忆理解 MyBatis-Plus 内置的 Mapper 接口和 Service 接口 6. 最后 “在这个最后的篇章中我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底继续在其他的领域奋斗。感谢你们我们总会在某个时刻再次相遇。”