MyBatis-Plus
MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官方文档地址:https://mybatis.plus/guide/
环境介绍
环境要素
版本
maven
3.3.3
jdk
1.8.0_1.81
编译
idea
springboot
2.1.7.RELEASE
系统
win10
快速开始
添加依赖
pom.xml中引入以下的包
com.baomidou
mybatis-plus-boot-starter
3.2.0
mysql
mysql-connector-java
8.0.17
runtime
org.projectlombok
lombok
1.18.8
org.springframework.boot
spring-boot-starter
2.1.7.RELEASE
org.springframework.boot
spring-boot-starter-test
2.1.7.RELEASE
test
配置
在application.properties中添加数据库配置
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/db_demo?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username = root
spring.datasource.password = 123456
在数据库造点东西
CREATE TABLE `tb_sys_role` (
`id` bigint(1) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`name` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`description` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `id`(`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of tb_sys_role
-- ----------------------------
INSERT INTO `tb_sys_role` VALUES (1, '管理员', '一切皆可管');
在springboot项目启动类DemoApplication中,添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication(scanBasePackages = "com.smart.demo.*")
@MapperScan("com.smart.demo.dao.mapper")
public class SmartStreetApplication {
public static void main(String[] args) {
SpringApplication.run(SmartStreetApplication.class, args);
}
}
编码
编写一个实体类TbSysRole(使用lombok简化代码)
@Data
public class TbSysRole {
private Long id;
private String name;
private String description;
}
编写一个Mapper类,在上面的com.smart.demo.dao.mapper包下
public interface SysRoleMapper extends BaseMapper {
}
开始使用
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SmartDemoApplication.class)
public class SampleTest {
@Resource
private SysRoleMapper roleMapper;
@Test
public void testRole() {
System.out.println(("----- selectAll method test ------"));
List list = roleMapper.selectList(null);
Assert.assertEquals(1, list.size());
}
}
小结
通过以上几个简单的步骤,我们就实现了 User 表的 CRUD 功能。
从以上步骤中,我们可以看到集成 MyBatis-Plus 非常的简单,只需要引入 starter 工程,并配置 mapper 扫描路径即可。
我那个擦,都不用xml 了,像JPA的调用一样丝滑。
下面介绍MP的具体使用方式
Mapper CRUD 接口
MP 封装通用的CRUD的接口BaseMapper;启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入到容器
使用实例
定义Mapper接口继承BaseMapper
public interface SysRoleMapper extends BaseMapper {
}
定义实体类
@Data
public class TbSysRole {
private Long id;
private String name;
private String description;
}
测试用例
@Test
public void testMapper() {
TbSysRole r = new TbSysRole();
r.setName("aa");
roleMapper.insert(r);
QueryWrapper query = Wrappers.query();
query.eq("name","aa");
TbSysRole role= roleMapper.selectOne(query);
Map map=new HashMap<>();
map.put("name",role.getName());
roleMapper.deleteByMap(map);
roleMapper.insert(r);
roleMapper.delete(query);
List ids=new ArrayList<>();
ids.add(2L);
ids.add(3L);
roleMapper.deleteBatchIds(ids);
}
测试用例
@Resource
private SysRoleMapper roleMapper;
@Test
public void testRole() {
System.out.println(("----- selectAll method test ------"));
List list = roleMapper.selectList(null);
Assert.assertEquals(1, list.size());
}
庐山面目
实体类的Mapper 继承BaseMapper 。其中泛型 T 为实