Java代码生成器

全自动代码生成器mybatis-generator

项目地址

https://github.com/GitHub-Laziji/mybatis-generator

代码模版位于resources下, 可根据需要自行修改

第一个模版是根据 commons-mybatis 编写的, 若不适合可以自行修改模版或选择其他模版

配置文件

resources下创建application-${name}.yml文件, ${name}随意, 例如: application-example.yml, 可创建多个

配置文件内容如下, 填入数据库配置, 以及生成代码的包名

模版文件映射用于自定义生成文件的包格式以及文件名

动态属性包含

  • {packageFilePath} 包文件路径 例如: com/xxx/xxx
  • {className} 类名 由表名改为驼峰命名法得来
  • {suffix} 类名后缀 DO或VO

一般按以下配置即可

现在项目中有两套模版template.path 可以选mybatismybatis-default

也可以自行扩展

spring:
  datasource:
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx?characterEncoding=utf-8
    username: xxxxxx
    password: xxxxxx

generator:
  package: com.xxx.xxx
  template:
    path: mybatis
    mapping: |
      Model.java.vm: main/java/{packageFilePath}/database/model/{className}.java
      Query.java.vm: main/java/{packageFilePath}/database/query/{className}Query.java
      Dao.java.vm: main/java/{packageFilePath}/database/dao/{className}.java
      Service.java.vm: main/java/{packageFilePath}/database/service/{className}Service.java
      Mapper.xml.vm: main/resources/mapper/{className}Mapper.xml

使用

在test文件下创建测试类

  • @ActiveProfiles("example")中填入刚才配置文件名的name
  • tableNames需要生成的表, 可以多个
  • zipPath 代码导出路径

调用generatorService.generateZip传入参数可以是表名数组String[]或者TableItem[]

运行测试方法

package pg.laziji.generator;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import pg.laziji.generator.mybatis.GeneratorService;

import javax.annotation.Resource;
import java.io.IOException;

@ActiveProfiles("example")
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleTest {

    @Resource
    private GeneratorService generatorService;

    @Test
    public void test(){
        String zipPath = "/home/code.zip";

//        String[] tableNames = new String[]{"table1","table2"};
//        generatorService.generateZip(tableNames,zipPath);

        TableItem[] tableItems = new TableItem[]{
                new TableItem("table1", "TableA"),
                new TableItem("table2", "TableB")
        };
        generatorService.generateZip(tableItems,zipPath);
    }
}
欢迎关注我的博客公众号
图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值