• MybatisPlus整合SpringBoot


    MyBatis整合SpingBoot

    一、引入SprngBoot0-Starter-parent依赖

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

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>
     	
    	<dependency>
         	<groupId>mysqlgroupId>
    	 	<artifactId>mysql-connector-javaartifactId>
            <version>8.0.28version>
    	dependency>
    	
    	<dependency>
    		<groupId>com.alibabagroupId>
    		<artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.8version>        
     	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, T> implements IService {
    
    }
    */
    @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

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

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

      • 描述:内置 SQL 默认指定排序,优先级低于 wrapper 条件查询
      属性类型必须指定默认值描述
      isDescbooleantrue是否倒序查询
      sortshortShort.MAX_VALUE数字越小越靠前
  • 相关阅读:
    C语言从入门到精通之【字符串】
    字符串旋转
    python 里面对于字典进行key或value排序输出
    【图神经网络论文整理】(二)—— HOW ATTENTIVE ARE GRAPH ATTENTION NETWORKS?:GATv2
    伦敦银怎么算自己的收益?
    【力扣 Hot100 | 第六天】4.21(字母异位词分组)
    CF821 A. Consecutive Sum
    Java毕设项目——大学生社团管理系统(java+SSM+Maven+Mysql+Jsp)
    rust
    Mac 安装comfigUI (M1)
  • 原文地址:https://blog.csdn.net/weixin_44907046/article/details/126926088