Skip to content

代码生成器使用

zouchangfu edited this page Jul 27, 2022 · 2 revisions

安装

在你的需要开发的业务系统中下载gobatis-cmd,然后安装到本地

  1. 下载gobatis-cmd
go get github.com/acmestack/gobatis-cmd/cmd/gobatis-cmd

image_IaezvJmoaf

  1. 安装gobatis-cmd
 go install github.com/acmestack/gobatis-cmd/cmd/gobatis-cmd

image_u--KVTc5Qj

此时gobatis-cmd 就已经安装到gopath目录中了。这里的演示使用的是window系统,其他系统类似,这里不做过多演示。

测试一下,使用gobatis-cmd -help 命令是否能生效。

image_jHirFt4rLV

出现以上信息,说明安装成功了。

使用

通过 -f 指定配置文件生成代码

gobatis-cmd -f configs/gobatis-conf.json

image_IFEGbCQhb7

{
  "driver": "mysql",
  "path": "./quick_start",
  "package": "test",
  "namespace": "test",
  "modelFile": "test_model.go",
  "tagName": "column",
  "mapperFile": "xml",
  "plugin": "",
  "keyword": false,
  "tableName": "",
  "dbName": "test",
  "host": "localhost",
  "port": 3306,
  "user": "root",
  "password": "123456"
}

或者使用命令参数:

gobatis-cmd -driver=mysql -host=localhost -port=3306 -user=test -pw=test -db=testdb -pkg=test_package -mapper=xml -namespace=test -path=.

参数介绍:

  -db string
        指定解析的数据库名称
  -driver string
        Driver (default "mysql")
  -host string
        数据库地址 (default "localhost")
  -model string
        生成的model文件名称 (default "models.go")
  -path string
        保存生成文件的路径
  -pkg string
        生成文件的包名 (default "acmestack/gobatis/default")
  -port int
        数据库的端口号  (default 3306)
  -pw string
        数据库的密码
  -table string
        指定生成的table名称
  -tag string
        生成Model的tag名称,多tag用逗号分隔"json,xml" (default "column")
  -user string
        数据库的用户名
  -mapper string
        mapper文件类型: xml | template | go默认xml-keyword bool
        是否自动添加转义符默认false如果为true则会根据driver名称辨识添加
  -namespace string
        添加namespace避免同表名冲突,(默认为空如果为空,自动填充为packageName.ModelName

会在当前的目录中生成三个文件:

在XML中,我们存放的是动态的SQL语句。

在test_model 中,我们存放数据库对应的实体和调用基础的CRUD方法。

在test_table_proxy中封装了基础的CRUD的实现方法,通过这里调用XML编写的动态SQL语句。

image_JZg24eaVAK