1.项目准备
引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
其他基本配置查看我的上一篇博客:
SpringBoot整合mybatis使用_adamxuu的博客-优快云博客
将application.yml中mybatis的配置改成mybatis plus即可
测试类、数据库表项等准备也请看我的上一篇博客
2.基本增删改查
在Mapper中添加如下代码:
如果你的数据库表项和实体类属性规范命名,如实体类属性id命名为authorId,数据库表项为author_id,可以直接测试,否则需要用注解配置Entity类。
注解详细配置可以看官方文档:
这是我的一份注解:
@TableName(value="author",resultMap = "authorMap",excludeProperty = "books")
public class Author {
@TableId(value = "author_id",type= IdType.AUTO)
Integer id;
@TableField("name")
String name;
List<Book> books;
}
直接开始测试:
@Test
public void PlusTest()
{
// //查
// List<Author> list=authorMapper.selectList(null);
// System.out.println(list);
// //单体查
// Author author=authorMapper.selectById(3);
// //增
// Author author = new Author(0,"苏童",null);
// authorMapper.addAuthor(author);
// System.out.println("新增作者id为:"+author.getId());
// //删
// authorMapper.deleteById(3);
// //直接从对象取出ID
// Author author = new Author(2,"古龙",null);
// authorMapper.deleteById(author);
//改
Author author = new Author(2,"古龙香水",null);
authorMapper.updateById(author);
}
可以看到单表增删改查十分方便,甚至不用写Mapper接口和xml。
3.条件构造器
官方文档:
写的比较好的博客:
mybatis-plus中wrapper的用法(详细)_mybatisplus wrapper_乞力马扎罗の黎明的博客-优快云博客
注意在查的时候,尽管没有符合条件的List,还是会返回一个大小为0的list实例而不是null