• SpringBoot整合mybatis-plus代码生成器(备用)


    • pom.xml 导入 MBP 依赖
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.2</version>
    </dependency>
    
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.2</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>
    
    • 代码生成器
    package com.nxj;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    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.NamingStrategy;
    
    public class GenerateTest {
        public static void main(String[] args) {
            //创建generator对象
            AutoGenerator autoGenerator = new AutoGenerator();
            //数据源
            DataSourceConfig dataSourceConfig = new DataSourceConfig();
            dataSourceConfig.setDbType(DbType.MYSQL);
            dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
            dataSourceConfig.setUsername("root");
            dataSourceConfig.setPassword("123456");
            dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test");
            autoGenerator.setDataSource(dataSourceConfig);
            //全局配置
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
            globalConfig.setAuthor("admin");
            globalConfig.setOpen(false);
            globalConfig.setServiceName("%sService");
            autoGenerator.setGlobalConfig(globalConfig);
            //包信息
            PackageConfig packageConfig = new PackageConfig();
            packageConfig.setParent("com.nxj");
            packageConfig.setEntity("pojo");
            packageConfig.setMapper("mapper");
            packageConfig.setService("service");
            packageConfig.setServiceImpl("service.impl");
            packageConfig.setController("controller");
            autoGenerator.setPackageInfo(packageConfig);
            //策略配置
            StrategyConfig strategyConfig = new StrategyConfig();
            strategyConfig.setInclude("test","table2"); // 想要那些表就写哪些
            strategyConfig.setNaming(NamingStrategy.underline_to_camel);
            strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
            strategyConfig.setEntityLombokModel(true);
            autoGenerator.setStrategy(strategyConfig);
            //运行
            autoGenerator.execute();
        }
    }
    
    • application.yml
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/test
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      mapper-locations: classpath:com/nxj/mapper/xml/*.xml
    server:
      port: 8181
    
    • 跨域
    package com.nxj.configuration;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class CrosConfiguration implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOriginPatterns("*")
                    .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                    .allowCredentials(true)
                    .maxAge(3600)
                    .allowedHeaders("*");
        }
    
    }
    
  • 相关阅读:
    【Flink系列十二】使用OpenResty 在InfluxDB协议层拦截Flink指标
    【Azkaban优化】防止IP变化导致频繁登录
    困扰多年的Quartz重复调度的问题,终于找到原因
    【Flink系列十一】FlinkSQL Gateway以及支持Kerberos多租户的实现思路
    【Flink系列十三】FlinkKafkaConnector KafkaSource FlinkKafkaConsumer没有上报指标
    IDEA项目结构出现 0% methods,0% lines covered up
    java去重 up
    svndown下来项目但是导入不了maven包,解决办法 up
    新型病毒加强勒索病毒预防 up
    java list 时间排序, up
  • 原文地址:https://www.cnblogs.com/ningxinjie/p/14623160.html
Copyright © 2020-2023  润新知