191231-SpringBoot 系列教程 MybatisPlus 整合篇
前面介绍了 SpringBoot 整合 Mybatis 实现 db 的增删改查操做,分别给出了 xml 和注解两种实现 mapper 接口的方式;虽然注解方式干掉了 xml 文件,可是使用起来并不优雅,本文将介绍 mybats-plus 的使用 case,简化常规的 CRUD 操做java
<!-- more -->mysql
本文使用 SpringBoot 版本为 2.2.1.RELEASE
, mybatis-plus 版本为3.2.0
,数据库为 mysql 5+git
推荐使用官方的教程来建立一个 SpringBoot 项目; 若是直接建立一个 maven 工程的话,将下面配置内容,拷贝到你的pom.xml
中github
mybatis-spring-boot-starter
,能够减小使人窒息的配置<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot-local</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
在 application.yml
配置文件中,加一下 db 的相关配置spring
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password:
接下来准备一个测试表(依然借用以前 db 操做系列博文中的表结构),用于后续的 CURD;表结果信息以下sql
DROP TABLE IF EXISTS `money`; CREATE TABLE `money` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名', `money` int(26) NOT NULL DEFAULT '0' COMMENT '有多少钱', `is_deleted` tinyint(1) NOT NULL DEFAULT '0', `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立时间', `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
mybatis-plus 与 mybatis 的使用姿式有一些区别,下面为不借助generator
直接手撸代码的 case数据库
建立表对应的 PO 对象: MoneyPo
mybatis
@Data public class MoneyPo { private Integer id; private String name; private Long money; private Integer isDeleted; private Timestamp createAt; private Timestamp updateAt; }
表的操做接口,与 mybatis 不一样的是这个接口继承BaseMapper
以后,就自带了单表的 CURD 操做接口了,基本上不须要定义额外的接口,就能够实现 db 交互app
public interface MoneyMapper extends BaseMapper<MoneyPo> { }
BaseMapper
的参数为表对应的 PO 对象上面完成以后,整合过程基本上就完了,没错,就这么简单,接下来咱们进入测试环节dom
首先是启动类,咱们加上了@MapperScan
注解,这样在 DAO 接口上就不须要添加@Mapper
注解了
@SpringBootApplication @MapperScan("com.git.hui.boot.mybatisplus.mapper") public class Application { public Application(MoneyRepository repository) { repository.testMapper(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
关于测试 case,下面会演示 CRUD 四种基本的操做 case,由于本文重点不是介绍 mybatis-plus 的用法,对于下面代码有疑问的能够查看官方文档: https://mp.baomidou.com/guide/
@Component public class MoneyRepository { @Autowired private MoneyMapper moneyMapper; private Random random = new Random(); public void testDemo() { MoneyPo po = new MoneyPo(); po.setName("mybatis plus user"); po.setMoney((long) random.nextInt(12343)); po.setIsDeleted(0); // 添加一条数据 moneyMapper.insert(po); // 查询 List<MoneyPo> list = moneyMapper.selectList(new QueryWrapper<MoneyPo>().lambda().eq(MoneyPo::getName, po.getName())); System.out.println("after insert: " + list); // 修改 po.setMoney(po.getMoney() + 300); moneyMapper.updateById(po); System.out.println("after update: " + moneyMapper.selectById(po.getId())); // 删除 moneyMapper.deleteById(po.getId()); // 查询 Map<String, Object> queryMap = new HashMap<>(2); queryMap.put("name", po.getName()); System.out.println("after delete: " + moneyMapper.selectByMap(queryMap)); } }
输出结果
尽信书则不如,以上内容,纯属一家之言,因我的能力有限,不免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛