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

Springboot整合MybatisPlus的实现过程解析

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

Springboot整合MybatisPlus的实现过程解析

这篇文章主要介绍了Springboot整合MybatisPlus的实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、pom文件



  4.0.0

  com.cun
  plus
  0.0.1-SNAPSHOT
  jar

  plus
  Demo project for Spring Boot

  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.14.RELEASE
     
  

  
    UTF-8
    UTF-8
    1.8
  

  

    
      org.springframework.boot
      spring-boot-starter-web
    

    
      mysql
      mysql-connector-java
      runtime
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    

    
    
      com.baomidou
      mybatis-plus-boot-starter
      2.3
    
    

    
    
      org.freemarker
      freemarker
      2.3.28
    


    
    
      com.alibaba
      druid-spring-boot-starter
      1.1.10
    

  

  
    
      
 org.springframework.boot
 spring-boot-maven-plugin
      
    
  

2、创建CodeGenerator.java

package com.cun.plus;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class CodeGenerator {
  public static void main(String[] args) {
    //1. 全局配置
    GlobalConfig config = new GlobalConfig();
    config.setActiveRecord(false) // 是否支持AR模式
 .setAuthor("len") // 作者
 .setOutputDir(".\src\main\java") // 生成路径
 .setFileOverride(true) // 文件覆盖
 .setIdType(IdType.AUTO) // 主键策略
 .setServiceName("I%sService") // 设置生成的service接口的名字的首字母是否为I
 // IUserService
 .setbaseResultMap(true)
 .setbaseColumnList(true);

    //2. 数据源配置
    DataSourceConfig dsConfig = new DataSourceConfig();
    dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型
 .setDriverName("com.mysql.jdbc.Driver")
 .setUrl("jdbc:mysql://localhost:3306/mydatab?useSSL=true&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8")
 .setUsername("root")
 .setPassword("lqq74561");

    //3. 策略配置

    //配置要生成的表的表名
    String[] tableName = {"system_power_type","system_admin","company","power_api","power_action","power_action_api","power_action_group","power_admin_group","power_group"};
    StrategyConfig stConfig = new StrategyConfig();
    stConfig.setCapitalMode(true) //全局大写命名
 .setDbColumnUnderline(true) // 指定表名 字段名是否使用下划线
 .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
 .setTablePrefix("tb_")
 .setInclude(tableName); // 生成的表

    //4. 包名策略配置
    PackageConfig pkConfig = new PackageConfig();
    pkConfig.setParent("com.cun.plus")
 .setMapper("mapper")
 .setService("service")
 .setController("controller")
 .setEntity("entity")
 .setXml("mapper");

    //5. 整合配置
    AutoGenerator ag = new AutoGenerator();
    ag.setGlobalConfig(config)
 .setDataSource(dsConfig)
 .setStrategy(stConfig)
 .setPackageInfo(pkConfig);

    //6. 执行
    ag.setTemplateEngine(new FreemarkerTemplateEngine());
    ag.execute();
  }
}

3、在application.yml中配置mybatis-plus

#mybatis-plus
mybatis-plus:
 #xml
 mapper-locations: classpath:/mapper
@Bean
@Profile({"dev","test"})// 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
  return new PerformanceInterceptor();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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