快速入门
一、导入依赖
<!-- mybatis-plus 是自己开发,并非官方的! -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
二、连接数据库
这一步和 mybatis 相同!
mysql 5 驱动不同 com.mysql.jdbc.Driver
mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置,向下兼容
serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?
useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
三、CRUD
1.主键自动生成
@TableId(type = IdType.AUTO)
源码
public enum IdType {
AUTO(0), // 数据库id自增
NONE(1), // 未设置主键
INPUT(2), // 手动输入
ID_WORKER(3), // 默认的全局唯一id
UUID(4), // 全局唯一id uuid
ID_WORKER_STR(5); //ID_WORKER 字符串表示法
}
2.动态sql
通过条件自动拼接动态sql!!!
3.逻辑删除
一个注解即可!
@TableLogic //逻辑删除
private Integer deleted;
四、配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
五.自动填充(注解+处理器)
创建时间、修改时间!这些个操作一遍都是自动化完成的,我们不希望手动更新!
1.在表中新增字段 create_time, update_time
2.实体类字段属性上需要增加注解
// 字段添加填充内容
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
3.编写处理器来处理这个注解即可!
package com.xwy.mybatisplus_study.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
六、插件使用
1.乐观锁(注解+插件(拦截器))
@Version //乐观锁Version注解
private Integer version;
拦截器配置
@Configuration
@MapperScan("com.xwy.mybatisplus_study.mapper")
public class MyBatisPlusConfig {
//注册插件
//看官网文档
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//注册乐观锁插件
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//注册分页插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
}
2.分页(插件(拦截器)+ 使用Page对象)
配置拦截器组件,同上
直接使用Page对象即可!
// 测试分页查询
@Test
public void testPage(){
// 参数一:当前页
// 参数二:页面大小
// 使用了分页插件之后,所有的分页操作也变得简单的!
Page<User> page = new Page<>(2,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
System.out.println(page.getTotal());
}
3.其他使用
myBatis puls 官方文档
七、条件构造器
十分重要:Wrapper
写一些复杂的sql就可以使用它来替代!
如:查询name不为空的用户,并且邮箱不为空的用户,年龄大于等于12
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.isNotNull("name")
.isNotNull("email")
.ge("age",12);
userMapper.selectList(wrapper).forEach(System.out::println);
一些用法:
.eq("name","狂神说"); //eq 相等
.orderByAsc("id") //排序
.between("age",20,30); // 区间
//模糊查询
.notLike("name","e") //左和右
.likeRight("email","t"); //t%
//子查询
.inSql("id","select id from user where id<3");
八、代码自动生成器
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、
Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
注:学习笔记,来源狂神说