|
| 1 | +## Using Templates to Generate Code |
| 2 | + |
| 3 | +### Starting from version 2025.1.1, EasyCode-MybatisCodeHelper will switch to a freemium model. Only template code hint functionality will require payment; other features such as preview and code generation will remain free. |
| 4 | + |
| 5 | +### For users with customization needs, such as adding company-specific comments or using internal frameworks, template-based code generation is recommended. |
| 6 | + |
| 7 | +### Install EasyCodeMybatisCodeHelper(1.4.0) plugin for template generation [Download Link](https://plugins.jetbrains.com/plugin/13847-easycode-mybatiscodehelper) |
| 8 | + |
| 9 | +## Features: template code hints, real-time preview, direct IDEA editor template operations, multiple group configuration, and direct code generation from templates. The most convenient template code generation plugin. |
| 10 | + |
| 11 | +Video tutorial: https://www.bilibili.com/video/BV19C4y1Y7RN |
| 12 | + |
| 13 | +GitHub: https://github.com/gejun123456/EasyCodeMybatisCodeHelperTemplates - download template files |
| 14 | +Copy template files directly to your project's easyCode directory or Scratches And Consoles' /extensions/EasyCode for code generation. Includes code hints, real-time preview, and direct template editing in IDEA editor. |
| 15 | + |
| 16 | +Template preview: |
| 17 | + |
| 18 | + |
| 19 | +Generate code from database tables in IDEA (multiple table selection supported): |
| 20 | + |
| 21 | + |
| 22 | +Currently supports code generation from either the project's easyCode directory or the scratch directory's /extensions/EasyCode |
| 23 | + |
| 24 | +The project's easyCode directory is recommended as it can be version controlled with git and shared with teammates to prevent loss. |
| 25 | + |
| 26 | + |
| 27 | +### What is group.json for? |
| 28 | +group.json configures relationships between templates, globalConfig, and typeMapper through group names in JSON. A project can have multiple generation groups. |
| 29 | + |
| 30 | +### New to Template Writing? |
| 31 | +Templates use Velocity syntax. Documentation: https://velocity.apache.org/. Contact me for template-related issues. |
| 32 | + |
| 33 | +## Upgrade Notice |
| 34 | +Due to storage changes in version 1.2.8, when upgrading from pre-1.2.7 versions, export templates to JSON first, then upgrade and import the JSON. |
| 35 | + |
| 36 | +## Template-Generated Code Has Joined Columns Without Commas |
| 37 | +In IDEA 2023.1's Velocity upgrade, change $velocityHasNext to $foreach.hasNext |
| 38 | + |
| 39 | +### Empty projectPath |
| 40 | +For projects with parallel modules without a main project, set projectPath in mybatisCodeHelper.vm: |
| 41 | +```velocity |
| 42 | +#set($projectPath="D:/workspace/idea/XXModule") |
| 43 | +``` |
| 44 | + |
| 45 | +## GenerateCode(old) vs GenerateFromEasyCodeFolder(new) |
| 46 | +GenerateCode(old) uses settings-configured templates and configurations. |
| 47 | +GenerateFromEasyCodeFolder(new) generates from easyCode folder templates. |
| 48 | +GenerateFromEasyCodeFolder(new) is recommended as it provides code hints, real-time preview, direct editing, and git integration. |
| 49 | + |
| 50 | +## Template Code Hints |
| 51 | +Click "Add dependency for code completion" at template top for automatic dependency addition. |
| 52 | +This enables code hints while editing. Remove dependency when done. |
| 53 | + |
| 54 | +## Why Package Names and Paths in mybatisCodeHelper.vm? |
| 55 | +This approach facilitates git version control and team sharing without individual configuration. |
| 56 | +Future updates will enable template-file comparison and add auto-suggestions for paths and package names. |
| 57 | + |
| 58 | +## Template Examples |
| 59 | +#### Remove Table Name Prefix |
| 60 | +Edit mybatisCodeHelper.vm in globalconfig: |
| 61 | +```velocity |
| 62 | +#if($tableInfo.obj.name.startsWith("table_")) |
| 63 | +$!tableInfo.setName($tableInfo.name.substring(5)) |
| 64 | +#end |
| 65 | +``` |
| 66 | + |
| 67 | +Or define a variable: |
| 68 | +```velocity |
| 69 | +#set($entityName=$tableInfo.name) |
| 70 | +#if($tableInfo.obj.name.startsWith("table_")) |
| 71 | +#set($entityName=$tableInfo.name.substring(5)) |
| 72 | +#end |
| 73 | +``` |
| 74 | +Use ${entityName} in templates. Multiple templates can reference this variable. |
| 75 | + |
| 76 | +#### Add Entity Class Suffix |
| 77 | +```velocity |
| 78 | +#set($entityName=$tableInfo.name) |
| 79 | +#set($entityName = $tool.append($entityName,'Entity')) |
| 80 | +``` |
| 81 | +Use ${entityName} in templates. |
| 82 | + |
| 83 | +#### Remove Field Prefix |
| 84 | +Edit mybatisCodeHelper.vm in globalconfig: |
| 85 | +```velocity |
| 86 | +#set($removeColumnPrefix="f_") |
| 87 | +#foreach($column in $tableInfo.fullColumn) |
| 88 | +#if($column.obj.name.startsWith($removeColumnPrefix)) |
| 89 | +$!column.setName($tool.firstLowerCase($column.getName().substring(1))) |
| 90 | +#end |
| 91 | +#end |
| 92 | +``` |
| 93 | + |
| 94 | +#### Remove Specific Insert Columns |
| 95 | +Edit your XML template (e.g., insertBatch): |
| 96 | +```xml |
| 97 | +#set($insertSkipFields = ["create_time","update_time"]) |
| 98 | +<insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true"> |
| 99 | + insert into $!{tableInfo.obj.name} |
| 100 | + ( |
| 101 | +#foreach($column in $tableInfo.otherColumn) |
| 102 | +#if($insertSkipFields.contains($column.obj.name)) |
| 103 | +#elseif($foreach.hasNext) |
| 104 | + $!column.obj.name, |
| 105 | + #else |
| 106 | + $!column.obj.name |
| 107 | + #end#end |
| 108 | + ) |
| 109 | + values |
| 110 | + <foreach collection="entities" item="entity" separator=","> |
| 111 | + ( |
| 112 | +#foreach($column in $tableInfo.otherColumn) |
| 113 | +#if($insertSkipFields.contains($column.obj.name)) |
| 114 | +#elseif($foreach.hasNext) |
| 115 | + #{entity.$!{column.name}}, |
| 116 | +#else |
| 117 | + #{entity.$!{column.name}} |
| 118 | +#end#end |
| 119 | + ) |
| 120 | + </foreach> |
| 121 | +</insert> |
| 122 | +``` |
| 123 | + |
| 124 | +#### Add jdbcType and typeHandler |
| 125 | +Edit mybatisCodeHelper.vm in globalconfig: |
| 126 | +```velocity |
| 127 | +#if($tool.newHashSet("java.lang.String").contains($column.type)) |
| 128 | + #set($jdbcType="VARCHAR") |
| 129 | + #elseif($tool.newHashSet("java.lang.Integer","int").contains($column.type)) |
| 130 | + #set($jdbcType="INTEGER") |
| 131 | + #else |
| 132 | + ##other types |
| 133 | + #set($jdbcType="VARCHAR") |
| 134 | +#end |
| 135 | +$tool.call($column.ext.put("jdbcType", $jdbcType)) |
| 136 | +``` |
| 137 | +Then use in XML (ext is a map for custom properties): |
| 138 | +```xml |
| 139 | +#{$!{column.name},jdbcType=$!{column.ext.jdbcType}} |
| 140 | +``` |
| 141 | + |
| 142 | +#### Get Table Name, Field Name, Field Type, Schema Name |
| 143 | +```velocity |
| 144 | +Table name = tableInfo.obj.name |
| 145 | +Field name = column.obj.name |
| 146 | +Field type = $!tool.getField($tableInfo.fullColumn.get(0).obj.dataType, "typeName") |
| 147 | +Field Java type = column.type |
| 148 | +Schema name = ${tableInfo.obj.getParent().getName()} |
| 149 | +``` |
| 150 | + |
| 151 | +### Note: EasyCodeMybatisCodeHelper plugin is forked from https://github.com/makejavas/EasyCode, modified for MybatisCodeHelperPro compatibility and direct template-based code generation. |
0 commit comments