Mybatis Plus(简称 MP)是一个Mybatis(opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
今天看下在SpringBoot中简单的使用MybatisPlus。
一、引入pom依赖
<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/></parent><groupId>com.xing</groupId><artifactId>springBootMybatisPlus</artifactId><version>0.0.1-SNAPSHOT</version><name>springBootMybatisPlus</name><description>MybatisPlusDemoprojectforSpringBoot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.15</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build></project>
二、直接使用上篇MybatisGenerator自动生成的目录结构
当然也可以自己建立,上图中,我们并没有在mapper、service等文件中编写其他的东西,注意不一样的地方:
1.mapper继承了com.baomidou.mybatisplus.core.mapper.BaseMapper2.service继承了com.baomidou.mybatisplus.extension.service.IService3.UserServiceImpl继承了com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
Java中,子类可以继承父类的所有,那么我们就可以直接使用父类中m默认提供的API,不用去逐个编写了。
三、注入到controller中使用
packagecom.xing.springBootMybatisPlus.controller; importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper; importcom.xing.springBootMybatisPlus.model.User; importcom.xing.springBootMybatisPlus.service.IUserService; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.stereotype.Controller; importjava.util.ArrayList; importjava.util.List; importjava.util.Map; /*** 前端控制器* @author x* @since 2021-11-19*/"/user") (publicclassUserController { finalIUserServiceiUserService; publicUserController(IUserServiceiUserService) { this.iUserService=iUserService; } /*** test* @return ok*/"/test") (publicStringtest(){ Useruser=newUser(); user.setId(22); user.setUserName("test"); user.setRealName("测试"); // 新增iUserService.save(user); Userresult1=iUserService.getOne(newQueryWrapper<User>().lambda().eq(User::getId,user.getId())); System.out.println("新增后数据:"+result1); // 修改user.setPassWord("密码"); iUserService.updateById(user); Userresult2=iUserService.getById(user.getId()); System.out.println("修改后数据:"+result2); // 获取全部数据List<User>result3=iUserService.list(); System.out.println("获取全部数据:"+result3); // 删除数据iUserService.removeById(user.getId()); // 获取全部数据List<Map<String, Object>>result4=iUserService.listMaps(); System.out.println("再次获取全部map数据:"+result4); // 再次新增iUserService.saveOrUpdate(user); // 获取全部数据List<Integer>list=newArrayList<>(); list.add(user.getId()); List<User>result5=iUserService.listByIds(list); System.out.println("再次获取全部map数据:"+result5); // 再次删除iUserService.removeById(user); // 新增多个Useruser2=newUser(); user2.setId(23); user2.setUserName("test2"); user2.setRealName("测试2"); List<User>userList=newArrayList<>(); userList.add(user); userList.add(user2); iUserService.saveBatch(userList); //QueryWrapper<User>userQueryWrapper=newQueryWrapper<>(); userQueryWrapper.eq(true, "user_name", user2.getUserName()); List<User>result6=iUserService.list(userQueryWrapper); System.out.println("再次获取全部数据:"+result6); return"ok"; } }
四、测试结果
还有一些配置:
server.port=8080spring.datasource.username=rootspring.datasource.password=rootspring.datasource.url=jdbc:mysql://localhost:3306/xinghua?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverlogging.level.com.xing.springBootMybatisPlus=debugmybatis-plus.check-config-location=truemybatis-plus.configuration.cache-enabled=falsemybatis-plus.configuration.map-underscore-to-camel-case=truemybatis-plus.configuration.auto-mapping-behavior=fullmybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xmlmybatis-plus.global-config.db-config.logic-not-delete-value=1mybatis-plus.global-config.db-config.logic-delete-value=0
总结:
MybatisPlus就是默认给我们提供了一些API,看上图特性中,还提供和了很多分页、主键生成等功能,可用的时候可以去官网查看例子。
点击原文或者直接去官网:
https://mp.baomidou.com/guide/
END