栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

如何在SpringBoot中使用Mybatis-Plus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在SpringBoot中使用Mybatis-Plus

Mybatis-Puls (下文简称MP)如何在SpringBoot中使用

1、首先,引入相关的依赖
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
2、在application.yml文件配置MP的相关属性
mybatis-plus:
  configuration:
    # 在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    # 举两个栗子 表名:address_book -->实体类名: AddressBook	字段名:user_name --> 属性名:userName
    map-underscore-to-camel-case: true
    # 开启控制台SQL日志打印
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
    # 指定主键生成策略 assign_id 采用雪花算法
      id-type: assign_id
3、如何使用MP的CRUD

mapper层

  1. 继承MP提供的 BaseMapper 类,并指定泛型

    @Mapper
    public interface EmployeeMapper extends BaseMapper {
    
    }
    

service接口层

  1. 继承MP提供的 IService 接口,并指定泛型

    public interface EmployeeService extends IService {
        
    }
    

service实现类层

  1. 继承MP提供的 ServiceImpl,指定泛型,实现 service 接口

    @Service
    public class EmployeeServiceImpl extends ServiceImpl implements EmployeeService {
        
    }
    

    需要指定两个泛型,第一个是Mapper,第二个是实体类

3、如何使用MP的分页插件
  1. 先搞一个配置类
@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

end…

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号