简介
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
- 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
- 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
- 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
- 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
- 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
- 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
First MybatisPlus项目
- 创建spring项目
- pom文件导入相关依赖
<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>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- application.yml配置数据库
server:
port: 8899 #数据库接口
spring:
datasource:
username: root #数据库用户名
password: 123456 #密码
url: jdbc:mysql://localhost:8888/test? serverTimezone=UTC&useunicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
#test为数据库名称
- 创建pojo文件编写第一个实体类User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
- 创建Mapper文件 编写对于实体User的数据库crud的Mapper接口
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
//不需要在配置xml文件 编写sql语句了 已经完成dao层配置了
}
- 编写测试类 测试数据库的User的增、读
@SpringBootTest
class MybatisPlusApplicationTests {
//继承了BASEmapper 所有方法都来自 父类
//我们也可以编写自己的拓展方法
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() { //读取
//参数是一个wrapper 条件构造器 先不用 null
//查询全部用户
List<User> userList=userMapper.selectList(null);
userList.forEach(System.out::println);
@Test
void insertTest() //增加
{
User user=new User();
user.setName("彭于晏");
user.setAge(3);
user.setEmail("1805332271@qq.com");
userMapper.insert(user); //自动填充id
System.out.println(user);
}
//更新
@Test
void updateTest()
{
User user=new User();
user.setId(1417407084273688578l);
user.setAge(1998);
userMapper.updateById(user);
}
//查询测试
@Test //单个查询 id
void SelectById()
{
User user=userMapper.selectById(1l);
System.out.println(user);
}
@Test //行数查询
void SelectByBatchId()
{
List<User> userList = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
userList.forEach(System.out::println);
}
@Test //属性查询
void SelectByBatchIds()
{
HashMap<String,Object> map=new HashMap<>();
map.put("name","Tom");
map.put("age",3);
List<User> userList = userMapper.selectByMap(map);
userList.forEach(System.out::println);
}
// 分页查询 通过Page对象直接操作
@Test
void PageTest()
{
//page 参数 当前页和页面大小
Page<User> page=new Page<>(2,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
System.out.println(page.getTotal());
}
//删除 参考查找操作一模一样 select更换为delect
}
}
配置日志
整个流程我们不清楚sql是怎么运行的,编写日志进行查看(application.yml文件里)
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
自动填充
例如数据更新时间、添加时间
public class User {
//对应数据库中的主键 id生成策略(自增id、uuid、雪花算法、redis、zookeeper)
//@TableId(type=IdType.INPUT)
@TableId(type = IdType.ID_WORKER)
private Long id;
private String name;
private Integer age;
private String email;
//自动填充增加时间、更新时间 handler文件
@TableField(fill= FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.UPDATE)
private Date updateTime;
}
自动添加类扩展
@Slf4j
@Component //部署到ioc容器中
public class MyMetaObjectHandler implements MetaObjectHandler {
//插入时的填充策略
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill......");
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
//更新时的填充策略
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill......");
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}