mybatis plus+lombok 逆向工具类

需要引用的jar

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.28</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
        <scope>provided</scope>
    </dependency>

/**

  • @Author zbf

  • @DATA 2019/10/8 16:48

  • @ClassIntroduction
    */
    public class CodeGenerator {

    public static void main(String[] args) {

     String[] tables = {"system_quartz","system_quartz_info"};
     String parentMapper="/src/main/java/com/qdwy/qudaowuyouservice/mapper/feedback";
     String item_model="/qudaowuyou-service";//model
     String mapper_url="/src/main/resources/mapper/feedback";
    
     String bean_moedel="/qudaowuyou-entity"; //实体
     String bean_url="/src/main/java/com/qdwy/qudaowuyouentity/entity/feedback";
     String db_url="jdbc:mysql://47.106.192.29:3306/qdwy_feedback?autoReconnect=true";
    
     String db_driver="com.mysql.cj.jdbc.Driver";
     String db_user="root";
     String db_password="qudaowuyou201951@#$&*";
     GeneratorUtils.genertatorUilt(tables, item_model,mapper_url, db_url, db_driver, db_user, db_password,bean_moedel,bean_url,parentMapper);
    

    }

}

/**

  • @Author zbf

  • @DATA 2019/10/14 21:01

  • @ClassIntroduction
    */
    public class GeneratorUtils {

    /**
    *

    • @param tables 表名

    • @param item_model 需要放在哪一个model下

    • @param mapperUrl mapper文件存放地址

    • @param db_url 数据库的链接url

    • @param db_driver 数据库的链接

    • @param db_user 数据库的用户名

    • @param db_password 数据库的密码
      */
      public static void genertatorUilt(String[] tables, String item_model, String mapperUrl, String db_url, String db_driver, String db_user,
      String db_password,String beanPath_url,String bean_url,String mapper_url) {
      // 代码生成器
      AutoGenerator mpg = new AutoGenerator();
      // 全局配置
      GlobalConfig gc = new GlobalConfig();
      final String projectPath = System.getProperty(“user.dir”)+item_model;
      final String beanProjectPath=System.getProperty(“user.dir”)+beanPath_url;

      gc.setOutputDir(projectPath + “/src/main/java”);
      gc.setAuthor(“zbf”);
      gc.setOpen(false);
      gc.setBaseResultMap(true);
      gc.setBaseColumnList(true);
      gc.setEnableCache(false);
      mpg.setGlobalConfig(gc);

      // 数据源配置
      DataSourceConfig dsc = new DataSourceConfig();
      dsc.setUrl(db_url);
      dsc.setDriverName(db_driver);
      dsc.setUsername(db_user);
      dsc.setPassword(db_password);
      mpg.setDataSource(dsc);

      // 包配置
      // PackageConfig pc = new PackageConfig();
      // pc.setModuleName(null);
      // pc.setParent(parentPackage);
      // mpg.setPackageInfo(pc);

      // 自定义配置
      InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {

       }
      

      };

      // 如果模板引擎是 freemarker
      String templatePath = “/templates/mapper.xml.ftl”;

      // 自定义输出配置
      List focList = new ArrayList<>();
      // 自定义配置会被优先输出
      focList.add(new FileOutConfig(templatePath) {
      @Override
      public String outputFile(TableInfo tableInfo) {
      // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
      /projectPath + “/src/main/resources/mapper/” + pc.getModuleName()
      + “/” + tableInfo.getEntityName() + “Mapper” + StringPool.DOT_XML;
      /
      return projectPath + mapperUrl
      + “/” + tableInfo.getEntityName() + “Mapper” + StringPool.DOT_XML;
      }
      });

      String templatePathBean="/templates/entity.java.ftl";
      focList.add(new FileOutConfig(templatePathBean) {
      @Override
      public String outputFile(TableInfo tableInfo) {
      return beanProjectPath+bean_url +"/"+tableInfo.getEntityName()+StringPool.DOT_JAVA;
      }
      });

      String templateMapper="/templates/mapper.java.ftl";
      focList.add(new FileOutConfig(templateMapper) {
      @Override
      public String outputFile(TableInfo tableInfo) {
      return projectPath+mapper_url+"/"+tableInfo.getEntityName()+“Mapper”+StringPool.DOT_JAVA;
      }
      });

      cfg.setFileOutConfigList(focList);
      mpg.setCfg(cfg);

      // 配置模板
      TemplateConfig templateConfig = new TemplateConfig();
      //是否需要产生对应的默认文件
      templateConfig.setXml(null);
      templateConfig.setEntity(null);
      templateConfig.setController(null);
      templateConfig.setServiceImpl(null);
      templateConfig.setService(null);
      templateConfig.setMapper(null);
      mpg.setTemplate(templateConfig);
      // 策略配置
      StrategyConfig strategy = new StrategyConfig();

      //实体对象公共父类
      //strategy.setSuperEntityClass(BaseEntity.class);

      //表名生成策略
      strategy.setNaming(NamingStrategy.underline_to_camel);
      //表字段生成策略
      strategy.setColumnNaming(NamingStrategy.underline_to_camel);
      strategy.setInclude(tables);
      strategy.setEntityLombokModel(true);
      // strategy.setTablePrefix(pc.getModuleName() + “_”);
      mpg.setStrategy(strategy);
      mpg.setTemplateEngine(new FreemarkerTemplateEngine());
      mpg.execute();
      }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于mybatis plus和postgresql的配置,可以按照以下步骤进行: 1. 在项目中引入mybatis plus和postgresql的依赖,例如: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.16</version> </dependency> ``` 2. 在application.properties(或者application.yaml)中配置数据库的连接信息,例如: ```properties spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=myuser spring.datasource.password=mypassword # mybatis plus 配置 mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml mybatis-plus.type-aliases-package=com.example.entity # 开启驼峰命名转换 mybatis-plus.configuration.map-underscore-to-camel-case=true ``` 3. 在实体类上使用相关注解,例如: ```java @Data @NoArgsConstructor @AllArgsConstructor @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; } ``` 4. 在mapper接口中继承BaseMapper类,并定义自己的方法,例如: ```java @Mapper public interface UserMapper extends BaseMapper<User> { // 自定义查询方法 List<User> findByName(String name); } ``` 这样就可以轻松使用mybatis plus和postgresql进行数据操作了。希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值