互联网上已经存在了海量的基于SpringBoot的MybatisPlus教程,为什么还要创作本系列文章?
主要原因在于互联网上现有的关于Mybatis Plus的单篇文章关注的内容太多,过于复杂,不利于初学者快速入门,同时也非常不利于初学者抓住重点。
本系列教程重在突出“简明”二字,用最简洁的语言、最少的代码,展现最核心的技术和阐述最关键的问题,最大程度上删减不必要的信息。
目标本文的目标是:
快速搭建基于SpringBoot的Mybatis-Plus非WEB的命令行开发环境;
能够查询并显示数据库中用户表的所有数据;
创建数据库和项目所需表;
-- ------------------------------ Table structure for t_users-- ----------------------------DROP TABLE IF EXISTS `t_users`;CREATE TABLE `t_users` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;-- ------------------------------ Records of t_users-- ----------------------------INSERT INTO `t_users` VALUES ('1', 'abc', '123');
Idea中创建Maven项目;
pom.xml文件中添加项目所需依赖,并刷新Maven依赖;
<dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
创建数据库表对应的实体类;
import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;@Data@TableName("t_users")public class UserEntity { private Long id; private String username; private String password;}
创建实体类对应的mapper类;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;import edu.sctu.demo.mybatis.plus.model.UserEntity;public interface UserMapper extends BaseMapper<UserEntity> {}
进行MybatisPlus相关配置;
import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Configuration;@Configuration@MapperScan("edu.sctu.demo.mybatis.plus.mapper")public class MybatisPlusConfig {}
此处只需要配置mapper类的包路径即可;
编写SpringBoot启动类,并进行测试;
@SpringBootApplicationpublic class Application implements CommandLineRunner { @Autowired(required = false) private UserMapper userMapper; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { System.out.println(userMapper.selectList(null)); }}
测试结果如下所示:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.1) 2021-12-23 17:18:19.196 INFO 5996 --- [ main] edu.sctu.demo.mybatis.plus.Application : Starting Application using Java 1.8.0_131 on WIN-K5EIUKCMVNI with PID 5996 (E:\123\sctu-tools-java\mybatis-plus-demo\target\classes started by Administrator in E:\123\sctu-tools-java) 2021-12-23 17:18:19.198 INFO 5996 --- [ main] edu.sctu.demo.mybatis.plus.Application : No active profile set, falling back to default profiles: default _ _ |_ _ _|_. ___ _ | _ | | |\/|_)(_| | |_\ |_)||_|_\ / | 3.4.3.4 2021-12-23 17:18:20.238 INFO 5996 --- [ main] edu.sctu.demo.mybatis.plus.Application : Started Application in 1.447 seconds (JVM running for 1.977) 2021-12-23 17:18:20.267 INFO 5996 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2021-12-23 17:18:20.594 INFO 5996 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. [UserEntity(id=1, username=abc, password=123)] 2021-12-23 17:18:20.638 INFO 5996 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2021-12-23 17:18:20.640 INFO 5996 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. Process finished with exit code 0结语
本文展示了SpringBoot整合MybatisPlus所需要的最少代码,可以帮助初学者快速入门。