Giter Club home page Giter Club logo

code-generator's Introduction

Code Generator

自定义代码自动生成器, 目前包含Java数据库Mapper, Dao, Service层模板

欢迎贡献各种模版

已有模板

目前项目中包含这些模版在resources下, 如果模版不合适可以自己模仿其中的模版进行修改

  • mybatis2 是根据 commons-mybatis 通用Mapper编写的, 依赖commons-mybatis 2.0
  • mybatis-default 这个生成的是简单的mybatis实体类、Dao接口以及XML, 不依赖其他包
  • mybatis-plus 生成mybatis-plus使用的实体类以及Mapper文件, 依赖mybatis-plus

配置文件

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

配置文件属性:

  • generator.datasource 填入自己的项目数据库相关配置
  • generator.package 项目包名
  • generator.template.base-path 表示模版文件的路径目前可选模板请看 已有模板
  • generator.template.output-paths 模板输出相对路径, 每行一个, 格式为[模板文件名]: [模板输出相对路径,可使用{}引入动态变量]可选动态变量包含:
    • {packagePath} 包文件路径, 例如: com/xxx/xxx
    • {className} 类名, 由表名转成驼峰命名法得来 可覆盖
    • {lowercaseClassName} 首字母小写的类名
    • 以及其他dynamicPath域中的自定义属性, '\'为转义符, 其后的字符不进行解析

一般按以下配置即可, 数据库支持mysqloracle

generator:
  datasource:
    type: mysql
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx?characterEncoding=utf-8
    username: xxxxxx
    password: xxxxxx
    
  package: com.xxx.xxx
  template:
    base-path: mybatis2
    output-paths: |
      Model.java.vm: main/java/{packagePath}/database/model/{className}.java
      Query.java.vm: main/java/{packagePath}/database/query/{className}Query.java
      Dao.java.vm: main/java/{packagePath}/database/dao/{className}Dao.java
      Service.java.vm: main/java/{packagePath}/database/service/{className}Service.java

使用

在test文件下创建测试类

  • @ActiveProfiles("example")中填入刚才配置文件名的name
  • tableNames需要生成的表, 可以多个, newBuilder使用构造类 自定义类名, 以及其他参数
  • zipPath 代码导出路径

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

生成的压缩包如果解压出错可以选择360解压

运行测试方法

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.model.TableItem;
import pg.laziji.generator.service.GeneratorService;

import javax.annotation.Resource;

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

    @Resource
    private GeneratorService generatorService;

    @Test
    public void test() {
        generatorService.generateZip(new TableItem[]{
                TableItem.newBuilder()
                        .tableName("table1")
                        .dynamicPathVariable("className", "TableA")
                        .build(),
                new TableItem("table2")
        }, "/home/code.zip");
    }
}

各个变量域包含的字段

system

只能在VM模板中使用, 可以通过TemplateContext.setSystemVariable(key, value)添加和覆盖, 相当于全局变量, 初始包含以下信息, value的值可以是Class, 例如放入CommonUtils.class 在模板中就可以调用该类中的静态方法 参考下面的configutils

  • config 可以在模板中通过这个获取所有配置文件信息system.config.get("xxx")

  • utils 工具类, 里面包含格式化当前时间的函数, 可以扩展system.utils.time("yyyy/MM/dd HH:mm:ss")

  • username 系统用户名

  • computerName 计算机名

  • osName 操作系统名称

  • osArch 架构

  • osVersion 系统版本

dynamicPath

可以在VM模板和路径配置中使用, 只能存放<String,String>的键值对, 可以通过TableItem.Builder.dynamicPathVariable(key, value)添加和覆盖, 文件的动态路径变量

  • packagePath 包路径 例如com/e/test
  • className 类名 来自表名转驼峰命名法, 可覆盖
  • lowercaseClassName 首字母小写的类名

template

只能在VM模板中使用, 和dynamicPath的区别是值可以是Object类型, 可以通过TableItem.Builder.templateVariable(key, value)添加和覆盖, value的值可以是Class, 例如放入CommonUtils.class 在模板中就可以调用该类中的静态方法 参考系统变量的configutils

  • 默认无

code-generator's People

Contributors

github-laziji avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.