mybatis-plus自动生成代码记录

更新一下数据库

我就在 idea 里面 可视化 地完成了,方式如下:

img

没有 命令行,没有用 naviCat ,idea 也是挺方便

解决一个依赖问题

本来 更新一下数据库,写一下对应 新 table 的代码,就能生成了,但是 fame 本身只有 mybatis plus,而没有 代码生成器的 依赖。(我估计原作者 zzw 也是有 用 mybatis plus generattor 的,就是没有 在 公开的 pom 文件里面写 吧。)

所以我要自己加上 generator 依赖。

maven 阿里云 下载不来 最新 的 mybatis plus generator 依赖,比如 版本 3.4 的

老版本,也就是我早些时候,学习 mybatis plus 的时候,用到的 老版本 3.0.5, mvn 倒是能给我下载,就是新的版本 mvn 仓库不知道有什么毛病,就是找不见。七煞老夫。

所以 更新 maven 的mirror 设置:

如下:

image.png
   
      aliyunmaven
      *
      阿里云公共仓库
      https://maven.aliyun.com/repository/public
    
    
      aliyunmaven
      central
      阿里云central
      https://maven.aliyun.com/repository/central
    
    
      aliyunmaven
      *
      apachen
      https://maven.aliyun.com/repository/apache-snapshots
    

吃了午饭,定义依赖 到 挺新的版本,就可以下载

新的 依赖 版本 pom 文件 如下:

        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        
        
            com.baomidou
            mybatis-plus-generator
            test
            3.4.0
        
        
            org.apache.velocity
            velocity-engine-core
            2.3
        

然后写一个 生成器的类, 在 test 模块里面:

image.png

其中的 autoGenerate() 如下:

public void autoGenerate() {
        AutoGenerator mpg = new AutoGenerator();
//        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        GlobalConfig gc = new GlobalConfig();
        String projectPath = "/Users/paul/code/javApp/Fame/fame-server";
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("paulkg12");
        gc.setFileOverride(false);
        gc.setIdType(IdType.ASSIGN_ID);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/fame?useUnicode=true&characterEncoding=Utf8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        PackageConfig pc = new PackageConfig();
        pc.setParent("com.zbw");
        pc.setModuleName("fame");
        pc.setEntity("model.entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("SAY");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);

        mpg.execute();
    }

然后你就 能 看见生成的代码了:

image.png

结果

image.png

后记

参考了:

mybatisplus 官方:

https://baomidou.com/guide/generator.html#%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96

image.png

How to Fix java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test [Solved] :
https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html#axzz74QtWbA7j

image.png

应该:jdbc:mysql://localhost:330....

你可能感兴趣的:(mybatis-plus自动生成代码记录)