SpringBoot 整合Mybatis 无XML版
宗旨
本文介绍SpringBoot整合Mybatis,简单实现分页和返回自增主键功能。旨在提升自升能力,同时,对阅读本文的小伙伴提供一定帮助,有错误的地方欢迎指出。如有转载,请标明原文地址。
博客连接:https://blog.csdn.net/qq_36248731,原创文章持续更新中,欢迎大家阅读、评论、收藏、转发。生命不息,学习不止!
环境
- JDK 1.8
- MySql 5.7
- SpringBoot项目
工具
- IDEA 2019
- Navicat
目录结构
MAVEN依赖
<!-- springboot starter-parent自动找到最合适的版本 因此不用加version
starter-web 提供了MVC AOP等常用的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加mysql 依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mysql 数据库驱动. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--
spring-boot mybatis依赖:
请不要使用1.0.0版本,因为还不支持拦截器插件,
-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--
MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
将其作为一个plugin装入到SqlSessionFactory中。
Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。
Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper