|
| 1 | +Doma CodeGen Plugin |
| 2 | +=================== |
| 3 | + |
| 4 | +Doma CodeGen Plugin is a gradle plugin. |
| 5 | +It generates Java source files and SQL files from Database. |
| 6 | + |
| 7 | +How to use |
| 8 | +---------- |
| 9 | + |
| 10 | +First, see [Gradle Plugin Portal](https://plugins.gradle.org/plugin/org.seasar.doma.codegen). |
| 11 | + |
| 12 | +The basic usage example is as follows: |
| 13 | + |
| 14 | +```groovy |
| 15 | +buildscript { |
| 16 | + repositories { |
| 17 | + mavenCentral() |
| 18 | + } |
| 19 | + dependencies { |
| 20 | + // specify your JDBC driver |
| 21 | + classpath 'com.h2database:h2:1.3.175' |
| 22 | + } |
| 23 | +} |
| 24 | +
|
| 25 | +plugins { |
| 26 | + id 'java' |
| 27 | + // specify the Doma CodeGen Plugin with correct version |
| 28 | + id 'org.seasar.doma.codegen' version 'x.x.x' |
| 29 | +} |
| 30 | +
|
| 31 | +domaCodeGen { |
| 32 | + // make an arbitrary named block |
| 33 | + dev { |
| 34 | + // JDBC url |
| 35 | + url = '...' |
| 36 | + // JDBC user |
| 37 | + user = '...' |
| 38 | + // JDBC password |
| 39 | + password = '...' |
| 40 | + // configuration for generated entity source files |
| 41 | + entity { |
| 42 | + packageName = 'org.example.entity' |
| 43 | + } |
| 44 | + // configuration for generated DAO source files |
| 45 | + dao { |
| 46 | + packageName = 'org.example.dao' |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +To generate all files, run `domaCodeGenDevAll` task: |
| 53 | + |
| 54 | +```bash |
| 55 | +$ ./gradlew domaCodeGenDevAll |
| 56 | +``` |
| 57 | + |
| 58 | +Gradle Tasks |
| 59 | +------------ |
| 60 | + |
| 61 | +The Doma CodeGen Plugin provides following tasks: |
| 62 | + |
| 63 | +- domaCodeGen*Name*All - Generate all. |
| 64 | +- domaCodeGen*Name*Dao - Generates DAO source files. |
| 65 | +- domaCodeGen*Name*Dto - Read resultSet metadata and generate a DTO source file. |
| 66 | +- domaCodeGen*Name*Entity - Generates entity source files. |
| 67 | +- domaCodeGen*Name*Sql - Generates SQL files. |
| 68 | +- domaCodeGen*Name*SqlTest - Generates SQL test source files. |
| 69 | + |
| 70 | +Note that each *Name* part in the above task names is replaced with the block name defined under the `domaCodeGen` block. |
| 71 | +In the above usage example, the *Dev* part is corresponding to the `dev` block. |
| 72 | + |
| 73 | +To check all defined task names, run the `tasks` task: |
| 74 | + |
| 75 | +```bash |
| 76 | +$ ./gradlew tasks |
| 77 | +``` |
| 78 | + |
| 79 | +Config Options |
| 80 | +-------------- |
| 81 | + |
| 82 | +### named config |
| 83 | + |
| 84 | +A named config must be under the `domaCodeGen` block. |
| 85 | +The name of the config is arbitrary. |
| 86 | +You can make multiple configs under the `domaCodeGen` block. |
| 87 | + |
| 88 | +In the following example, we define two configs - `sales` and `account`: |
| 89 | + |
| 90 | +```groovy |
| 91 | +domaCodeGen { |
| 92 | + sales { |
| 93 | + url = "jdbc:h2:mem:sales" |
| 94 | + } |
| 95 | + account { |
| 96 | + url = "jdbc:h2:mem:account" |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +| Option | Description | Values | Default | |
| 102 | +| :--- | :--- | :--- | :--- | |
| 103 | +| url | JDBC url | | | |
| 104 | +| user | JDBC user | | | |
| 105 | +| password | JDBC password | | | |
| 106 | +| dataSource | database data source | | inferred by the url | |
| 107 | +| codeGenDialect | database dialect | | inferred by the url | |
| 108 | +| schemaName | database schema name | | | |
| 109 | +| tableNamePattern | database table pattern (Regex) | | `.*` | |
| 110 | +| ignoredTableNamePattern | database ignored table pattern (Regex) | | `.*\$.*` | |
| 111 | +| tableTypes | database table type | such as TABLE, VIEW and so on | `TABLE` | |
| 112 | +| versionColumnNamePattern | database version column pattern (Regex) | | `VERSION([_]?NO)?` | |
| 113 | +| templateEncoding | encoding for freeMarker template files | | `UTF-8` | |
| 114 | +| templateDir | directory for user customized template files | | | |
| 115 | +| encoding | encoding for generated Java source files | | `UTF-8` | |
| 116 | +| sourceDir | directory for generated Java source files | | `src/main/java` | |
| 117 | +| testSourceDir | directory for generated Java test source files | | `src/test/java` | |
| 118 | +| resourceDir | directory for generated SQL files | | src/main/resources | |
| 119 | +| globalFactory | entry point to customize plugin behavior | | `new org.seasar.doma.gradle.codegen.GlobalFactory()` | |
| 120 | + |
| 121 | +### entity |
| 122 | + |
| 123 | +An `entity` block must be under a named config: |
| 124 | + |
| 125 | +```groovy |
| 126 | +domaCodeGen { |
| 127 | + sales { |
| 128 | + entity { |
| 129 | + useAccessor = false |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +| Option | Description | Values | Default | |
| 136 | +| :--- | :--- | :--- | :--- | |
| 137 | +| overwrite | where to overwrite generated entity files or not | | `true` | |
| 138 | +| overwriteListener | allow to overwrite listeners or not | | `false` | |
| 139 | +| superclassName | common superclass for generated entity classes | | | |
| 140 | +| listenerSuperclassName | common superclass for generated entity listener classes | | | |
| 141 | +| packageName | package name for generated entity class | | `example.entity` | |
| 142 | +| generationType | generation type for entity identities | enum value of `org.seasar.doma.gradle.codegen.desc.GenerationType` | | |
| 143 | +| namingType | naming convention | enum value of org.`seasar.doma.gradle.codegen.desc.NamingType` | | |
| 144 | +| initialValue | initial value for entity identities | | | |
| 145 | +| allocationSize | allocation size for entity identities | | | |
| 146 | +| showCatalogName | whether to show catalog names or not | | `false` | |
| 147 | +| showSchemaName | whether to show schema names or not | | `false` | |
| 148 | +| showTableName | whether to show table names or not | | `true` | |
| 149 | +| showColumnName | whether to show column names or not | | `true` | |
| 150 | +| showDbComment | whether to show database comments or not | | `true` | |
| 151 | +| useAccessor | whether to use accessors or not | | `true` | |
| 152 | +| useListener | whether to use listeners or not | | `true` | |
| 153 | +| originalStatesPropertyName | property to be annotated with `@OriginalStates` | | | |
| 154 | +| entityPropertyClassNamesFile | file used to resolve entity property classes | | | |
| 155 | +| prefix | prefix for entity classes | | | |
| 156 | +| suffix | suffix for entity classes | | | |
| 157 | + |
| 158 | +### dao |
| 159 | + |
| 160 | +A `dao` block must be under a named config: |
| 161 | + |
| 162 | +```groovy |
| 163 | +domaCodeGen { |
| 164 | + sales { |
| 165 | + dao { |
| 166 | + packageName = 'org.example.sales.dao' |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +| Option | Description | Values | Default | |
| 173 | +| :--- | :--- | :--- | :--- | |
| 174 | +| overwrite | whether to overwrite generated DAO files or not | | `false` | |
| 175 | +| packageName | package name for generated DAO classes | | `example.dao` | |
| 176 | +| suffix | suffix for Dao classes | | `Dao` | |
| 177 | +| configClassName | `org.seasar.doma.jdbc.Config` implemented class name. Tha name is used at @Dao | | `false` | |
| 178 | + |
| 179 | +### sql |
| 180 | + |
| 181 | +A `sql` block must be under a named config: |
| 182 | + |
| 183 | +```groovy |
| 184 | +domaCodeGen { |
| 185 | + sales { |
| 186 | + sql { |
| 187 | + overwrite = false |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +| Option | Description | Values | Default | |
| 194 | +| :--- | :--- | :--- | :--- | |
| 195 | +| overwrite | whether to overwrite generated sql files or not | | `true` | |
| 196 | + |
| 197 | + |
| 198 | +Customization |
| 199 | +------------- |
| 200 | + |
| 201 | +### Using custom template files |
| 202 | + |
| 203 | +Default template files are located in the source code repository of the Doma CodeGen Plugin. |
| 204 | +They are listed as follows: |
| 205 | + |
| 206 | +| Template File | Data Model Class | Generated Files | |
| 207 | +| :--- | :--- | :--- | |
| 208 | +| entity.ftl | org.seasar.doma.gradle.codege.desc.EntityDesc | entity source files| |
| 209 | +| entityListener.ftl | org.seasar.doma.gradle.codege.desc.EntityListenerDesc | entity listener source files | |
| 210 | +| dao.ftl | org.seasar.doma.gradle.codege.desc.DaoDesc | DAO source files | |
| 211 | +| sqlTestCase.ftl | org.seasar.doma.gradle.codege.desc.SqlTestDesc | test source files for SQL | |
| 212 | +| selectById.sql.ftl | org.seasar.doma.gradle.codege.desc.SqlDesc | SQL files | |
| 213 | +| selectByIdAndVersion.sql.ftl | org.seasar.doma.gradle.codege.desc.SqlDesc | SQL files | |
| 214 | + |
| 215 | +To create custom template files, copy them and modify their contents without changing file names. |
| 216 | +Then put them in the directory which is specified to the `templateDir` option. |
| 217 | + |
| 218 | +```groovy |
| 219 | +domaCodeGen { |
| 220 | + dev { |
| 221 | + url = '...' |
| 222 | + user = '...' |
| 223 | + password = '...' |
| 224 | + // specify the directory including your custom template files |
| 225 | + templateDir = "$projectDir/template" |
| 226 | + entity { |
| 227 | + packageName = 'org.example.entity' |
| 228 | + } |
| 229 | + dao { |
| 230 | + packageName = 'org.example.dao' |
| 231 | + } |
| 232 | + } |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +The Doma CodeGen Plugin uses [Apache FreeMarker](https://freemarker.apache.org/) to process the template files. |
| 237 | + |
| 238 | +Sample Project |
| 239 | +-------------- |
| 240 | + |
| 241 | +- [codegen-sample](https://github.com/domaframework/codegen-sample) |
| 242 | + |
| 243 | +License |
| 244 | +------- |
| 245 | + |
| 246 | +``` |
| 247 | +Copyright 2020 domaframework.org |
| 248 | +
|
| 249 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 250 | +you may not use this file except in compliance with the License. |
| 251 | +You may obtain a copy of the License at |
| 252 | +
|
| 253 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 254 | +
|
| 255 | +Unless required by applicable law or agreed to in writing, software |
| 256 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 257 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 258 | +See the License for the specific language governing permissions and |
| 259 | +limitations under the License. |
| 260 | +``` |
0 commit comments