SpringBoot整合Mybatis-Plus

1.pom.xml添加依赖

<!--mysql数据库连接驱动-->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>
<!-- mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.0</version>
</dependency>

 

2.application.yml配置

server:
  port: 80
spring:
  application:
    name: springboot-mybatis-plus
  datasource:
    url: jdbc:mysql://localhost:3306/company_manage?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root

#mybatis-plus配置控制台打印完整带参数SQL语句
logging:
  level:
    com.company.management.system: debug

 

3.Mybatis-plus分页插件

@Configuration
public class MybatisPlusConfig {    
  @Bean   
public PaginationInterceptor paginationInterceptor() {   PaginationInterceptor paginationInterceptor = new PaginationInterceptor();   paginationInterceptor.setDialectType("mysql");   return paginationInterceptor;   } }

 

 4.代码生成器

**
 * <p>
 * 代码生成器演示
 * </p>
 */
public class MpGenerator {
        public static void main(String[] args){
            AutoGenerator mpg = new AutoGenerator();

            //TODO 全局配置
            GlobalConfig gc = new GlobalConfig();
            gc.setOutputDir(System.getProperty("user.dir")+"/src/main/java");//设置输出目录
            gc.setFileOverride(true);//设置文件重写
            gc.setActiveRecord(true);//设置开启AR模式,不需要ActiveRecord特性的请改为false
            gc.setEnableCache(false);//XML二级缓存
            gc.setBaseResultMap(true);//XML ResultMap
            gc.setBaseColumnList(false);//XML columList
            gc.setAuthor("");//作者

            //自定义文件命名,注意%s 会自动填充表实体属性
            gc.setControllerName("%sController");
            gc.setServiceName("%sService");
            gc.setServiceImplName("%sServiceImpl");
            gc.setMapperName("%sMapper");
            gc.setXmlName("%sMapper");
            mpg.setGlobalConfig(gc);

            //TODO 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setDbType(DbType.MYSQL);
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("root");
            dsc.setUrl("jdbc:mysql://localhost:3306/manage?characterEncoding=utf8&useSSL=false&serverTimezone=GMT");
            mpg.setDataSource(dsc);

            //TODO 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setLogicDeleteFieldName("IS_DEL");//设置逻辑删除字段
            strategy.setRestControllerStyle(true);//启用rest风格
            strategy.setTablePrefix(new String[]{});//此处可以修改您的表前缀
            strategy.setNaming(NamingStrategy.underline_to_camel);//表名生成策略
            strategy.setInclude(new String[]{"manage_user","manage_roles","manage_permission","manage_menu"});//需要生成的表
            strategy.setSuperServiceClass(null);// 自定义 service 父类
            strategy.setSuperServiceImplClass(null);// 自定义 service 实现类父类
            strategy.setSuperMapperClass(null);// 自定义 mapper 父类
            mpg.setStrategy(strategy);

            //TODO 包配置
            PackageConfig pc = new PackageConfig();
            pc.setParent("com.company.management.system");
            pc.setController("controller");
            pc.setService("service");
            pc.setServiceImpl("service.impl");
            pc.setMapper("mapper");
            pc.setEntity("entity");
            pc.setXml("mapper");
            mpg.setPackageInfo(pc);

            //执行生成
            mpg.execute();
        }
}

 

 5.启动类扫描mapper

@SpringBootApplication
@MapperScan("com.company.management.system.mapper")
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

 

目录结构

 

 

posted @ 2020-03-10 10:34  一月1  阅读(596)  评论(0编辑  收藏  举报