MyBatis-Plus代码生成器配置及application文件配置

一)、代码生成器配置

<dependency>
    <groupId>org.apache.velocitygroupId>
    <artifactId>velocity-engine-coreartifactId>
    <version>2.0version>
dependency>

代码生成时,指定生成的日期格式类型

GlobalConfig gc = new GlobalConfig(); 
gc.setDateType(DateType.TIME_PACK);
/**
 * 测试MyBatis-Plus代码生成器,注意要记得加velocity-engine-core依赖
 */
public static void main(String[] args){
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();

    // 1. 设置全局配置
    GlobalConfig gc = new GlobalConfig();
    // 获取当前项目路径
    String projectPath = System.getProperty("user.dir");
    // 设置生成文件的输出目录,默认为D盘
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("MoCha"); // 设置作者
    gc.setOpen(false); // 是否打开输出目录,默认为true
    gc.setActiveRecord(false); // 是否开启AR模式,默认为false
    gc.setFileOverride(true); // 是否覆盖已有文件,默认为false
    gc.setIdType(IdType.AUTO); // 指定生成的主键的ID类型
    //  设置生成的service接口的名字的首字母是否为I,如果注释该行代码则会生成如IEmployeeService这样的文件
    gc.setServiceName("%sService");
    gc.setBaseColumnList(true);
    gc.setBaseResultMap(true);
    // 将全局配置添加到代码生成器中
    mpg.setGlobalConfig(gc);

    // 2. 设置数据源
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL);
    // mysql驱动6以上
    dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
    // MySQL8版本,使用 jdbc:mysql://localhost:3306/demo_database?useSSL=false&allowPublicKeyRetrieval=true
    dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/demo_database?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
    dataSourceConfig.setUsername("root");
    dataSourceConfig.setPassword("123456");
    mpg.setDataSource(dataSourceConfig);

    // 3. 数据库表配置
    StrategyConfig strategyConfig = new StrategyConfig();
    // 数据库表映射到实体的命名策略,下划线转驼峰命名
    strategyConfig.setNaming(NamingStrategy.underline_to_camel);
    strategyConfig.setEntityLombokModel(true); // 是否为lombok模型
    strategyConfig.setCapitalMode(true); // 是否大写命名
    // 当数据表以"tbl"为前缀时,不会将Tbl作为Java文件前缀
    strategyConfig.setTablePrefix("tbl_"); //设置表前缀
    // 需要包含的表名,允许正则表达式(也就是指定将对应的数据表进行代码生成)
    strategyConfig.setInclude("bonus");
    mpg.setStrategy(strategyConfig);

    // 4. 包配置
    PackageConfig packageConfig = new PackageConfig();
    // 设置模块名称
    packageConfig.setModuleName("bonus");
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {
        @Override
        public void initMap() {
            // to do nothing
        }
    };
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();

    // 如果模板引擎是 velocity
    String templatePath = "/templates/mapper.xml.vm";
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig(templatePath) {
        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 ,如果 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return projectPath + "/src/main/resources/mapper/" + packageConfig.getModuleName()
                    + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    // 设置父包名称,这样一来设置其他层的包名时就只需要设置该层名字即可
    packageConfig.setParent("top.yangzefeng.demo");
    packageConfig.setController("controller");
    packageConfig.setService("service");
    packageConfig.setMapper("mapper");
    packageConfig.setEntity("entity");

    cfg.setFileOutConfigList(focList);

    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);

    mpg.setTemplate(templateConfig);
    mpg.setCfg(cfg);
    mpg.setPackageInfo(packageConfig);
    mpg.execute();
}

二)、application.yml配置文件

# 注意mysql7以上的驱动更换为com.mysql.cj.jdbc.Driver
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo_database?useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

# 打印sql语句
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径
'mapperLocations' was not specified or no matching resources found

你可能感兴趣的:(MyBatis-Plus,Spring,Boot)