Code generation
In the process of enterprise software development, most of the time is for the addition, deletion, modification, and development of database tables. Through the general addition, deletion, modification and query code generator, you can effectively improve efficiency and reduce costs; let the machine complete the regular repetitive work, and liberate developers.
MyBatis Generator
- MyBatis Generator is a code generation tool provided by MyBatis
It can help us generate the persistent object (po) corresponding to the table, the interface for operating the database (dao), and the xml (mapper) of CRUD sql.
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${last.version}</version> <configuration> <!--mybatis的代码生成器的配置策略文件--> <configurationFile>mybatis-generator-config.xml</configurationFile> </configuration></plugin>
- Configuration code generation related strategy file mybatis-generator-config.xml
<generatorConfiguration> <context> <!-- jdbc连接 --> <jdbcConnection> ... </jdbcConnection> <!-- schema为数据库名,tableName为对应的数据库表名 --> <table> ... </table> <!-- 注释 --> <commentGenerator> ... </commentGenerator> <!-- 类型转换 --> <javaTypeResolver> ... </javaTypeResolver> <!-- 生成实体类配置 --> <javaModelGenerator> ... </javaModelGenerator> <!-- 生成Mapper.xml文件配置 --> <sqlMapGenerator> ... </sqlMapGenerator> <!-- 生成Mapper.java 接口--> <javaClientGenerator> ... </javaClientGenerator> </context></generatorConfiguration>
- Disadvantage
- Each code generation needs to configure the corresponding mybatis-generator-config to configure related generation attributes and rules in the form of XML
- Unable to generate general Controller and Service classes, unable to customize templates, etc.
To sum up the two points: mybatis-generator is very inconvenient to use
EasyCode
EasyCode[1] is a code generation plug-in developed based on IntelliJ IDEA Ultimate, which mainly generates all kinds of codes you want through custom templates (based on velocity). Usually used to generate Entity, Dao, Service, Controller. If you have strong hands-on ability, it can also be used to generate HTML, JS, PHP and other codes. In theory, any code related to data can be generated.
Get started quickly
- Install IDEA EasyCode plug-in. Support online installation, just search and install the plug-in market.
- Use IDEA to connect to the target data source
No, you are still using MyBatis Generator? Try this tool
- Select target table for code generation
image.png
No, you are still using MyBatis Generator? Try this tool
Advanced configuration
As above, the single table-based addition, deletion, and modification method can be completed, including Controller, Service, Mapper, and Entity. But the default generation is a generic file based on the native MyBatis, which is not applicable to Mybatis extension plug-ins such as MyBatisPlus and Generic Mapper. We can dynamically add our generation rules by editing the EasyCode template file, and export them to others.
- You can even configure new templates to generate front-end pages, such as element-based additions, deletions, and changes.
image.png
to sum up
- Of course, many scaffolds have built-in code generation functions. For example, the development platform module of pig[2] realizes code generation in the form of a custom template engine, which can better integrate existing businesses and improve development efficiency.
No, you are still using MyBatis Generator? Try this tool
Reference
[1]EasyCode: https://github.com/makejavas/EasyCode
[2]pig: https://github.com/pig-mesh/pig