springboot引入mybatis-plus

  • Post author:
  • Post category:其他


1.引入mybatis-plus的依赖包(不要看网上其他的依赖  引入3个依赖  都是瞎扯淡) 直接用我的  完全ojbk2

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.0.3</version>
</dependency>

2.使用@MapperScan启动类中添加扫描地址   当然目前是全局引入 也是最通用   你也可以在每个Mapper接口上面添加@Mapper注解

/**
 * @author peanut
 * @date 20210329
 */
@SpringBootApplication
@MapperScan("com.peanut.dao")
public class SpringbootElementApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootElementApplication.class, args);
	}
}

3.然后在咱们dao  service 两层分别继承mybatisplus的基类   就像下面这样

// dao
public interface SysUserMapper extends BaseMapper<SysUser> {}
// service
public interface SysUserService extends IService<SysUser> {}
// serviceImpl
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{}

4.如果要使用分页插件  那我们还需要配置 分页插件   非常简单  直接粘贴拿去  如下

@EnableTransactionManagement
@Configuration
@MapperScan("com.peanut.dao")
public class MybatisPlusConfig {
    /**
     *   mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        // 设置方言
        page.setDialectType("mysql");
        return page;
    }
}

@EnableTransactionManagement   记得开启事务管理器啊

5.再来看下 配置文件

mybatis-plus:
  type-aliases-package: com.peanut.entity
  mapper-locations: classpath:mappers/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

至此  配置完成   大佬们  赶快试试吧