MybatisPlus整合SpringBoot

发布于:2022-12-13 ⋅ 阅读:(575) ⋅ 点赞:(0)

MyBatis整合SpingBoot

一、引入SprngBoot0-Starter-parent依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.6</version>
    <relativePath/>
</parent>

二、引入MybatisPlus以及SpringBoot基础框架

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.1</version>
    </dependency>
 	<!--mysql数据库连接驱动-->
	<dependency>
     	<groupId>mysql</groupId>
	 	<artifactId>mysql-connector-java</artifactId>
        <version>8.0.28</version>
	</dependency>
	<!--德鲁伊数据库连接池-->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.8</version>        
 	</dependency>
</dependencies>

三、配置数据库连接

spring:
	datasource:
    username: 账户
    password: 密码
    url: jdbc:mysql://连接地址:3306/数据库名称?useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.cj.jdbc.Driver

四、配置启动配置类

1、配置分页

2、配置乐观锁

@Configuration //配置类
@EnableTransactionManagement //开启事务管理器
@MapperScan(basePackages = "扫描的Mapper包名")
public class MybatisConfig {
    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //设置Mybatis拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        
        
        //设置分页拦截器类型---(数据库类型)
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        //配置溢出总页数后是否进行处理
        paginationInnerInterceptor.setOverflow(true);
        //单页分页条数限制
        paginationInnerInterceptor.setMaxLimit(1000L);
        //其他配置参照分页插件拦截器源码
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        
        //设置乐观锁
        /*
        	1、支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
			2、整数类型下 newVersion = oldVersion + 1 newVersion 会回写到 entity 中
        	3、仅支持 updateById(id) 与 update(entity, wrapper) 方法
			4、在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
        */
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
       
        
      	//防止全表更新插件
        BlockAttackInnerInterceptor blockAttackInnerInterceptor = new BlockAttackInnerInterceptor();
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor);
            
        return interceptor;
    }

}

五、@Mapper注解与Dao和Service使用

//Dao/Mapper
@Mapper
public interface 实体类Entity extends BaseMapper<你的数据Entity> {
   //实体类属性
}
//Service
//Interface
public interface Service名称 extends IService<实体类Entity> {
    //Service提供的方法接口定义
}
//ServiceImpl
/*
源码IService
IService 实现类( 泛型:M 是 mapper 对象,T 是实体 )
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

}
*/
@Service("注入的ServiceBean名称")
public class ServiceImpl名称 extends ServiceImpl<Mapper对象, 实体类Entity> implements Service名称 {
    //Service实现类
}

六、常用实体类注解

官方参考文档

  1. @TableName

    • 描述:表名注解,标识实体类对应的表
    • 使用位置:实体类
  2. @TableId

    • 描述:主键注解
    • 使用位置:实体类主键字段
  3. @TableField

    • 描述:字段注解(非主键)
  4. @Version

    • 描述:乐观锁注解、标记 @Verison 在字段上
  5. @EnumValue

    • 描述:普通枚举类注解(注解在枚举字段上)
  6. @TableLogic

    • 描述:表字段逻辑处理注解(逻辑删除)
    属性 类型 必须指定 默认值 描述
    value String “” 逻辑未删除值
    delval String “” 逻辑删除值
  7. @KeySequence

    • 描述:序列主键策略 oracle
    • 属性:value、resultMap
    属性 类型 必须指定 默认值 描述
    value String “” 序列名
    clazz Class Long.class id 的类型, 可以指定 String.class,这样返回的 Sequence 值是字符串"1"
  8. @OrderBy

    • 描述:内置 SQL 默认指定排序,优先级低于 wrapper 条件查询
    属性 类型 必须指定 默认值 描述
    isDesc boolean true 是否倒序查询
    sort short Short.MAX_VALUE 数字越小越靠前